OpenAPITools / OpenAPITools/openapi-generator

[BUG] [Python] Generator does not correctly generate examples

Open
#19,042 0 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

I want to improve the examples in the documentation of the generated Python client. When I include the example within the schema or not at all, following is generated:

data_experiment = eliona.api_client2.DataExperiment() # DataExperiment

While that's alright, I would like to have the instance initialized with example data as well (in reality that class has a lot of properties).

So I moved the example out of the schema a level up (see the openapi.yaml excerpt below), and following was generated:

data_experiment = {"assetId":4711} # DataExperiment

With that, I gained example data, but it is no longer a DataExperiment class.

Am I missing something, or is there something wrong with the generator?

openapi-generator version

7.3.0, 7.5.0, 7.6.0

OpenAPI declaration file content or url
requestBody:
    required: true
    content:
      application/json:
        schema:
          title: DataExperiment
          type: object
          description: Data for assets
          required:
            - assetId
          properties:
            assetId:
              type: integer
              description: ID of the corresponding asset
              example: 4711
        example:
          assetId: 4711
Generation Details
docker run --rm \
     -v "${PWD}:/local" \
    openapitools/openapi-generator-cli:v7.6.0 \
    generate \
    -g python \
    -o /local \
    ...
Steps to reproduce

Generate the client, see the docs.

Related issues/PRs

None found

Suggest a fix

Don't know.

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 provided Docker generation command with the OpenAPI YAML and compare the generated Python documentation for the schema-level and request-body examples. Trace the Python generator's example handling from the generate entry point; done means the generated example includes the sample data while remaining a DataExperiment instance.

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
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.