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

Getting a type error when using an OpenAPI spec with a header that's missing schema.type #430

Closed
benlei opened this issue Feb 14, 2023 · 8 comments
Labels
bug Something isn't working

Comments

@benlei
Copy link

benlei commented Feb 14, 2023

Describe the bug

I would get an error with the following message:

Cannot read properties of undefined (reading 'type') 

Here's how a part of my OpenAPI spec looks like:

          responses:
            '201':
              description: The resource was created successfully.
              headers:
                location:
                  description: URL of the created resource

With other generators it seems to be less strict.

Expected behavior

It'd be nice if there was a way to be less strict / default to string.

Current behavior

When running yarn docusaurus gen-api-docs all you get the following:

Cannot read properties of undefined (reading 'type') 

Possible solution

Make it less strict, or default to a string type if type property is undefined.

Context

Currently the Swagger docs for some projects are kind of hand written and aren't perfect - but the Swagger UI page for those projects still render just fine. It would be nice if the strictness could be toned down / default to string if type isn't present.

Your Environment

  • Version used: v1.5.2
@benlei benlei added the bug Something isn't working label Feb 14, 2023
@welcome-to-palo-alto-networks

🎉 Thanks for opening your first issue here! Welcome to the community!

@sserrata
Copy link
Member

Hi @benlei, can you share a link to of copy of the full OpenAPI spec?

@benlei
Copy link
Author

benlei commented Feb 16, 2023

@sserrata Sure I created a repo: https://github.com/benlei/docusaurus-openapi-docs-failed-to-gen

Specifically added this from the template repo: https://github.com/benlei/docusaurus-openapi-docs-failed-to-gen/blob/master/examples/petstore.yaml#L857

And when I run yarn docusaurus gen-api-docs all I get:

[ERROR] TypeError: Cannot read properties of undefined (reading 'type')
    at /Users/blei/WebstormProjects/my-website-docu/node_modules/docusaurus-plugin-openapi-docs/lib/markdown/createStatusCodes.js:69:48
    at Array.map (<anonymous>)
    at /Users/blei/WebstormProjects/my-website-docu/node_modules/docusaurus-plugin-openapi-docs/lib/markdown/createStatusCodes.js:68:45
    at guard (/Users/blei/WebstormProjects/my-website-docu/node_modules/docusaurus-plugin-openapi-docs/lib/markdown/utils.js:21:26)
    at createResponseHeaders (/Users/blei/WebstormProjects/my-website-docu/node_modules/docusaurus-plugin-openapi-docs/lib/markdown/createStatusCodes.js:65:30)
    at /Users/blei/WebstormProjects/my-website-docu/node_modules/docusaurus-plugin-openapi-docs/lib/markdown/createStatusCodes.js:284:41
    at Array.map (<anonymous>)
    at createStatusCodes (/Users/blei/WebstormProjects/my-website-docu/node_modules/docusaurus-plugin-openapi-docs/lib/markdown/createStatusCodes.js:262:33)
    at createApiPageMD (/Users/blei/WebstormProjects/my-website-docu/node_modules/docusaurus-plugin-openapi-docs/lib/markdown/index.js:44:51)
    at /Users/blei/WebstormProjects/my-website-docu/node_modules/docusaurus-plugin-openapi-docs/lib/index.js:195:54

@sserrata
Copy link
Member

For reference, it appears Redoc defaults to "any" when no schema is defined:

Screenshot 2023-02-17 at 10 11 06 AM

@sserrata
Copy link
Member

Also note that spectral and other linters flag the missing schema or content definitions:

Screenshot 2023-02-17 at 10 29 10 AM

Since the missing schema in your example appears to violate the OpenAPI specification it's debatable as to whether it should be supported in our plugin.

@benlei
Copy link
Author

benlei commented Feb 18, 2023

I probably should setup a linter some time later... but besides that, It would be convenient for it to fallback, or at the very least have a more descriptive message about what what field/line/etc has the issue so that it can be quickly addressed.

@sserrata
Copy link
Member

Hi @benlei, I agree the logging could be improved. It's on our roadmap to improve logging/error-handling.

I did a bit more research and came across this thread: OAI/OpenAPI-Specification#1657

After skimming through some of the responses/opinions, I think there's a strong case/consensus around interpreting no schema.type as "any" - or, basically leaving it up to the tooling to decide how to handle those cases.

@sserrata
Copy link
Member

Available to test in 0.0.0-569

Aniruddh25 pushed a commit to Azure/data-api-builder that referenced this issue Jul 3, 2024
…fix Infragistics AppBuilder tooling (#2283)

## Why make this change?

- Closes #2212 which describes how the missing `"type":"object"`
key/value pair on the response child object schema breaks certain client
tooling. in this case: Infragistics AppBuilder.

### Background

I found a relevant thread that discusses whether type is a required
property. Consensus is that type isn't required:

OAI/OpenAPI-Specification#1657
OpenAPI-Specification discussion
PaloAltoNetworks/docusaurus-openapi-docs#430
Example of how different tooling handles type or missing type
differently.
Ultimately, different tooling handles the presence of the type property
differently. Some may try to guess the type when not present:

The fact that the type isn't required means that
https://github.com/microsoft/OpenAPI.NET didn't complain about missing
type. An error if type were required would have helped prevent this
becoming an issue in the first place.

## What is this change?

- Adds `"type": "object"` to the openapi document for describing the
response schema:
```json
                "responses": {
                    "200": {
                        "description": "OK",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "type": "object", // <--- This property/value
                                    "properties": {
                                        "value": {
                                            "type": "array",
                                            "items": {
                                                "$ref": "#/components/schemas/Book"
                                            }
                                        },
                                        "nextLink": {
                                            "type": "string"
                                        }
                                    }
                                }
                            }
                        }
                    }
```

## How was this tested?

- [x] Integration Tests
- [ ] Unit Tests

## Sample Request(s)

View generated schema at
```https
GET localhost:5001/api/openapi
```

Co-authored-by: Abhishek  Kumar <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants