Code Monkey home page Code Monkey logo

Comments (7)

tiangolo avatar tiangolo commented on April 28, 2024 2

Sorry for the delay.

In fact, some of those types are generating errors.

I'll check this more thoroughly as it seems there's a bug in Pydantic to solve (which I probably introduced there while implementing all the JSON Schema stuff).

If you are curious, I create the API with your code, then I check the raw JSON OpenAPI endpoint, then I copy it and paste it here: https://editor.swagger.io, there I can debug it and see all the errors. You can check that if you want to see what is the actual problem in the generated schemas.

from fastapi.

euri10 avatar euri10 commented on April 28, 2024 1

I've pushed on my fork a branch that tries to help on this (https://github.com/euri10/fastapi/tree/display_pydantic_types)

I used 2 models, Working and Failing:

  • Working contains all types that are in pydantic documentation, types which fail are commented out
  • Failing contains all failing types commented above

the test_openapi doesn't test anything, it's more to print the json schema to paste like you said on https://editor.swagger.io

I guess most errors come from upstream in pydantic schema generation, just hope this branch helps in figuring out what types may cause issues, pydantic schema is a rather impressive topic to jump in 💥 🙀 , not sure I would not where to look at ! 🐙

from fastapi.

fgimian avatar fgimian commented on April 28, 2024 1

After a little more digging, I can see that FastAPI is doing the right thing here (the OpenAPI JSOn generated is correct). The problem seems to be on swagger-ui's end. Some related issues are swagger-api/swagger-ui#4819 and swagger-api/swagger-ui#3803

I guess we'll need to wait for this to be fixed upstream 😄

Cheers
Fotis

from fastapi.

tiangolo avatar tiangolo commented on April 28, 2024

Yeah, Pydantic internals are quite heavy. It even uses undocumented Python features.

I'll check it there with your examples to see where I did something wrong.

from fastapi.

haizaar avatar haizaar commented on April 28, 2024

I hit this as well, both for Unions and Enum.

Here is the simple reproduction for Enum:

from enum import Enum

from fastapi import FastAPI
from pydantic import BaseModel


class ModelName(Enum):
    alexnet = "alexnet"
    resnet = "resnet"
    lenet = "lenet"


class Model(BaseModel):
    size: int
    name: ModelName


app = FastAPI()


@app.post("/")
def post(model: Model):
    pass

And the docs miss the name field indeed:
image

Workaround for Enum - inherit it from str as well:

class ModelName(str, Enum):
    alexnet = "alexnet"
    resnet = "resnet"
    lenet = "lenet"

(This is how pydantic docs do it BTW). That immediately made me think that there is a problem with schema, but it's not - Model.schema_json() return the following:

{
  "title": "Model",
  "type": "object",
  "properties": {
    "size": {
      "title": "Size",
      "type": "integer"
    },
    "name": {
      "title": "Name",
      "enum": [
        "alexnet",
        "resnet",
        "lenet"
      ]
    }
  },
  "required": [
    "size",
    "name"
  ]
}

from fastapi.

tiangolo avatar tiangolo commented on April 28, 2024

I just realized I replied in the PR but not here.

Let me copy some sections from there:

The way to use an enum would be like:

class MyEnum(Enum):
    a = "foo"
    b = "bar"

and then:

my_enum: MyEnum

@haizaar about enums with str, good point. That's an issue in Swagger UI itself: swagger-api/swagger-ui#3761

But is good to know there's a workaround for the API docs. Maybe we could use it in the FastAPI docs examples.


On the other side, about Union, they show up in Swagger UI in the schema, but not in the example, that's also Swagger UI: swagger-api/swagger-ui#3803

from fastapi.

fgimian avatar fgimian commented on April 28, 2024

I just encountered this one today too with a Union 😄

Are there any workarounds for getting a Union[str, List[str]] to display in the docs?

Thanks so much
Fotis

from fastapi.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.