OpenAPITools / OpenAPITools/openapi-generator

[BUG] [Python][client] nested oneOf in allOf generates all fields as required

Open
#19,213 0 comments 1 reaction 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)?
  • [s] 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

An object with nested oneOf in allOf structure generates a class with all properties required, even though the properties under oneOf are in fact optional.

From the example schema below the code:

req = openapi_client.Mergedrequest(
        prop1=10,
        prop2=20,
    )

generates following error:

    self.__pydantic_validator__.validate_python(data, self_instance=self)
pydantic_core._pydantic_core.ValidationError: 1 validation error for Mergedrequest
prop3
  Field required [type=missing, input_value={'prop1': 10, 'prop2': 20}, input_type=dict]
    For further information visit https://errors.pydantic.dev/2.8/v/missing
openapi-generator version

7.7.0

OpenAPI declaration file content or url
openapi: '3.0.3'
info:
  title: API Title
  version: '1.0'
servers:
  - url: http://localhost:8080/v1

paths:
  /test:
    post:
      operationId: 'createTestMe'
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/mergedrequest'
      responses:
        '200':
          description: OK
          content:
            text/plain:
              schema:
                $ref: '#/components/schemas/simpleresponse'

components:
  schemas:
    mergedrequest:
      allOf:
        - $ref: '#/components/schemas/simplerequest1'
        - oneOf:
            - $ref: '#/components/schemas/simplerequest2'
            - $ref: '#/components/schemas/simplerequest3'
    simplerequest1:
      type: object
      properties:
        prop1:
          type: integer
      required:
        - prop1
    simplerequest2:
      type: object
      properties:
        prop2:
          type: integer
      required:
        - prop2
    simplerequest3:
      type: object
      properties:
        prop3:
          type: integer
      required:
        - prop3
    simpleresponse:
      type: string
Generation Details

Just defaults:

openapi-generator generate -g python -i openapi.yaml -o ./client     
Steps to reproduce
  1. Generate client code from the schema
  2. Try creating Mergedrequest object with only prop1 and prop2 specified
Related issues/PRs

Not found

Suggest a fix

If strict implementation of oneOf for part of the properties may be too complex, maybe just marking them as optional would be enough. The schema validation on the server side will do the trick if someone would try to push prop2 and prop3 together.

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 running the generation command against the provided openapi.yaml, then inspect the generated Mergedrequest model and its handling of the nested oneOf inside allOf. Reproduce construction with only prop1 and prop2; done means the generated Python client no longer requires prop3 while retaining the schema's oneOf behavior.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.