OpenAPITools / OpenAPITools/openapi-generator

[BUG][PYTHON] Cannot generate required but nullable object in 7.3.0

Open
#17,940 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

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

In the OpenApi, propertyA is both required and Nullable, in 7.2.0 this was declared as Optional

class ObjectDto(BaseModel):
    """
    ObjectDto
    """ # noqa: E501
    property_a: Optional[SubObject] = Field(alias="propertyA")

but in 7.3.0 the Optional disappeared

class ObjectDto(BaseModel):
    """
    ObjectDto
    """ # noqa: E501
    property_a: ObjectDtoPropertyA = Field(alias="propertyA")

Which means from_dict is failing despite null being valid

        _obj = cls.model_validate({
            "propertyA": ObjectDtoPropertyA.from_dict(obj["propertyA"]) if obj.get("propertyA") is not None else None
        })
openapi-generator version

Regression between 7.2.0 and 7.3.0

OpenAPI declaration file content or url
openapi: 3.1.0
info:
  title: API
  version: '0.1'

paths:
  /api/postA:
    post:
      responses:
        '200':
          description: success
          content:
            application/json:
              schema:
                allOf:
                  - $ref: '#/components/schemas/ObjectDto'
components:
  schemas:
    ObjectDto:
      properties:
        propertyA:
          anyOf:
            - $ref: '#/components/schemas/SubObject'
            - type: 'null'
      type: object
      title: ObjectDto
      required:
        - propertyA
    SubObject:
      type: string
Generation Details

Simple generation in 7.3.0 with -g python

Steps to reproduce
ObjectDto(property_a=None)

pydantic_core._pydantic_core.ValidationError: 1 validation error for ObjectDto
property_a
  Input should be a valid dictionary or instance of ObjectDtoPropertyA [type=model_type, input_value=None, input_type=NoneType]
    For further information visit https://errors.pydantic.dev/2.5/v/model_type

Related issues/PRs

Seems to be the same problem as #17938 in java

Suggest a fix

I see a lot of things were moved around so I am not sure about a fix

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

Start by generating the supplied OpenAPI 3.1 specification with the Python generator using 7.2.0 and 7.3.0, then compare the generated ObjectDto model and its from_dict behavior. The fix is complete when a required nullable property remains nullable and ObjectDto(property_a=None) validates successfully.

Written by the indexing model from the issue text.

Assessment

Tech stack
openapi, python
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 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.