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

Endpoints are not created for routes with Pydantic model default values #944

Open
sagiv-s opened this issue Jan 21, 2024 · 1 comment
Open

Comments

@sagiv-s
Copy link

sagiv-s commented Jan 21, 2024

Describe the bug
When attempting to generate the client for an endpoint containing a route with a default value represented by a Pydantic model, the client generation fails. Specifically, the issue arises when the default value is set as a Pydantic model, such as the example below:

from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()
class Cake(BaseModel):
    slices: int

@app.post("/eat_cake")
async def eat_cake(cake: Cake | None = Cake(slices=20)):
    return {"message": f"Eating {cake.slices} slices of cake!"}

The generated client results in the following error message:

Warning(s) encountered while generating. Client was generated, but some pieces may be missing
WARNING parsing POST /graph_study/{customer_name}/distinct_identities/distinct_to_base/eat_cake within default. Endpoint will not be generated.
Value {'slices': 20} is not valid, only None is allowed

This issue is caused by a condition not being met in the union.py file.

To Reproduce

  1. Define a FastAPI app with a route containing a Pydantic model with a default value.
  2. Attempt to generate the client using openapi-python-client version 0.17.2.

OpenAPI Spec File

{
  "openapi": "3.0.2",
  "info": {
    "title": "FastAPI",
    "version": "1.0"
  },
  "paths": {
    "/cake_eater/eat_cake": {
      "post": {
        "summary": "Eat Cake",
        "operationId": "eat_cake_cake_eater___eat_cake_post",
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "anyOf": [
                  {
                    "$ref": "#/components/schemas/Cake"
                  },
                  {
                    "type": "null"
                  }
                ],
                "title": "Cake",
                "default": {
                  "slices": 20
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Successful Response",
            "content": {
              "application/json": {
                "schema": {}
              }
            }
          },
          "422": {
            "description": "Validation Error",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/HTTPValidationError"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "Cake": {
        "properties": {
          "slices": {
            "type": "integer",
            "title": "Slices"
          }
        },
        "type": "object",
        "required": [
          "slices"
        ],
        "title": "Cake"
      },
      "HTTPValidationError": {
        "properties": {
          "detail": {
            "items": {
              "$ref": "#/components/schemas/ValidationError"
            },
            "type": "array",
            "title": "Detail"
          }
        },
        "type": "object",
        "title": "HTTPValidationError"
      },
      "ValidationError": {
        "properties": {
          "loc": {
            "items": {
              "anyOf": [
                {
                  "type": "string"
                },
                {
                  "type": "integer"
                }
              ]
            },
            "type": "array",
            "title": "Location"
          },
          "msg": {
            "type": "string",
            "title": "Message"
          },
          "type": {
            "type": "string",
            "title": "Error Type"
          }
        },
        "type": "object",
        "required": [
          "loc",
          "msg",
          "type"
        ],
        "title": "ValidationError"
      }
    }
  }
}

Desktop (please complete the following information):

  • OS: Windows 11
  • Python Version: 3.11.1
  • openapi-python-client version 0.17.2

Additional context

@vogre
Copy link

vogre commented Mar 24, 2024

If following the description on json-schema.org

The default keyword specifies a default value. This value is not used to fill in missing values during the validation process. Non-validation tools such as documentation generators or form generators may use this value to give hints to users about how to use a value. However, default is typically used to express that if a value is missing, then the value is semantically the same as if the value was present with the default value. The value of default should validate against the schema in which it resides, but that isn't required.

I think this would suggest a behavior of ignoring values that do not validate against the schema (treating it as missing instead).

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