Skip to content

Commit 2aa4922

Browse files
authored
Bugfix: #1666: Change requestBody argument name to 'body' (#20207)
Since Connexion defaults to using 'body' as the argument name for the requestBody, the controller's argument name is adjusted accordingly.
1 parent 706c0a1 commit 2aa4922

File tree

6 files changed

+169
-8
lines changed

6 files changed

+169
-8
lines changed

modules/openapi-generator/src/main/resources/python-flask/controller.mustache

+4-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ from {{packageName}} import util
1010
{{#operation}}
1111

1212

13-
def {{operationId}}({{#allParams}}{{paramName}}{{^required}}=None{{/required}}{{^-last}}, {{/-last}}{{/allParams}}): # noqa: E501
13+
def {{operationId}}({{#allParams}}{{^isBodyParam}}{{paramName}}{{/isBodyParam}}{{#isBodyParam}}body{{/isBodyParam}}{{^required}}=None{{/required}}{{^-last}}, {{/-last}}{{/allParams}}): # noqa: E501
1414
"""{{summary}}{{^summary}}{{operationId}}{{/summary}}
1515

1616
{{notes}} # noqa: E501
@@ -60,6 +60,9 @@ def {{operationId}}({{#allParams}}{{paramName}}{{^required}}=None{{/required}}{{
6060
:rtype: Union[{{returnType}}{{^returnType}}None{{/returnType}}, Tuple[{{returnType}}{{^returnType}}None{{/returnType}}, int], Tuple[{{returnType}}{{^returnType}}None{{/returnType}}, int, Dict[str, str]]
6161
"""
6262
{{#allParams}}
63+
{{#isBodyParam}}
64+
{{paramName}} = body
65+
{{/isBodyParam}}
6366
{{^isContainer}}
6467
{{#isDate}}
6568
{{paramName}} = util.deserialize_date({{paramName}})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package org.openapitools.codegen.python;
2+
3+
import io.swagger.parser.OpenAPIParser;
4+
import io.swagger.v3.oas.models.OpenAPI;
5+
import io.swagger.v3.parser.core.models.ParseOptions;
6+
import org.openapitools.codegen.*;
7+
import org.openapitools.codegen.languages.PythonFlaskConnexionServerCodegen;
8+
import org.openapitools.codegen.languages.features.CXFServerFeatures;
9+
import org.testng.Assert;
10+
import org.testng.annotations.Test;
11+
12+
import java.io.File;
13+
import java.io.IOException;
14+
import java.nio.file.Files;
15+
import java.nio.file.Path;
16+
import java.nio.file.Paths;
17+
import java.util.List;
18+
19+
import static org.openapitools.codegen.TestUtils.assertFileContains;
20+
import static org.openapitools.codegen.TestUtils.assertFileExists;
21+
22+
public class PythonFlaskConnexionServerCodegenTest {
23+
24+
// Helper function, intended to reduce boilerplate
25+
static private String generateFiles(DefaultCodegen codegen, String filePath) throws IOException {
26+
final File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
27+
output.deleteOnExit();
28+
final String outputPath = output.getAbsolutePath().replace('\\', '/');
29+
30+
codegen.setOutputDir(output.getAbsolutePath());
31+
codegen.additionalProperties().put(CXFServerFeatures.LOAD_TEST_DATA_FROM_FILE, "true");
32+
33+
final ClientOptInput input = new ClientOptInput();
34+
final OpenAPI openAPI = new OpenAPIParser().readLocation(filePath, null, new ParseOptions()).getOpenAPI();
35+
input.openAPI(openAPI);
36+
input.config(codegen);
37+
38+
final DefaultGenerator generator = new DefaultGenerator();
39+
final List<File> files = generator.opts(input).generate();
40+
41+
Assert.assertTrue(files.size() > 0);
42+
return outputPath + "/";
43+
}
44+
45+
46+
@Test(description = "test requestBody")
47+
public void testRequestBody() throws IOException {
48+
final DefaultCodegen codegen = new PythonFlaskConnexionServerCodegen();
49+
final String outputPath = generateFiles(codegen, "src/test/resources/bugs/issue_1666.yaml");
50+
51+
final Path p1 = Paths.get(outputPath + "openapi_server/controllers/test1_controller.py");
52+
assertFileExists(p1);
53+
assertFileContains(p1, "def not_required(body=None):");
54+
assertFileContains(p1, "test_request = body");
55+
56+
final Path p2 = Paths.get(outputPath + "openapi_server/controllers/test2_controller.py");
57+
assertFileContains(p2, "def required(body):");
58+
assertFileContains(p2, "test_request = body");
59+
60+
final Path p3 = Paths.get(outputPath + "openapi_server/controllers/test3_controller.py");
61+
assertFileContains(p3, "def with_path_param(param1, body=None):");
62+
assertFileContains(p3, "test_request = body");
63+
64+
final Path p4 = Paths.get(outputPath + "openapi_server/controllers/test4_controller.py");
65+
assertFileContains(p4, "def with_path_param_required(param1, body):");
66+
assertFileContains(p4, "test_request = body");
67+
}
68+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
openapi: "3.0.3"
2+
info:
3+
title: issue #1666
4+
version: 1.0.0
5+
tags:
6+
- name: test1
7+
- name: test2
8+
- name: test3
9+
- name: test4
10+
paths:
11+
"/not-required":
12+
post:
13+
operationId: notRequired
14+
requestBody:
15+
content:
16+
application/json:
17+
schema:
18+
$ref: '#/components/schemas/TestRequest'
19+
responses:
20+
'200':
21+
description: success
22+
tags:
23+
- test1
24+
"/required":
25+
post:
26+
operationId: required
27+
requestBody:
28+
required: true
29+
content:
30+
application/json:
31+
schema:
32+
$ref: '#/components/schemas/TestRequest'
33+
responses:
34+
'200':
35+
description: success
36+
tags:
37+
- test2
38+
"/with-path-param/{param1}":
39+
post:
40+
operationId: withPathParam
41+
parameters:
42+
- name: param1
43+
in: path
44+
required: true
45+
schema:
46+
type: string
47+
requestBody:
48+
content:
49+
application/json:
50+
schema:
51+
$ref: '#/components/schemas/TestRequest'
52+
responses:
53+
'200':
54+
description: success
55+
tags:
56+
- test3
57+
"/with-path-param-required/{param1}":
58+
post:
59+
operationId: withPathParamRequired
60+
parameters:
61+
- name: param1
62+
in: path
63+
required: true
64+
schema:
65+
type: string
66+
requestBody:
67+
required: true
68+
content:
69+
application/json:
70+
schema:
71+
$ref: '#/components/schemas/TestRequest'
72+
responses:
73+
'200':
74+
description: success
75+
tags:
76+
- test4
77+
components:
78+
schemas:
79+
TestRequest:
80+
type: object
81+
properties:
82+
key1:
83+
type: string

samples/server/petstore/python-flask/openapi_server/controllers/pet_controller.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from openapi_server import util
99

1010

11-
def add_pet(pet): # noqa: E501
11+
def add_pet(body): # noqa: E501
1212
"""Add a new pet to the store
1313
1414
# noqa: E501
@@ -18,6 +18,7 @@ def add_pet(pet): # noqa: E501
1818
1919
:rtype: Union[Pet, Tuple[Pet, int], Tuple[Pet, int, Dict[str, str]]
2020
"""
21+
pet = body
2122
if connexion.request.is_json:
2223
pet = Pet.from_dict(connexion.request.get_json()) # noqa: E501
2324
return 'do some magic!'
@@ -77,7 +78,7 @@ def get_pet_by_id(pet_id): # noqa: E501
7778
return 'do some magic!'
7879

7980

80-
def update_pet(pet): # noqa: E501
81+
def update_pet(body): # noqa: E501
8182
"""Update an existing pet
8283
8384
# noqa: E501
@@ -87,6 +88,7 @@ def update_pet(pet): # noqa: E501
8788
8889
:rtype: Union[Pet, Tuple[Pet, int], Tuple[Pet, int, Dict[str, str]]
8990
"""
91+
pet = body
9092
if connexion.request.is_json:
9193
pet = Pet.from_dict(connexion.request.get_json()) # noqa: E501
9294
return 'do some magic!'

samples/server/petstore/python-flask/openapi_server/controllers/store_controller.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def get_order_by_id(order_id): # noqa: E501
4444
return 'do some magic!'
4545

4646

47-
def place_order(order): # noqa: E501
47+
def place_order(body): # noqa: E501
4848
"""Place an order for a pet
4949
5050
# noqa: E501
@@ -54,6 +54,7 @@ def place_order(order): # noqa: E501
5454
5555
:rtype: Union[Order, Tuple[Order, int], Tuple[Order, int, Dict[str, str]]
5656
"""
57+
order = body
5758
if connexion.request.is_json:
5859
order = Order.from_dict(connexion.request.get_json()) # noqa: E501
5960
return 'do some magic!'

samples/server/petstore/python-flask/openapi_server/controllers/user_controller.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from openapi_server import util
88

99

10-
def create_user(user): # noqa: E501
10+
def create_user(body): # noqa: E501
1111
"""Create user
1212
1313
This can only be done by the logged in user. # noqa: E501
@@ -17,12 +17,13 @@ def create_user(user): # noqa: E501
1717
1818
:rtype: Union[None, Tuple[None, int], Tuple[None, int, Dict[str, str]]
1919
"""
20+
user = body
2021
if connexion.request.is_json:
2122
user = User.from_dict(connexion.request.get_json()) # noqa: E501
2223
return 'do some magic!'
2324

2425

25-
def create_users_with_array_input(user): # noqa: E501
26+
def create_users_with_array_input(body): # noqa: E501
2627
"""Creates list of users with given input array
2728
2829
# noqa: E501
@@ -32,12 +33,13 @@ def create_users_with_array_input(user): # noqa: E501
3233
3334
:rtype: Union[None, Tuple[None, int], Tuple[None, int, Dict[str, str]]
3435
"""
36+
user = body
3537
if connexion.request.is_json:
3638
user = [User.from_dict(d) for d in connexion.request.get_json()] # noqa: E501
3739
return 'do some magic!'
3840

3941

40-
def create_users_with_list_input(user): # noqa: E501
42+
def create_users_with_list_input(body): # noqa: E501
4143
"""Creates list of users with given input array
4244
4345
# noqa: E501
@@ -47,6 +49,7 @@ def create_users_with_list_input(user): # noqa: E501
4749
4850
:rtype: Union[None, Tuple[None, int], Tuple[None, int, Dict[str, str]]
4951
"""
52+
user = body
5053
if connexion.request.is_json:
5154
user = [User.from_dict(d) for d in connexion.request.get_json()] # noqa: E501
5255
return 'do some magic!'
@@ -104,7 +107,7 @@ def logout_user(): # noqa: E501
104107
return 'do some magic!'
105108

106109

107-
def update_user(username, user): # noqa: E501
110+
def update_user(username, body): # noqa: E501
108111
"""Updated user
109112
110113
This can only be done by the logged in user. # noqa: E501
@@ -116,6 +119,7 @@ def update_user(username, user): # noqa: E501
116119
117120
:rtype: Union[None, Tuple[None, int], Tuple[None, int, Dict[str, str]]
118121
"""
122+
user = body
119123
if connexion.request.is_json:
120124
user = User.from_dict(connexion.request.get_json()) # noqa: E501
121125
return 'do some magic!'

0 commit comments

Comments
 (0)