OpenAPITools / OpenAPITools/openapi-generator

[BUG][PYTHON] Generated models and optional parameters

Open
#15,567 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Client: Python 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

I am using the version 7.0.0 dated 2023/05/16 to generate Python code and the models do not take into account the readonly and optional flags from the spec file. As an example, my API includes a component called DataCollectionPlan, and a POST for the same component schema.

In order to use the example code, I must declare the Python object with all the optional and readonly parameters, such as id or description. The id is not necessary when using POST, while the optionals can be set to None, but this is not the correct way to treat optional object parameters.

openapi-generator version

7.0.0 dated 2023/05/16

OpenAPI declaration file content or url

The YAML section of the component.

    DataCollectionPlan:
      type: object
      properties:
        id:
          type: string
          readOnly: true
        name:
          type: string
        description:
          type: string
        kafkaTopic:
          type: string
          description:
        intervalInMinutes:
          type: number
        equipment:
          type: object
          properties:
            id: 
             type: string
        equipmentConnection:
          type: object
          properties:
            id: 
             type: string
        subscribers:
          type: array
          items:
            $ref: '#/components/schemas/Subscriber'
        requestList:
          type: array
          items:
            oneOf:
              - $ref: '#/components/schemas/AlarmRequest'
              - $ref: '#/components/schemas/EventRequest'
              - $ref: '#/components/schemas/TraceRequest'
        appKey:
          type: string
          readOnly: true
          description: UUID of the App which created this DCP.
      required:
        - id
        - name
        - equipment
        - equipmentConnection
        - requestList

The YAML section of the POST.

    post:
      summary: Create a Data Collection Plan
      description:       
      tags:
        - DataCollectionPlan
      parameters: 
      - in: header
        required: true
        name: X-App-Key
        schema:
          type: string
        description: App Key in UUID format
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/DataCollectionPlan'
Generation Details

I tried both with Java version 8 and using the Docker container from openapitools/openapi-generator-cli:latest.

Steps to reproduce

Just generated code as normal.

Related issues/PRs

None.

Suggest a fix

This was not an issue in the version 7.0.0 dated 2022/05/26. It seems like the model files had a different structure, and they took into account when model is used for POST or for results from PUT.

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 reproducing generation for the supplied DataCollectionPlan schema and POST declaration, then compare the generated Python model structure with the older 7.0.0 version dated 2022/05/26. Done means readOnly fields such as id and appKey, plus optional properties, are not required when constructing the model for the POST use case.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.