[BUG] [Server: Python] Incorrect parameter example used for requestBody parameters
Created by: pavlot
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
- Generate python-flask code using given commandline
- Check test_default_controller.py for example used in
test_example_post
test - 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");
}