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] docs don't show nested enum attribute for body

See original GitHub issue

Describe the bug I have a post method with the body as pydantic object, with one attribute as Enum object; All validation works perfectly; However, two independent (?) issues occur:

  1. complaint.dict() generates a dictionary - but does not flatten nested enum object into string. I am getting this is beyound the idea of dict(), but would be nice to have something that flattens the whole entity;
  2. complaint_type attribute does not show up on the swagger

To Reproduce example coe snippet:

class ComplaintType(Enum):
    other = 'other'
    commercial = "commercial"
    house = "house"
    park = "park"
    residential = "residential"
    street = "street"
    vehicle = "vehicle"


class Complaint(BaseModel):
    complaint_type:ComplaintType
    timestamp:datetime = datetime.now()
    lat:float
    lon:float
    description:str

Expected behavior I expected

  1. complaint_type to be on a swagger and in the schema as (I guess) “oneOf” element
  2. be able to flatten Pydantic object into a fully json-able dictionary (converting enum objects into strings)

Environment:

  • OS: macOS
  • FastAPI Version 0.30.0
  • Python version 3,7.3

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
Sieboldianuscommented, Jun 23, 2019

Can you test whether this fixes it:

class Geoaccuracy(str, Enum):
    unknown = "unknown"
    latlng = "latlng"
    place = "place"
    city = "city"
    country = "country"

This works for me and correctly shows in SwaggerDocs. In your case, you’ve specified type Enum but not which type for Enum (looks like its str, too).

1reaction
Casyfillcommented, Jul 3, 2019

Thank you both! That makes a lot of sense - somehow I never used Enum before, so - good to know!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Nested enum is static? - java - Stack Overflow
Your enum declaration is not static, it is top-level, and top-level classes cannot be static. Constant bodies define an implicit nested anonymous subclass...
Read more >
Enums - Swagger
Enums. You can use the enum keyword to specify possible values of a request parameter or a model property. For example, the sort...
Read more >
Enums - C# language specification - Microsoft Learn
An enum declaration cannot include a type parameter list, but any enum nested inside a generic class declaration or a generic struct ...
Read more >
Handbook - Enums - TypeScript
In this generated code, an enum is compiled into an object that stores both forward ( name -> value ) and reverse (...
Read more >
Defining an Enum - The Rust Programming Language - MIT
That property of IP addresses makes the enum data structure appropriate for this case, because enum values can only be one of the...
Read more >

github_iconTop Related Medium Post

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