Repository: codekie/openapi-examples-validator
Pull request #65 (2020-04-29 12:31:44)

fix statistics for `schemasWithExamples` over multiple mapping-f…
…iles


Patch:
diff --git a/src/index.js b/src/index.js
index 9d51b19..bcfccac 100644
--- a/src/index.js
+++ b/src/index.js
@@ -314,11 +314,16 @@ function _mergeValidationResponses(response1, response2) {
         statistics: _.entries(response1.statistics)
             .reduce((res, [key, val]) => {
                 if (PROP__SCHEMAS_WITH_EXAMPLES === key) {
-                    const schemasWithExample = response2.statistics[SYM__INTERNAL][PROP__SCHEMAS_WITH_EXAMPLES]
-                        .values();
-                    for (let schema of schemasWithExample) {
-                        res[SYM__INTERNAL][PROP__SCHEMAS_WITH_EXAMPLES].add(schema);
-                    }
+                    [
+                        response1,
+                        response2
+                    ].forEach(response => {
+                        const schemasWithExample = response.statistics[SYM__INTERNAL][PROP__SCHEMAS_WITH_EXAMPLES]
+                            .values();
+                        for (let schema of schemasWithExample) {
+                            res[SYM__INTERNAL][PROP__SCHEMAS_WITH_EXAMPLES].add(schema);
+                        }
+                    });
                     return res;
                 }
                 res[key] = val + response2.statistics[key];


Test patch:
diff --git a/test/data/v3/simple-api-with-examples-and-missing-schema.json b/test/data/v3/simple-api-with-examples-and-missing-schema.json
new file mode 100644
index 0000000..dc5ae6a
--- /dev/null
+++ b/test/data/v3/simple-api-with-examples-and-missing-schema.json
@@ -0,0 +1,240 @@
+{
+  "openapi": "3.0.0",
+  "info": {
+    "version": "1.0.0",
+    "title": "Swagger Petstore",
+    "license": {
+      "name": "MIT"
+    }
+  },
+  "servers": [
+    {
+      "url": "http://petstore.swagger.io/v1"
+    }
+  ],
+  "paths": {
+    "/pets": {
+      "get": {
+        "summary": "List all pets",
+        "operationId": "listPets",
+        "tags": [
+          "pets"
+        ],
+        "parameters": [
+          {
+            "name": "limit",
+            "in": "query",
+            "description": "How many items to return at one time (max 100)",
+            "required": false,
+            "schema": {
+              "type": "number"
+            }
+          }
+        ],
+        "responses": {
+          "200": {
+            "description": "An paged array of pets",
+            "headers": {
+              "x-next": {
+                "description": "A link to the next page of responses",
+                "schema": {
+                  "type": "string"
+                }
+              }
+            },
+            "content": {
+              "application/json": {
+                "examples": {
+                  "single": {
+                    "value": [
+                      {
+                        "id": 0,
+                        "name": "Herbert",
+                        "tag": "Doggo"
+                      }
+                    ]
+                  },
+                  "two": {
+                    "value": [
+                      {
+                        "id": 0,
+                        "name": "Herbert",
+                        "tag": "Doggo"
+                      },
+                      {
+                        "id": 1,
+                        "name": "Mannie",
+                        "tag": "Cat"
+                      }
+                    ]
+                  },
+                  "three": {
+                    "value": [
+                      {
+                        "id": 0,
+                        "name": "Herbert",
+                        "tag": "Doggo"
+                      },
+                      {
+                        "id": 1,
+                        "name": "Mannie",
+                        "tag": "Cat"
+                      },
+                      {
+                        "id": 2,
+                        "name": "Fluffers",
+                        "tag": "Bunny"
+                      }
+                    ]
+                  }
+                }
+              }
+            }
+          },
+          "default": {
+            "description": "unexpected error",
+            "content": {
+              "application/json": {
+                "schema": {
+                  "required": [
+                    "code",
+                    "message"
+                  ],
+                  "properties": {
+                    "code": {
+                      "type": "number"
+                    },
+                    "message": {
+                      "type": "string"
+                    }
+                  }
+                }
+              }
+            }
+          }
+        }
+      },
+      "post": {
+        "summary": "Create a pet",
+        "operationId": "createPets",
+        "tags": [
+          "pets"
+        ],
+        "responses": {
+          "201": {
+            "description": "Null response"
+          },
+          "default": {
+            "description": "unexpected error",
+            "content": {
+              "application/json": {
+                "schema": {
+                  "required": [
+                    "code",
+                    "message"
+                  ],
+                  "properties": {
+                    "code": {
+                      "type": "number"
+                    },
+                    "message": {
+                      "type": "string"
+                    }
+                  }
+                },
+                "examples": {
+                  "default": {
+                    "value": {
+                      "code": 100,
+                      "message": "Application error"
+                    }
+                  }
+                }
+              }
+            }
+          }
+        }
+      }
+    },
+    "/pets/{petId}": {
+      "get": {
+        "summary": "Info for a specific pet",
+        "operationId": "showPetById",
+        "tags": [
+          "pets"
+        ],
+        "parameters": [
+          {
+            "name": "petId",
+            "in": "path",
+            "required": true,
+            "description": "The id of the pet to retrieve",
+            "schema": {
+              "type": "string"
+            }
+          }
+        ],
+        "responses": {
+          "200": {
+            "description": "Expected response to a valid request",
+            "content": {
+              "application/json": {
+                "schema": {
+                  "type": "array",
+                  "items": {
+                    "required": [
+                      "id",
+                      "name"
+                    ],
+                    "properties": {
+                      "id": {
+                        "type": "number"
+                      },
+                      "name": {
+                        "type": "string"
+                      },
+                      "tag": {
+                        "type": "string"
+                      }
+                    }
+                  }
+                },
+                "examples": {
+                  "default": {
+                    "value": [
+                      {
+                        "id": 15,
+                        "name": "Elliot"
+                      }
+                    ]
+                  }
+                }
+              }
+            }
+          },
+          "default": {
+            "description": "unexpected error",
+            "content": {
+              "application/json": {
+                "schema": {
+                  "required": [
+                    "code",
+                    "message"
+                  ],
+                  "properties": {
+                    "code": {
+                      "type": "number"
+                    },
+                    "message": {
+                      "type": "string"
+                    }
+                  }
+                }
+              }
+            }
+          }
+        }
+      }
+    }
+  }
+}
diff --git a/test/specs/impl/v2/index.js b/test/specs/impl/v2/index.js
index 65f0f80..342e310 100644
--- a/test/specs/impl/v2/index.js
+++ b/test/specs/impl/v2/index.js
@@ -326,7 +326,7 @@ describe('Main-module, for v2 should', () => {
         it('should collect the statistics over all mapping-files', async() => {
             (await validateExamplesByMap(FILE_PATH__EXTERNAL_EXAMPLES_SCHEMA, FILE_PATH__EXTERNAL_EXAMPLES_GLOB))
                 .statistics.should.deep.equal({
-                    schemasWithExamples: 2,
+                    schemasWithExamples: 4,
                     examplesWithoutSchema: 0,
                     examplesTotal: 7,
                     matchingFilePathsMapping: 2
diff --git a/test/specs/impl/v3/index.js b/test/specs/impl/v3/index.js
index 4859dd8..a9002f4 100644
--- a/test/specs/impl/v3/index.js
+++ b/test/specs/impl/v3/index.js
@@ -10,6 +10,8 @@ const JSON_PATH__CONTEXT_MUTUALLY_EXCLUSIVE = '/paths/~1pets/get/responses/200/c
     REL_PATH__WITH_INTERNAL_REFS = 'v3/simple-api-with-examples-with-refs',
     REL_PATH__EXAMPLE__INVALID__WITH_INTERNAL_REFS = 'v3/simple-api-with-example-with-refs-invalid',
     REL_PATH__EXAMPLES__INVALID__WITH_INTERNAL_REFS = 'v3/simple-api-with-examples-with-refs-invalid',
+    FILE_PATH__EXAMPLES_WITH_MISSING_SCHEMA
+        = path.join(__dirname, '../../../data/v3/simple-api-with-examples-and-missing-schema.json'),
     FILE_PATH__INVALID__YAML = path.join(__dirname, '../../../data/v3/simple-api-with-examples-with-refs-invalid.yaml'),
     FILE_PATH__INVALID__YML = path.join(__dirname, '../../../data/v3/simple-api-with-examples-with-refs-invalid.yml'),
     FILE_PATH__VALID__REQUEST_PARAMETER = path.join(__dirname, '../../../data/v3/request-valid-parameter.json'),
@@ -226,4 +228,10 @@ describe('Main-module, for v3 should', function() {
             });
         });
     });
+    describe('statistics for examples, without schema', function() {
+        it('should return the right amount', async function() {
+            (await validateFile(FILE_PATH__EXAMPLES_WITH_MISSING_SCHEMA))
+                .statistics.schemasWithExamples.should.equal(2);
+        });
+    });
 });