Skip to content

How to export by Pydantic models by alias #1436

@tspanos

Description

@tspanos

Description

It seems that Starlite will return the non-alias field names for pydantic models in a response, but will generate the OpenAPI documentation based on the field aliases.

Take this code for example:

from humps import camelize
from pydantic import BaseModel
from starlite import Starlite, get

class User(BaseModel):
    user_id: str = "A1"
    user_name: str = "Starlite User"

    class Config:
        alias_generator = camelize
        allow_population_by_field_name = True

@get("/")
def get_user() -> User:
    return User()

app = Starlite(route_handlers=[get_user])

The expected response of the get_user route is:

{"userId":"A1","userName":"Starlite User"}

Instead, Starlite returns:

{"user_id":"A1","user_name":"Starlite User"}

The OpenAPI docs correctly interpret the alias.

      "User": {
        "properties": {
          "userId": {
            "type": "string",
            "title": "Userid",
            "default": "A1"
          },
          "userName": {
            "type": "string",
            "title": "Username",
            "default": "Starlite User"
          }
        },
        "type": "object",
        "title": "User"
      }

URL to code causing the issue

No response

MCVE

No response

Steps to reproduce

1. Go to '...'
2. Click on '....'
3. Scroll down to '....'
4. See error

Screenshots

No response

Logs

No response

Starlite Version

1.5.9

Platform

  • Linux
  • Mac
  • Windows
  • Other (Please specify in the description above)

Metadata

Metadata

Assignees

No one assigned

    Labels

    QuestionThis is a question and further information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions