swagger-api / swagger-api/swagger-codegen

[Python] Generating None values for optional pydantic fields according to return_type even when they are missing in the actual API response.

Open
#11,221 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Generated the client from: https://editor.swagger.io/#
API call return the Generated pydantic object with missing fields as Nones instead of not return them at all.
Basically this is because how the deserialization works:

Result before deserialization:

{
    "nextMarker": "123",
    "results": [{
            "id": "345",
            "key": "678",
            "created": "2021-05-24T07:39:53.245Z",
            "updated": "2021-07-08T11:20:17.768Z",
            "type": "type",
            "status": "status",
            "priority": "priority",
            "group": "group"
        }
    ]
}

Result after deserialization:

{
    'next_marker': '123',
    'results': [{
            'company': None,
            'created': datetime.datetime(2021, 5, 24, 7, 39, 53, 245000, tzinfo = tzlocal()),
            'description': None,
            'external_ids': None,
            'group': 'group',
            'id': '345',
            'key': '678',
            'name': None,
            'pending_for_group': None,
            'priority': 'priority',
            'redirection_reason': None,
            'status': 'status',
            'tags': None,
            'type': 'type',
            'updated': datetime.datetime(2021, 7, 8, 11, 20, 17, 768000, tzinfo = tzlocal())
        }
    ]
}

The pydantic object:

class Results(BaseModel):
    class Config:
        allow_population_by_field_name = True

    next_marker: Optional[str] = Field(
        None,
        alias="",
        description="description",
    )
    results: List["Res"]
    
 class Res(BaseModel):
    class Config:
        allow_population_by_field_name = True

    company: Optional[str] = Field(
        None,
        description="",
        example="",
    )
    created: datetime = Field(..., description="")
    description: Optional[str] = Field(
        None,
        description='',
        example="",
    )
    external_ids: Optional[List["ExId"]] = Field(
        None,
        alias="",
        description="",
    )
    group: Optional[str] = Field(
        None,
        description="",
        example="",
    )
    id: str = Field(
        ..., description=""
    )
    key: str = Field(
        ...,
        description="",
        example="",
    )
    name: Optional[str] = Field(
        None,
        description='',
        example="",
    )
    pending_for_group: Optional[str] = Field(
        None,
        alias="",
        description="",
    )
    priority: str = Field(
        ..., description="", example=""
    )
    redirection_reason: Optional[str] = Field(
        None,
        alias="",
        description="",
    )
    status: str = Field(..., description="", example="")
    tags: Optional[List[constr(regex=r"^\S*$")]] = Field(
        None,
        description="",
        example=[],
    )
    type: str = Field(..., description="", example="")
    updated: datetime = Field(
        ..., description=""
    )

Is there any way to not get the extra Generated pydantic fields (because they are not present in the actual API response)?
(They are marked as Optional in Type hints.)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No repository file or test is named. Reproduce the generated Python client from the Swagger Editor example, then inspect the Pydantic model generation and deserialization behavior for optional fields. Done means optional fields absent from the API response are not added as None, with the behavior verified against the shown response.

Written by the indexing model from the issue text.

Assessment

Tech stack
openapi, python
Domain
api
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.