ErikWittern / openapi-snippet

Generates code snippets for given Swagger / Open API documents
MIT License
115 stars 66 forks source link

fix: default missing type to any type #90

Open michaelgwelch opened 2 years ago

michaelgwelch commented 2 years ago

Primarily this is to fix the TypeError that is thrown if a parameter is missing any type information. Such an error makes it hard to debug your spec:

/openapi-snippet/openapi-to-har.js:441
 'SOME_' + (param.type || param.schema.type).toUpperCase() + '_VALUE';
                                             ^
TypeError: Cannot read properties of undefined (reading 'type')
    at getParameterValues (/Users/cwelchmi/emu/metasys-rest-api/node_modules/openapi-snippet/openapi-to-har.js:441:43)

Per the spec if no type is given then the type defaults to the Any Type: https://swagger.io/docs/specification/data-models/data-types/#any

This commit removes a check that explicitly sets any undefined type to object, instead leaving it blank. Then where type checking is done it adds support for missing types.

One existing test was expecting SOME_OBJECT_TYPE because of a missing type, this test was corrected to look for SOME_ANY_TYPE. An additional test was added to explicitly state that missing types are to be considered Any Type.

In addition a fix was made that if the value of a type field is not a string, then the parameter value SOME_ERROR_TYPE is generated. This allows the snippet generator to run without error, but presumably in some workflow the openapi validator will catch the spec error.

Fixes #89

Note While I cannot find an explicit statement about Any Type in the OpenApi spec, any openapi validators I used (like SwaggerHub) act as if the type is Any Type and does not act as if the type is "object". This is likely because in the absence of a constraint in OpenAPI spec, no constraint is assumed.

michaelgwelch commented 1 year ago

@ErikWittern are you still supporting this project?