Skip to content

anyOf of required-only members in a request body generates unparsable code ("ruff failed") #1520

Open

Description

@alanwilter

Describe the bug

A request body whose object schema has properties plus an anyOf of members that carry only required (the usual JSON Schema way to say "at least one of these two fields") makes the generator write an endpoint module that is not valid Python. _get_kwargs gets body: Any | Unset, the if branch that serialises the body is missing, and an orphan else: is left behind. The run then ends with ruff failed, so the cause looks like a formatting problem.

I would expect either a working client (for instance the object model with both fields optional) or a clear error or warning naming the schema, as for other unsupported constructs.

OpenAPI Spec File

openapi: 3.0.1
info: {title: repro, version: "1"}
paths:
  /run:
    post:
      requestBody:
        content:
          application/json:
            schema:
              type: object
              properties:
                a: {type: integer}
                b: {type: string}
              anyOf:
              - required: [a]
              - required: [b]
      responses:
        '200': {description: ok}

To reproduce

openapi-python-client generate --path repro.yaml --meta none --output-path out
Error(s) encountered while generating, client was not created
Generating out
ruff failed

error: Failed to parse api/default/post_run.py:34:5: Expected a statement

out/api/default/post_run.py, lines 15 to 38:

def _get_kwargs(
    *,
    body: Any | Unset = UNSET,

) -> dict[str, Any]:
    headers: dict[str, Any] = {}


    

    

    _kwargs: dict[str, Any] = {
        "method": "post",
        "url": "/run",
    }

    
    else:
        _kwargs["json"] = body

    headers["Content-Type"] = "application/json"

    _kwargs["headers"] = headers
    return _kwargs

Removing the anyOf generates a normal body model. An anyOf of two complete object schemas (each with its own type, properties and required) also works and gives a union of two models.

Desktop

  • OS: Ubuntu 24.04
  • Python: 3.12
  • openapi-python-client: 0.30.0

Activity

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