PayU / api-schema-builder

Apache License 2.0
32 stars 18 forks source link

Loadash.get doesn't work without quotes #92

Open SlavMAK opened 3 years ago

SlavMAK commented 3 years ago

Hi! ๐Ÿ‘‹

Firstly, thanks for your work on this project! ๐Ÿ™‚

Today I used patch-package to patch api-schema-builder@2.0.8 for the project I'm working on.

Here is the diff that solved my problem:

diff --git a/node_modules/api-schema-builder/src/parsers/open-api3.js b/node_modules/api-schema-builder/src/parsers/open-api3.js
index c0665d6..f4c2cf0 100644
--- a/node_modules/api-schema-builder/src/parsers/open-api3.js
+++ b/node_modules/api-schema-builder/src/parsers/open-api3.js
@@ -17,13 +17,13 @@ module.exports = {
 };

 function buildRequestBodyValidation(dereferenced, referenced, currentPath, currentMethod, options) {
-    const contentTypes = get(dereferenced, `paths[${currentPath}][${currentMethod}].requestBody.content`);
+    const contentTypes = get(dereferenced, `paths['${currentPath}']['${currentMethod}'].requestBody.content`);
     if (!contentTypes) {
         return;
     }

     // Add default validator for default content type for compatibility sake
-    const requestPath = `paths[${currentPath}][${currentMethod}].requestBody.content[${schemaUtils.DEFAULT_REQUEST_CONTENT_TYPE}].schema`;
+    const requestPath = `paths['${currentPath}']['${currentMethod}'].requestBody.content['${schemaUtils.DEFAULT_REQUEST_CONTENT_TYPE}'].schema`;

     const dereferencedBodySchema = get(dereferenced, requestPath);
     const referencedBodySchema = get(referenced, requestPath);
@@ -37,7 +37,7 @@ function buildRequestBodyValidation(dereferenced, referenced, currentPath, curre

     // Add validators for all content types
     const schema = Object.keys(contentTypes).reduce((result, contentType) => {
-        const requestPath = `paths[${currentPath}][${currentMethod}].requestBody.content[${contentType}].schema`;
+        const requestPath = `paths['${currentPath}']['${currentMethod}'].requestBody.content['${contentType}'].schema`;

         const dereferencedBodySchema = get(dereferenced, requestPath);
         const referencedBodySchema = get(referenced, requestPath);
@@ -55,7 +55,7 @@ function buildRequestBodyValidation(dereferenced, referenced, currentPath, curre
 }

 function buildResponseBodyValidation(dereferenced, referenced, currentPath, currentMethod, statusCode, options) {
-    const contentTypes = get(dereferenced, `paths[${currentPath}][${currentMethod}].responses[${statusCode}].content`);
+    const contentTypes = get(dereferenced, `paths['${currentPath}']['${currentMethod}'].responses['${statusCode}'].content`);
     if (!contentTypes) {
         return;
     }
@@ -135,7 +135,7 @@ function handleSchema(data) {
 }

 function buildHeadersValidation(responses, statusCode, { ajvConfigParams, formats, keywords, contentTypeValidation }) {
-    const headers = get(responses, `[${statusCode}].headers`);
+    const headers = get(responses, `['${statusCode}'].headers`);
     if (!headers) return;

     const defaultAjvOptions = {

This issue body was partially generated by patch-package.

SlavMAK commented 3 years ago

Otherwise, the body validator is always empty and does not work.

drave23 commented 2 years ago

Can you share your swagger or params that cause the issue?

Enome commented 8 months ago

Just ran into this issue as well, not sure this will get fixed here but I'll mention it in case others run into the same issue.

AdonisJS / Japa has a dependency api-schema-builder for testing. If in your openapi file you use a vendor specific mimetype that contains dots then your res.assertAgainstApiSpec(); tests will fail because api-schema-builder is not using quotes with lodash get.

This will return undefined:

get(
  {
    response: {
      200: { content: { "application/vnd.abc+json": "abc" } },
    },
  },
  "response[200].content[application/vnd.abc+json]"
)

This will work:

get(
  {
    response: {
      200: { content: { "application/vnd.abc+json": "abc" } },
    },
  },
  "response['200'].content['application/vnd.abc+json']"
)