OpenAPITools / OpenAPITools/openapi-generator

[BUG][Python] Reference to schema property produces undefined types

Open
#16,951 9 comments 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

Given Robocorp API the generated response models contain undefined types coming from pagination_details.PaginationDetails like HasMore and Next.
E.g. list_processes200_response.py:

class ListProcesses200Response(BaseModel):
    """
    ListProcesses200Response
    """
    next: Next = Field(...)
    has_more: HasMore = Field(...)
    data: conlist(ListProcesses200ResponseDataInner) = Field(...)
    __properties = ["next", "has_more", "data"]

where there's no resource created for these Next and HasMore (non existing model classes), and the only reference of them I found in the pagination_details.py model as attributes.

openapi-generator version

7.0.1, 8.0.0-SNAPSHOT, 7.1.0-SNAPSHOT

OpenAPI declaration file content or url

--> https://robocorp.com/api/openapi.json

Generation Details
% openapi-generator-cli generate -g python -i https://robocorp.com/api/openapi.json -c openapiconf.yaml --skip-validate-spec -o src -t templates

openapiconf.yaml:

generateSourceCodeOnly: true
packageName: robocorp.workspace
packageUrl: https://github.com/robocorp/robo/
packageVersion: 0.1.0
projectName: robocorp-workspace

Generated source.

Steps to reproduce

Just run the command above and look into the models using these pagination properties: HasMore, Next.

Related issues/PRs

Couldn't find one.

Suggest a fix

The problem can be solved in multiple ways:

  • A. The codegen detects that these properties are coming from a shared resource and the models including them (has_more, next) should include and use the parent resource actually, through pagination_details attribute taking pagination_details.PaginationDetails as type. (maybe it works out of the box if the OpenAPI schema will return such a resource instead of the props alone)
  • B. The codegen generates such individual models for properties from PaginationDetails like HasMore and Next so these can be imported and used by the models as they are.
  • C. The codegen will simply detect references to schema properties and will just use primitive types as it should. (see structure below)

Property references from a common shared schema:

"properties": {
                                        "next": {
                                            "$ref": "#/components/schemas/paginationDetails/properties/next"
                                        },
                                        "has_more": {
                                            "$ref": "#/components/schemas/paginationDetails/properties/has_more"
                                        },
                                        "data": {
                                            "type": "array",
                                            "items": {
                                                "$ref": "#/components/schemas/WorkerGroupResource"
                                            }
                                        }
                                    }

The schema:

"paginationDetails": {
                "type": "object",
                "required": [
                    "next",
                    "has_more"
                ],
                "properties": {
                    "next": {
                        "type": "string",
                        "description": "The full URL to access the next set of results. Null if there are no next set of results.",
                        "nullable": true,
                        "format": "url"
                    },
                    "has_more": {
                        "type": "boolean",
                        "description": "Whether or not there are more elements available after this set. If false, this set comprises the end of the list."
                    }
                }
            }

Expecting to get something like this in every model referencing them:

class PaginationDetails(BaseModel):
    """
    PaginationDetails
    """
    next: Optional[StrictStr] = Field(description="The full URL to access the next set of results. Null if there are no next set of results.")
    has_more: StrictBool = Field(description="Whether or not there are more elements available after this set. If false, this set comprises the end of the list.")
    __properties: ClassVar[List[str]] = ["next", "has_more"]

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

Reproduce the issue with the provided openapi-generator-cli command and https://robocorp.com/api/openapi.json. Inspect the generated list_processes200_response.py and pagination_details.py models, focusing on references to paginationDetails/properties/next and has_more. Done means generated Python models use the referenced primitive types instead of undefined Next and HasMore classes.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.