Skip to content

Python: make_instance generates syntax error for optional nullable fields with default null in nested objects #25047

Closed

Description

@sieira

Bug

When an optional nullable field with anyOf: [type: object, type: null] appears as the 4th field in an object, the Python test generator produces invalid syntax:

prop4 = ,

instead of:

prop4 = None,

Reproduction

Minimal OpenAPI spec:

openapi: 3.1.0
info:
  title: Minimal reproduction
  description: Minimal reproduction spec
  version: "1.0"
paths:
  /a:
    post:
      requestBody:
        content:
          application/json:
            schema:
              type: object
              properties:
                object1:
                  type: object
                  properties:
                    prop1:
                      type: object
                      properties:
                        prop: 
                          type: string
                    prop2:
                      type: object
                      properties:
                        prop:
                          type: string
                    prop3:
                      type: object
                      properties:
                        prop:
                          type: string
                    prop4:
                      anyOf:
                        - type: object
                          properties:
                            prop:
                              type: string
                        - type: null

Generate Python client: openapi-generator-cli generate -i spec.yaml -g python -o out

The generated test file contains prop4 = , -- a syntax error.

Activity

  1. reopened this on Sep 29, 2026
  2. sieira commented on Sep 29, 2026

    @sieira
    Author

    Updated finding: tested with 8 nullable anyOf fields (prop1–prop8). Bug only triggers on prop4 (the 4th nullable field), not on prop5–prop8. So it's specifically the 4th nullable anyOf field, not all of them.

  3. sieira commented on Sep 29, 2026

    @sieira
    Author

    Tested with 8 nullable anyOf fields (prop1–prop8 all nullable). Bug still only triggers on prop4 (the 4th field overall), not prop5–prop8. Confirmed it's the 4th field, not the 4th nullable.

  4. Mattias-Sehlstedt commented on Sep 29, 2026

    @Mattias-Sehlstedt
    Contributor

    Please provide more details about what generated model that contains the error so it is possible to fully understand the reported issue (e.g., provide the name of the generated file and a larger context that illustrates where the prop is initialized incorrectly).

    Please also state what version of the generator that you are using.

  5. added a commit that references this issue on Oct 4, 2026
    4882687
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions