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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

response expected in response data #915

Open
lpelleau opened this issue May 15, 2024 · 0 comments
Open

response expected in response data #915

lpelleau opened this issue May 15, 2024 · 0 comments

Comments

@lpelleau
Copy link

lpelleau commented May 15, 2024

Describe the bug
express-openapi-validator expects json API responses to be under a response object, where the OpenAPI spec doesn't.

To Reproduce
OpenAPI spec:

paths:
  /merchants:
    get:
      x-eov-operation-id: merchants#list
      x-eov-operation-handler: routes
      responses:
        "200":
          description: Successful operation
          content:
            application/json:
              schema:
                type: object
                properties:
                  data:
                    type: array
                    $ref: "#/components/schemas/Merchant"

express config:

app.use(
  OpenApiValidator.middleware({
    apiSpec,
    validateResponses: true,
    operationHandlers: path.join(__dirname),
    validateSecurity: ...,
  })
);

handler:

app.use((req: Request, res: Response, next: NextFunction) => {
    const response = // { data: [{ item1 }, { item2 }] } => fetch data
    res.status(response.statusCode).json(response);
    next();
});

Actual behavior
I get this error:

{
   "message":"/response/data must be object",
   "errors":[
      {
         "path":"/response/data",
         "message":"must be object",
         "errorCode":"type.openapi.validation"
      }
   ]
}

Expected behavior
I would expect my data, conform to the OpenAPI spec, to be returned, without expecting an additional response wrapping the OpenAPI spec.
See the added response here: https://github.com/cdimascio/express-openapi-validator/blame/5c98d17c470aecf53f60d3fb3bda4005d65aa9aa/src/middlewares/openapi.response.validator.ts#L281

When I replace this line with:

        responseSchemas[name] = {
          ...responseSchemas[name],
          [mediaTypeToValidate]: {
            // $schema: 'http://json-schema.org/schema#',
            // $schema: "http://json-schema.org/draft-04/schema#",
            type: 'object',
            properties: {
              ...schema,
            },
            components: this.spec.components ?? {},
          },
        };

I get the expected behaviour out of express-openapi-validator

I must be doing something unexpected as I could not find any similar experience, if that's the case, I would appreciate some guidelines 🙏

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

No branches or pull requests

1 participant