question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG] Body() descriptions cause generated OpenAPI to use `allOf`instead of schema

See original GitHub issue

Describe the bug When defining a route with a Body parameter, if a description is added to the Body, FastAPI generates an allOf field in the requestBody schema. This breaks OpenAPI Generator, and according to the spec, allOf should be used for object composition.

To Reproduce Create a route with a Body parameter ie:

@route.patch("/{request_id}")
def update_request(request_id: UUID, update:RequestModel = Body(..., description="Blah blah")
     return "stuff"

OpenAPI Result:

        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "title": "Update",
                "allOf": [
                  {
                    "$ref": "#/components/schemas/RequestModel"
                  }
                ],
                "description": ""Blah blah"
              }
            }
          },
          "required": true
        },

Removing the description generates the requestBody without the allOf (and without the description).

Expected behavior

        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/RequestModel"
               "description": "Blah blah"
              }
            }
          }

**Environment:**
 - OS: macOS
 - FastAPI Version 0.42.0
 - Python 3.7.4

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
reynoldsdjcommented, Feb 24, 2020

It’s part of the OpenAPI spec. When using $ref, the object containing it cannot contain any additional properties. So the only way to have a $ref and a description is through allOf.

This object cannot be extended with additional properties and any properties added SHALL be ignored.

https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#fixed-fields-19

Thank you for the response. I missed that in the OpenAPI specs.

1reaction
tiangolocommented, Feb 12, 2020

It’s part of the OpenAPI spec. When using $ref, the object containing it cannot contain any additional properties. So the only way to have a $ref and a description is through allOf.

This object cannot be extended with additional properties and any properties added SHALL be ignored.

https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#fixed-fields-19

Read more comments on GitHub >

github_iconTop Results From Across the Web

OpenAPI Specification - IntelliJ IDEA Documentation - JetBrains
An OpenAPI Specification (OAS) is a description format for REST APIs. Swagger is a set of tools based on this specification for writing,...
Read more >
ng-openapi-gen - npm
This project is a NPM module that generates model interfaces and web service clients from an OpenApi 3 specification. The generated classes ...
Read more >
SwaggerUI is defaulting to version 2 when a Springboot ...
java -jar openapi-generator-cli.jar generate -i stack.yml -g spring -p java8=true. I created the project and the Error class is showing up:
Read more >
OpenAPI CodeGen Extensions - APIMatic Documentation
How to use APIMatic Code Generation extensions for OpenAPI/Swagger to customize SDKs generated using APIMatic.
Read more >
Using the OpenAPI Generator for Spring Boot - mimacom blog
In this blog post, I would like to have a closer look at the basic project setup for generating code from the API...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found