Repository: Redocly/redocly-cli
Pull request #717 (2022-05-27 08:59:23)

fix: allow scalar property with falsy values
## What/Why/How?
`scalar-property-missing-example` rule was reporting an error for falsy values like '', 0, false in the example.

## Reference
N/a

## Testing

## Screenshots (optional)

## Check yourself

- [x] Code is linted
- [ ] Tested with redoc/reference-docs/workflows
- [x] All new/updated code is covered with tests

## Security

- [x] Security impact of change has been considered
- [x] Code follows company security practices and guidelines


AntonKozachuk: @RomanHotsiy one concern is that an example can sometimes have a `null` value for nullable properties. What do you think about this?
github-actions[bot]: <!-- jest coverage report action at  -->

## Coverage report 


### Total coverage

| Status | Category   | Percentage | Covered / Total |
| :----: | :--------- | :--------- | :-------------: |
|   🟡   | Statements | 65.91%     |    2639/4004    |
|   🟡   | Branches   | 60.02%     |    1449/2414    |
|   🔴   | Functions  | 57.91%     |     421/727     |
|   🟡   | Lines      | 66.15%     |    2441/3690    |

> Status of coverage: 🟢 - ok, 🟡 - slightly more than threshold, 🔴 - under the threshold




<p align="right">Report generated by <a href="https://github.com/ArtiomTr/jest-coverage-report-action">🧪jest coverage report action</a> from 47e7fd61def28fa1d47ee1992cd51ffd6f01f9f9</p>


Patch:
diff --git a/packages/core/src/rules/common/scalar-property-missing-example.ts b/packages/core/src/rules/common/scalar-property-missing-example.ts
index 03da8245..ff640241 100644
--- a/packages/core/src/rules/common/scalar-property-missing-example.ts
+++ b/packages/core/src/rules/common/scalar-property-missing-example.ts
@@ -19,7 +19,10 @@ export const ScalarPropertyMissingExample: Oas3Rule | Oas2Rule = () => {
           continue;
         }
 
-        if (!propSchema.example && !(propSchema as Oas3_1Schema).examples) {
+        if (
+          propSchema.example === undefined &&
+          (propSchema as Oas3_1Schema).examples === undefined
+        ) {
           report({
             message: `Scalar property should have "example"${
               oasVersion === OasVersion.Version3_1 ? ' or "examples"' : ''


Test patch:
diff --git a/packages/core/src/rules/common/__tests__/scalar-property-missing-example.test.ts b/packages/core/src/rules/common/__tests__/scalar-property-missing-example.test.ts
index a64f4107..50ef7ffb 100644
--- a/packages/core/src/rules/common/__tests__/scalar-property-missing-example.test.ts
+++ b/packages/core/src/rules/common/__tests__/scalar-property-missing-example.test.ts
@@ -204,4 +204,61 @@ describe('Oas3.1 scalar-property-missing-example', () => {
 
     expect(replaceSourceWithRef(results)).toMatchInlineSnapshot(`Array []`);
   });
+
+  it('should not report on a scalar property of falsy values', async () => {
+    const document = parseYamlToDocument(
+      outdent`
+        openapi: 3.0.0
+        components:
+          schemas:
+            User:
+              type: object
+              properties:
+                testBool:
+                  type: boolean
+                  example: false
+                testString:
+                  type: string
+                  example: ""
+                testNumber:
+                  type: number
+                  example: 0
+      `,
+      'foobar.yaml',
+    );
+
+    const results = await lintDocument({
+      externalRefResolver: new BaseResolver(),
+      document,
+      config: await makeConfig({ 'scalar-property-missing-example': 'error' }),
+    });
+
+    expect(replaceSourceWithRef(results)).toMatchInlineSnapshot(`Array []`);
+  });
+
+  it('should not report on a nullable scalar property values', async () => {
+    const document = parseYamlToDocument(
+      outdent`
+        openapi: 3.0.0
+        components:
+          schemas:
+            User:
+              type: object
+              properties:
+                testBool:
+                  type: string
+                  nullable: true
+                  example: null
+      `,
+      'foobar.yaml',
+    );
+
+    const results = await lintDocument({
+      externalRefResolver: new BaseResolver(),
+      document,
+      config: await makeConfig({ 'scalar-property-missing-example': 'error' }),
+    });
+
+    expect(replaceSourceWithRef(results)).toMatchInlineSnapshot(`Array []`);
+  });
 });