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

Api definition merging produces invalid schemas #38

Open
sigmasoldi3r opened this issue Jan 29, 2020 · 2 comments
Open

Api definition merging produces invalid schemas #38

sigmasoldi3r opened this issue Jan 29, 2020 · 2 comments

Comments

@sigmasoldi3r
Copy link

I'm using routing controllers openapi, along with typeorm and routing controllers typeorm extension.

This enables you to implicitly transform an ID to an entity from the DB, like:

  @Get('/')
  @ResponseSchema(TaskRecord, { isArray: true })
  @OpenAPI({
    summary: 'Lists all records of the user.'
  })
  public async getAll(@EntityFromQuery('user') user: User) {
    return await this.records.getRecordsOf(user);
  }

But the problem is that the parameter passed is an integer in fact, but the OpenAPI spec generator thinks that the parameter is an entity/object, to circumvent this I've added the parameter to the OpenAPI decorator, like:

    parameters: [
      { name: 'user', in: 'query', schema: { type: 'integer', minimum: 0 }, required: true }
    ],

But (I suppose that) because of the merging logic of the decorators (Mentioned in the readme - "Multiple @OpenAPI decorators"), the integer type and the entity $ref gets merged:

"parameters": [
  {
    "in": "query",
    "name": "user",
    "required": true,
    "schema": {
      "$ref": "#/components/schemas/User",
      "type": "integer",
      "minimum": 0,
    }
  }
]

Which is wrong, and swagger UI displays it as "User" type:

imagen

Can we avoid that in some way? (IE: Specify that the field must not be merged)

@sigmasoldi3r sigmasoldi3r changed the title Decorator merge produces invalid schemas in parameters Api definition merging produces invalid schemas Jan 29, 2020
@epiphone
Copy link
Owner

epiphone commented Jan 29, 2020

Instead of passing OpenAPI an object try passing a function which performs the merge manually. From the docs:

Alternatively you can call @openapi with a function of type (source: OperationObject, route: IRoute) => OperationObject, i.e. a function receiving the existing spec as well as the target route, spitting out an updated spec. This function parameter can be used to implement for example your own merging logic or custom decorators.

You might first want to try passing in a function that just logs its parameters to get a sense of what's going on.

@sigmasoldi3r
Copy link
Author

I'll check it out! Didn't see that function feature. Anyway I'm patching the object before sending it for now, so works cool.

The project is great by the way! Thanks and keep it up 💪

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

2 participants