OpenAPITools / OpenAPITools/openapi-generator

[BUG] [Server: Python] Incorrect parameter example used for requestBody parameters

Open
#6,711 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug Server: Python
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
  • [ x ] Have you provided a full/minimal spec to reproduce the issue?
  • [ x ] Have you validated the input using an OpenAPI validator (example)?
  • [ x ] Have you search for related issues/PRs?
  • [ x ] What's the actual output vs expected output?
Description

As mentioned incorrect example taken for requestBody parameters when generated code by python-flask generator

openapi-generator version

v4.3.1

OpenAPI declaration file content or url
---
openapi: 3.0.2
info:
  title: OpenApiGeneratorTest
  version: 1.0.0
paths:
  /example:
    post:
      requestBody:
        content:
          application/json:
            schema:
              type: array
              items:
                $ref: '#/components/schemas/ExampleComplexType'
            examples:
              Random request data:
                value:
                - id: 19
                  text: some text
                - id: 63
                  text: some text
      responses:
        "200":
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/ExampleComplexType'
              examples:
                Random results:
                  value:
                  - id: 50
                    text: some text
                  - id: 3
                    text: some text
          description: Responce description text
components:
  schemas:
    ExampleComplexType:
      title: Root Type for ExampleComplexType
      description: ""
      type: object
      properties:
        id:
          format: int32
          type: integer
        text:
          type: string
      example:
        id: 0
        text: Some text

expected: python test must contain example like:

[{"id":19, "text":"some text"},{"id":63, "text":"some text"}]

Actual

{"id":0, "text":"some text"}
Command line used for generation

openapi-generator generate -g python-flask -i OpenApiGeneratorTest.yaml

Steps to reproduce
  1. Generate python-flask code using given commandline
  2. Check test_default_controller.py for example used in test_example_post test
  3. Rut tox - it will be failed with message
E   AssertionError: 400 != 200 : Response body is : {
E     "detail": "{'id': 0, 'text': 'Some text'} is not of type 'array'",
E     "status": 400,
E     "title": "Bad Request",
E     "type": "about:blank"
E   }
Related issues/PRs

Not found

Suggest a fix

Issue is in /openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonAbstractConnexionServerCodegen.java:postProcessOperationsWithModels:

.....
operation.bodyParam.example = example.get("example");
.....

This call overwrite example already exists in body param and assign example from operation object

Possible solution: add check whether bodyParam.example not empty:

                    if (operation.bodyParam.example == null &&
                    		example.get("contentType") != null 
                    		&& example.get("contentType").equals("application/json")
                    		) {
                        operation.bodyParam.example = example.get("example");
                    }

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 in src/main/java/org/openapitools/codegen/languages/PythonAbstractConnexionServerCodegen.java, specifically postProcessOperationsWithModels, and inspect how the requestBody example is selected. Reproduce with the supplied command, then check test_default_controller.py and run tox; done means the generated test uses the requestBody array example and passes with a 200 response.

Written by the indexing model from the issue text.

Assessment

Tech stack
flask, java, openapi, python
Domain
backend-api-design, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.