OpenAPITools / OpenAPITools/openapi-generator

[BUG] [Python] model_validate infine nested loop

Open
#20,664 1 comment 0 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 validating a model that references other models, we can run into cases where it will never validate properly. This is because it could, in theory, go in an infinite nested loop. Of course, API servers do not return full objects in these cases.

For example, take the bellow model. What my API server does is return the "id" for the parent, otherwise it would go in an infinite nested loop. But how do I get around this with the validation, which will expect a correct and complete parent object?

openapi: 3.0.0
info:
  title: Nested Model Example
  version: 1.0.0

paths:
  /node:
    get:
      summary: Get a node with a reference
      responses:
        '200':
          description: A node object
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Node'

components:
  schemas:
    Node:
      type: object
      properties:
        id:
          type: string
        name:
          type: string
        child:
          $ref: '#/components/schemas/ChildNode'  # References another schema

    ChildNode:
      type: object
      properties:
        id:
          type: string
        value:
          type: string
        parent:
          $ref: '#/components/schemas/Node'  # References parent Node

examples:
  NodeExample:
    value:
      id: "550e8400-e29b-41d4-a716-446655440000"
      name: "Root Node"
      child:
        id: "660e8400-e29b-41d4-a716-446655440001"
        value: "Child Node Value"
        parent:
          id: "550e8400-e29b-41d4-a716-446655440000"

openapi-generator version

7.11.0

Steps to reproduce

Reproduce with any nested models. If needed, I can create an example code snippet for API server and client.

Related issues/PRs
Suggest a fix

Validation could be more lenient and accept that nested models cannot run for ever. For example, there could be a validation flag to accept that only "id" being returned is an acceptable case.

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 with the generated Python model_validate entry point and reproduce the issue using the supplied Node and ChildNode schemas, where each model references the other. Compare the returned partial parent object with the validation behavior, then define and verify a bounded or explicitly configurable outcome for this recursive case.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
api
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.