Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Loadash.get doesn't work without quotes #92

Open
SlavMAK opened this issue Aug 13, 2021 · 3 comments
Open

Loadash.get doesn't work without quotes #92

SlavMAK opened this issue Aug 13, 2021 · 3 comments

Comments

@SlavMAK
Copy link

SlavMAK commented Aug 13, 2021

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
Copy link
Author

SlavMAK commented Aug 13, 2021

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

@drave23
Copy link

drave23 commented Jan 2, 2022

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

@Enome
Copy link

Enome commented Dec 19, 2023

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']"
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants