Skip to content

[BUG][PYTHON] Pydantic warning on serializing OneOf #17939

@BaptisteSaves

Description

@BaptisteSaves

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

When the spec use OneOf a class containing the different alternative is created.

This class is raising a Pydantic warning when calling model_dump. The warning is valid but the field is then overwritten which means the code should not raise it.

/venv/lib/python3.10/site-packages/pydantic/main.py:308: UserWarning: Pydantic serializer warnings:
  Expected `list[str]` but got `_LiteralGenericAlias` - serialized value may not be as expected
  return self.__pydantic_serializer__.to_python(

Example:

	_dict = self.model_dump(
            by_alias=True,
            exclude=excluded_fields,
            exclude_none=True,
        )
        # override the default output from pydantic by calling `to_dict()` of property_a
        if self.property_a:
            _dict['propertyA'] = self.property_a.to_dict()
        return _dict

This warning is really annoying because it will raise errors in tests and logs in production environment

openapi-generator version

7.2.0, 7.3.0, latest

OpenAPI declaration file content or url
openapi: 3.1.0
info:
  title: API
  version: '0.1'
  
paths:
  /api/postA:
    post:
      requestBody:
        required: true
        content:
          application/json:
            schema:
              allOf:
                - $ref: '#/components/schemas/ObjectDto'
      responses:
        '200':
          description: Successful Response

components:
  schemas:
    ObjectDto:
      properties:
        propertyA:
          anyOf:
            - type: string
            - type: 'null'

      type: object
      title: ObjectDto
Generation Details

Only setting use is -g python

Steps to reproduce

Generate python client from given OpenApi

print(ObjectDto(property_a=ObjectDtoPropertyA("test")).to_dict())

Result is:

/venv/lib/python3.10/site-packages/pydantic/main.py:308: UserWarning: Pydantic serializer warnings:
  Expected `list[str]` but got `_LiteralGenericAlias` - serialized value may not be as expected
  return self.__pydantic_serializer__.to_python(
{'propertyA': 'test'}
Related issues/PRs

Did not find any

Suggest a fix

In model_generic.mustache, the code (line 148) call model_dump, a parameter already exist to remove warning so we could set it

_dict = self.model_dump(
            by_alias=True,
            exclude=excluded_fields,
            exclude_none=True,
            warnings=False,
        )

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions