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

SwaggerClient does not fail when openapi spec is malformed #2953

Open
davidnewcomb opened this issue May 13, 2023 · 1 comment
Open

SwaggerClient does not fail when openapi spec is malformed #2953

davidnewcomb opened this issue May 13, 2023 · 1 comment

Comments

@davidnewcomb
Copy link

davidnewcomb commented May 13, 2023

Q&A (please complete the following information)

  • OS: macOS
  • Environment: Firefox, Chrome (all)
  • Method of installation: yarn
  • Swagger-Client version: 3.9.17
  • Swagger/OpenAPI version: OpenAPI 3.0

Content & configuration

Swagger/OpenAPI definition. This is an example of the malformed document. It invalidated the whole client.

  /revalidate:
    get:
      tags:
      - rfc
      summary: ...
      description: ...
      operationId: reused_operation_id
      parameters:
      - name: secret
        in: query
        description: ...
        required: true
        schema:
          type: string
      parameters:  # <--- bad spec
      - name: url
        in: query
        description: URL to revalidate
        required: true
        schema:
          type: string
      responses:
        '200':
          description: successful operation
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/...'
      security:
      - rfc_xxx: []

Swagger-Client usage:

SwaggerClient({
  url: process.env.NEXT_PUBLIC_OPENAPI_YAML
})
.then(
    (client) => {
        console.log('Client' + JSON.stringify(client, null, 4))
        try {
           let res = client.apis.rfc.calendarlist(opts, {})
           console.log('calendarlist:res', res)
           return res
        } catch (err) {
            console.log('calendarlist:err', err)
        }
    }
    }
).then(
     (response) => {
       return response.body
     }
).catch(
     (err) => {
         return {error: true, message:'...', payload: err ...}
     }
)

Describe the bug you're encountering

When the yaml spec is incorrect/malformed/invalid (in my case I had 2 parameter sections by mistake), the client is created but is empty like this:

API-C:calendar:list:client generic.js:24:20
Client{
    "url": "http://127.0.0.1:3000/rfc-v1.yaml",
    "authorizations": {
        "rfc_xxxx": "--expired--"
    },
    "spec": {},
    "errors": [],
    "apis": {}
}

So when I call client.apis.rfc.calendarlist(opts, {}) I get TypeError: client.apis.rfc is undefined.

To reproduce...

Steps to reproduce the behavior:

  1. Make your yaml file invalid

Expected behavior

If the yaml file could not be parsed correctly then SwaggerClient should never have entered the first then providing a broken (client), it should throw so it can be handled/logged in the catch.

I expect that the .then((client) =>{}) should not be called. The .catch((err)=>{}) should be called instead with a message about why the client could not be created.

Screenshots

image

Additional context or thoughts

If the client is not perfectly created then it should fail.

Additional information: if the (client) => {throw new Error('help!')} or TypeError from the failed client then .catch(err => { err is {} } which is unhelpful. The exception/error is consumed.

My workaround is:

    .then(
        (client) => {
            // https://github.com/swagger-api/swagger-js/issues/2953
            try {
                return clientCall(client)
            } catch (err) {
                console.error(err)
                throw err // exception is consumed but don't know what else to do with it!
            }
        }
    ).then(
        (response) => {
         /// normal stuff
        }        
    ).catch(
        (err) => {
            console.log('API-E:' + debug + ':error:  ' + JSON.stringify(err, null, 4)) // err is always {}
            
            if (err.response === undefined) {
                return { error: true, type: 'CLIENT', message: 'Client side error', payload: null }
            }
        }
    )

As you can see, there is a lot of stuff that shouldn't really be there and it has a bad code smell.

@stockiNail
Copy link

The same issue. Seems the client is ignoring if the OAS is malformed or invalid.

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