kogosoftwarellc / open-api

A Monorepo of various packages to power OpenAPI in node
MIT License
895 stars 237 forks source link

Validation of similar paths produce error on required parameters #663

Closed andrejflorjancic closed 3 years ago

andrejflorjancic commented 4 years ago

Framework: express openapi: 6.0.0 swagger: 2.0

When using paths validation has problems on similar paths.

Example:

<project>
        paths/
               users/
                     {id}.js
               articles/
                     {id}.js

users/{id}.js:

module.exports.get.apiDoc = {
  description: 'A description for retrieving a user.',
  tags: ['users'],
  operationId: 'getUser',
  // parameters for this operation
  parameters: [
    {
      in: 'path',
      name: 'id',
      type: 'string'
    }, {
      in: 'query',
      name: 'country',
      required: true,
      type: 'string'
    }
  ],
  responses: {
    default: {
      $ref: '#/definitions/Error'
    }
  }
};

articles/{id}.js:

module.exports.get.apiDoc = {
  description: 'A description for retrieving a article.',
  tags: ['articles'],
  operationId: 'getArticle',
  // parameters for this operation
  parameters: [
    {
      in: 'path',
      name: 'id',
      type: 'string'
    }, {
      in: 'query',
      name: 'store',
      required: true,
      type: 'string'
    }
  ],
  responses: {
    default: {
      $ref: '#/definitions/Error'
    }
  }
};

Calling ../users/{id} will produce error should have required property 'store'.

Renaming path articles to xarticles and than calling ../xarticles/{id} will produce error should have required property 'country'.

malnor commented 3 years ago

Been sitting with this for a day now. Have a very similar problem /clients GET getting mixed up with /client/{client_id} GET.

malnor commented 3 years ago

@andrejflorjancic after debugging the code I found that the problem was actually my code and how I attached the apiDoc to the object that caused problems. module.exports.get.apiDoc = xxx; isn't the best way of doing this.

So this can be ignored. Even though this is actually more of a javascript problem, the documentation could be more clear on having the same method multiple times requires a different pattern. It may be possible to improve the code to avoid this also, but I'm not sure on that.

andrejflorjancic commented 3 years ago

thx