Skip to content

Enums with default values generate incorrect field type and to_dict implementation #373

Closed
@joshzana

Description

@joshzana

Describe the bug
We have several Enums in our API spec, and models that include fields of that enum type. Sometimes, we set default values on those enum fields. In those cases, the generated code in to_dict is incorrect and cannot be used to POST entities to our API.

To Reproduce
Steps to reproduce the behavior:
Add this code to a main.py file

from typing import Optional, Union, Dict
from fastapi import FastAPI
from pydantic import BaseModel
from enum import Enum

from starlette.responses import Response

app = FastAPI()

class ItemType(str, Enum):
    RATIO = "ratio"
    CURRENCY = "currency"

class ItemResource(BaseModel):
    id: int
    optional_id: Optional[int]
    id_with_default: int = 42

    item_type: ItemType
    optional_item_type: Optional[ItemType]
    item_type_with_default: ItemType = ItemType.RATIO

@app.post("/")
def write_item(model: ItemResource):
    return Response(status_code=201)

Run the code with uvicorn main:app &
Generate an sdk with openapi-python-client generate --url http://localhost:8000/openapi.json
Open the generated item_resource.py

Expected behavior
item_type_with_default should look like:
item_type_with_default: Union[Unset, ItemType] = ItemType.RATIO

to_dict should have code like:

item_type_with_default = self.item_type_with_default
if item_type_with_default is not UNSET:
    field_dict["item_type_with_default"] = item_type_with_default

Actual behavior
item_type_with_default looks like:
item_type_with_default: Union[Unset, None] = UNSET

to_dict has code like:

item_type_with_default = None
if item_type_with_default is not UNSET:
    field_dict["item_type_with_default"] = item_type_with_default

Since self.item_type_with_default is never accessed, the caller has no way to actually provide a value to the server.
You can see the code in there for id_with_default, an int field with default that does the right thing.

OpenAPI Spec File

{"openapi":"3.0.2","info":{"title":"FastAPI","version":"0.1.0"},"paths":{"/":{"post":{"summary":"Write Item","operationId":"write_item__post","requestBody":{"content":{"application/json":{"schema":{"$ref":"#/components/schemas/ItemResource"}}},"required":true},"responses":{"200":{"description":"Successful Response","content":{"application/json":{"schema":{}}}},"422":{"description":"Validation Error","content":{"application/json":{"schema":{"$ref":"#/components/schemas/HTTPValidationError"}}}}}}}},"components":{"schemas":{"HTTPValidationError":{"title":"HTTPValidationError","type":"object","properties":{"detail":{"title":"Detail","type":"array","items":{"$ref":"#/components/schemas/ValidationError"}}}},"ItemResource":{"title":"ItemResource","required":["id","item_type"],"type":"object","properties":{"id":{"title":"Id","type":"integer"},"optional_id":{"title":"Optional Id","type":"integer"},"id_with_default":{"title":"Id With Default","type":"integer","default":42},"item_type":{"$ref":"#/components/schemas/ItemType"},"optional_item_type":{"$ref":"#/components/schemas/ItemType"},"item_type_with_default":{"allOf":[{"$ref":"#/components/schemas/ItemType"}],"default":"ratio"}}},"ItemType":{"title":"ItemType","enum":["ratio","currency"],"type":"string","description":"An enumeration."},"ValidationError":{"title":"ValidationError","required":["loc","msg","type"],"type":"object","properties":{"loc":{"title":"Location","type":"array","items":{"type":"string"}},"msg":{"title":"Message","type":"string"},"type":{"title":"Error Type","type":"string"}}}}}}

Desktop:

  • OS: macOS 11.2.2
  • Python Version: 3.8.6
  • openapi-python-client version 0.8.0
  • fast-api version 0.62.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    🐞bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions