Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
4 of 5 tasks
pawel-kow opened this issue Jul 22, 2024 · 0 comments
Open
4 of 5 tasks

Comments

@pawel-kow
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant