Skip to content

Commit b798dc0

Browse files
committed
Merge branch 'master' of https://github.com/WannabeSoftwareEngineer/openapi-generator into WannabeSoftwareEngineer-master
2 parents 258d971 + f371a91 commit b798dc0

File tree

3 files changed

+94
-4
lines changed

3 files changed

+94
-4
lines changed

modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7805,8 +7805,13 @@ protected void updateRequestBodyForArray(CodegenParameter codegenParameter, Sche
78057805
}
78067806
}
78077807

7808-
protected void updateRequestBodyForString(CodegenParameter codegenParameter, Schema schema, Set<String> imports, String bodyParameterName) {
7809-
updateRequestBodyForPrimitiveType(codegenParameter, schema, bodyParameterName, imports);
7808+
protected void updateRequestBodyForString(CodegenParameter codegenParameter, Schema schema, String name, Set<String> imports, String bodyParameterName) {
7809+
if (!StringUtils.isEmpty(name)) {
7810+
addBodyModelSchema(codegenParameter, name, schema, imports, bodyParameterName, false);
7811+
} else {
7812+
updateRequestBodyForPrimitiveType(codegenParameter, schema, bodyParameterName, imports);
7813+
}
7814+
78107815
if (ModelUtils.isByteArraySchema(schema)) {
78117816
codegenParameter.setIsString(false);
78127817
codegenParameter.isByteArray = true;
@@ -8007,7 +8012,7 @@ public CodegenParameter fromRequestBody(RequestBody body, Set<String> imports, S
80078012
// swagger v2 only, type file
80088013
codegenParameter.isFile = true;
80098014
} else if (ModelUtils.isStringSchema(schema)) {
8010-
updateRequestBodyForString(codegenParameter, schema, imports, bodyParameterName);
8015+
updateRequestBodyForString(codegenParameter, schema, name, imports, bodyParameterName);
80118016
} else if (ModelUtils.isNumberSchema(schema)) {
80128017
updateRequestBodyForPrimitiveType(codegenParameter, schema, bodyParameterName, imports);
80138018
codegenParameter.isNumeric = Boolean.TRUE;

modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultCodegenTest.java

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import org.openapitools.codegen.utils.SemVer;
4949
import org.slf4j.LoggerFactory;
5050
import org.testng.Assert;
51+
import org.testng.annotations.DataProvider;
5152
import org.testng.annotations.Ignore;
5253
import org.testng.annotations.Test;
5354

@@ -5044,4 +5045,40 @@ private List<String> getNames(List<CodegenProperty> props) {
50445045
if (props == null) return null;
50455046
return props.stream().map(v -> v.name).collect(Collectors.toList());
50465047
}
5047-
}
5048+
5049+
@Test
5050+
public void testRequestBodyWithStringEnumSchema() {
5051+
// Given
5052+
OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/issue_21407.yaml");
5053+
DefaultCodegen codegen = new DefaultCodegen();
5054+
codegen.setOpenAPI(openAPI);
5055+
String path = "/v1/resource-class/send-using-schema";
5056+
5057+
// When
5058+
CodegenOperation codegenOperation = codegen.fromOperation(path, "POST", openAPI.getPaths().get(path).getPost(), null);
5059+
5060+
// Then
5061+
assertThat(codegenOperation.bodyParam).satisfies(bodyParam -> {
5062+
assertThat(bodyParam).isNotNull();
5063+
assertThat(bodyParam.getDataType()).isEqualTo("Letter");
5064+
});
5065+
}
5066+
5067+
@Test
5068+
public void testRequestBodyWithStringSchema() {
5069+
// Given
5070+
OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/issue_21407.yaml");
5071+
DefaultCodegen codegen = new DefaultCodegen();
5072+
codegen.setOpenAPI(openAPI);
5073+
String path = "/v1/resource-class/send-using-string";
5074+
5075+
// When
5076+
CodegenOperation codegenOperation = codegen.fromOperation(path, "POST", openAPI.getPaths().get(path).getPost(), null);
5077+
5078+
// Then
5079+
assertThat(codegenOperation.bodyParam).satisfies(bodyParam -> {
5080+
assertThat(bodyParam).isNotNull();
5081+
assertThat(bodyParam.getDataType()).isEqualTo("String");
5082+
});
5083+
}
5084+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
openapi: 3.0.1
2+
info:
3+
title: sample spec
4+
description: "Sample spec"
5+
version: 0.0.1
6+
tags:
7+
- name: ResourceClass
8+
paths:
9+
/v1/resource-class/send-using-schema:
10+
post:
11+
tags:
12+
- ResourceClass
13+
description: Add @Operation annotation to provide a description
14+
operationId: send-using-schema
15+
requestBody:
16+
content:
17+
application/json:
18+
schema:
19+
$ref: "#/components/schemas/Letter"
20+
responses:
21+
"200":
22+
description: OK - the request has succeeded.
23+
content:
24+
application/json:
25+
schema:
26+
$ref: "#/components/schemas/Letter"
27+
/v1/resource-class/send-using-string:
28+
post:
29+
tags:
30+
- ResourceClass
31+
description: Add @Operation annotation to provide a description
32+
operationId: send-using-string
33+
requestBody:
34+
content:
35+
application/json:
36+
schema:
37+
type: string
38+
responses:
39+
"204":
40+
description: "No Content - the request has been successfully processed,\
41+
\ but there is no additional content."
42+
components:
43+
schemas:
44+
Letter:
45+
type: string
46+
enum:
47+
- A
48+
- B

0 commit comments

Comments
 (0)