OpenAPITools / OpenAPITools/openapi-generator

[BUG][Python Client] oneOf for different constrainted types fails to resolve with missing patterns

Open
#19,034 3 comments 2 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

when generating modles fo one-of/any-of of multiple string, a intermediate models for pydantic is generated,
which ignores patterns and passes/fails in a confusing manner

https://github.com/RonnyPfannschmidt-RedHat/openapi-codegen-show-issues/blob/9659d501374b493fe1dd43aa18c48ba2c3b924d0/oapi_codegen_broken_string/models/pass_string_mangled_string_any.py#L29-L115

https://github.com/RonnyPfannschmidt-RedHat/openapi-codegen-show-issues/blob/9659d501374b493fe1dd43aa18c48ba2c3b924d0/oapi_codegen_broken_string/models/pass_string_mangled_string_one.py#L27C1-L118C1

openapi-generator version

7.6.0

OpenAPI declaration file content or url

https://github.com/RonnyPfannschmidt-RedHat/openapi-codegen-show-issues/blob/9659d501374b493fe1dd43aa18c48ba2c3b924d0/openapi/openapi.BrokenString.yaml#L1-L42

openapi: 3.0.0
info:
  title: Broken Examples Home
  version: 0.0.0
tags: []
paths:
  /string-example:
    get:
      operationId: Mangled_get
      parameters: []
      responses:
        '200':
          description: The request has succeeded.
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/PassString'
components:
  schemas:
    HereBeDragons:
      type: string
      pattern: here
    PassString:
      type: object
      properties:
        mangled_string_one:
          oneOf:
            - $ref: '#/components/schemas/PettyNumber'
            - $ref: '#/components/schemas/HereBeDragons'
        mangled_string_any:
          anyOf:
            - $ref: '#/components/schemas/PettyNumber'
            - $ref: '#/components/schemas/HereBeDragons'
    PettyNumber:
      type: string
      pattern: '[0-9]{2,8}'
servers:
  - url: https://example.com
    description: The service endpoint
    variables: {}
Generation Details
npm run openapi-compile -- \
    -i openapi/openapi.BrokenString.yaml\
    --package-name=oapi_codegen_condused_values
Suggest a fix

a starting point would be to re-attach the patterns to the validation

optional strings with a string constraints should be used

additionally the nesting is quite a pain wrt choice of object models, i'd strongly prefer if no indirection models where introduced

https://github.com/RonnyPfannschmidt-RedHat/openapi-codegen-show-issues/blob/9659d501374b493fe1dd43aa18c48ba2c3b924d0/manual_pydantic_broken_string.py is a minimal version of using pydantic validators to pass over those values

my recommendation for openapi-codegen would be to create type aliases for constrainted strings
(using either type statements or the runtime backport for them

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

Run the npm openapi-compile command against openapi/openapi.BrokenString.yaml and compare the generated models linked in the report. Start with the oneOf and anyOf handling for PassString, PettyNumber, and HereBeDragons. Done means the generated Pydantic validation preserves both string patterns without confusing intermediary behavior, with the expected constrained-string model structure.

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
Quiet
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.