welovecoding/swaxios

Common parameters are not handled

mfodor opened this issue · 3 comments

Official docs:
https://swagger.io/docs/specification/describing-parameters/#common

My valid configuration:

paths:
  /posts/{id}:
    # This is considered as a request method then trows error
    parameters:
      - name: id
        in: path
        description: id
        required: true
        schema:
          type: integer
          format: int32
    get:
      # ...

Error on console:

TypeError: Cannot convert undefined or null to object
    at Function.entries (<anonymous>)
    at MethodGenerator.includesSuccessResponse (node_modules/swaxios/dist/generators/MethodGenerator.js:62:54)
    at new MethodGenerator (node_modules/swaxios/dist/generators/MethodGenerator.js:49:18)
    at new ResourceGenerator (node_modules/swaxios/dist/generators/ResourceGenerator.js:40:42)
    at node_modules/swaxios/dist/Swaxios.js:45:34
    at Generator.next (<anonymous>)
    at node_modules/swaxios/dist/Swaxios.js:8:71
    at new Promise (<anonymous>)
    at __awaiter (node_modules/swaxios/dist/Swaxios.js:4:12)
    at exportServices (node_modules/swaxios/dist/Swaxios.js:34:12)
// new MethodGenerator (node_modules/swaxios/dist/generators/MethodGenerator.js:49:18)
const postFix = parameterMatch ? `By${StringUtil.camelCase(parameterMatch.splice(1), true)}` : 'All';
this.parameterMethod = this.operation.operationId || `${this.method}${postFix}`;
if (this.includesSuccessResponse(this.responses)) {
// where
//  - postFix: ById
//  - parameterMethod: propertiesById
//  - this.responses: undefined

// MethodGenerator.includesSuccessResponse (node_modules/swaxios/dist/generators/MethodGenerator.js:62:54
includesSuccessResponse(responses) {
    for (const [successCode, response] of Object.entries(responses)) { // <- response is undefined here

So, common parameters should be added to parameters as well, and if common parameters are defined it should be skipped when trying to process methods (operations).

Hi @mfodor, your link refers to OpenAPI 3, Swaxios currently supports only OpenAPI 2.

We should maybe throw an error if a user tries to parse an OpenAPI 3 document.

edit: Created #242 for that.

Ah, I see. Thank you for pointing out.

🤦 I just noticed that common parameters are also available in OpenAPI 2, see https://swagger.io/docs/specification/2-0/describing-parameters/.