Skip to content

Commit f371a91

Browse files
FIX #21407 - use referenced string enum schema in requestBody if available
1 parent 8087f2b commit f371a91

File tree

4 files changed

+105
-3
lines changed

4 files changed

+105
-3
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,9 @@ public static enum ENUM_PROPERTY_NAMING_TYPE {camelCase, PascalCase, snake_case,
397397
public static final String REMOVE_ENUM_VALUE_PREFIX = "removeEnumValuePrefix";
398398
public static final String REMOVE_ENUM_VALUE_PREFIX_DESC = "Remove the common prefix of enum values";
399399

400+
public static final String USE_STRING_ENUM_SCHEMA_REF_FOR_REQUEST_BODY = "useStringEnumSchemaRefForRequestBody";
401+
public static final String USE_STRING_ENUM_SCHEMA_REF_FOR_REQUEST_BODY_DESC = "A boolean flag that controls how the parameter type for a referenced string enum in a request body is generated. When set to true, the generator will use the referenced schema instead of a plain String type";
402+
400403
public static final String SKIP_ONEOF_ANYOF_GETTER = "skipOneOfAnyOfGetter";
401404
public static final String SKIP_ONEOF_ANYOF_GETTER_DESC = "Skip the generation of getter for sub-schemas in oneOf/anyOf models.";
402405

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
@@ -7794,8 +7794,13 @@ protected void updateRequestBodyForArray(CodegenParameter codegenParameter, Sche
77947794
}
77957795
}
77967796

7797-
protected void updateRequestBodyForString(CodegenParameter codegenParameter, Schema schema, Set<String> imports, String bodyParameterName) {
7798-
updateRequestBodyForPrimitiveType(codegenParameter, schema, bodyParameterName, imports);
7797+
protected void updateRequestBodyForString(CodegenParameter codegenParameter, Schema schema, String name, Set<String> imports, String bodyParameterName) {
7798+
if (convertPropertyToBoolean(CodegenConstants.USE_STRING_ENUM_SCHEMA_REF_FOR_REQUEST_BODY) && !StringUtils.isEmpty(name)) {
7799+
addBodyModelSchema(codegenParameter, name, schema, imports, bodyParameterName, false);
7800+
} else {
7801+
updateRequestBodyForPrimitiveType(codegenParameter, schema, bodyParameterName, imports);
7802+
}
7803+
77997804
if (ModelUtils.isByteArraySchema(schema)) {
78007805
codegenParameter.setIsString(false);
78017806
codegenParameter.isByteArray = true;
@@ -7996,7 +8001,7 @@ public CodegenParameter fromRequestBody(RequestBody body, Set<String> imports, S
79968001
// swagger v2 only, type file
79978002
codegenParameter.isFile = true;
79988003
} else if (ModelUtils.isStringSchema(schema)) {
7999-
updateRequestBodyForString(codegenParameter, schema, imports, bodyParameterName);
8004+
updateRequestBodyForString(codegenParameter, schema, name, imports, bodyParameterName);
80008005
} else if (ModelUtils.isNumberSchema(schema)) {
80018006
updateRequestBodyForPrimitiveType(codegenParameter, schema, bodyParameterName, imports);
80028007
codegenParameter.isNumeric = Boolean.TRUE;

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

Lines changed: 46 additions & 0 deletions
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

@@ -5015,4 +5016,49 @@ public void testSingleRequestParameter_hasSingleParamTrue() {
50155016
// When & Then
50165017
assertThat(codegenOperation.getHasSingleParam()).isTrue();
50175018
}
5019+
5020+
@DataProvider(name = "testRequestBodyWithStringEnumSchemaData")
5021+
private Object[][] testRequestBodyWithStringEnumSchemaData() {
5022+
return new Object[][]{
5023+
{false, "String"},
5024+
{true, "Letter"}
5025+
};
5026+
}
5027+
5028+
@Test(dataProvider = "testRequestBodyWithStringEnumSchemaData")
5029+
public void testRequestBodyWithStringEnumSchema(boolean useStringEnumSchemaRefForRequestBody, String expectedDataType) {
5030+
// Given
5031+
OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/issue_21407.yaml");
5032+
DefaultCodegen codegen = new DefaultCodegen();
5033+
codegen.setOpenAPI(openAPI);
5034+
codegen.additionalProperties().put(CodegenConstants.USE_STRING_ENUM_SCHEMA_REF_FOR_REQUEST_BODY, useStringEnumSchemaRefForRequestBody);
5035+
String path = "/v1/resource-class/send-using-schema";
5036+
5037+
// When
5038+
CodegenOperation codegenOperation = codegen.fromOperation(path, "POST", openAPI.getPaths().get(path).getPost(), null);
5039+
5040+
// Then
5041+
assertThat(codegenOperation.bodyParam).satisfies(bodyParam -> {
5042+
assertThat(bodyParam).isNotNull();
5043+
assertThat(bodyParam.getDataType()).isEqualTo(expectedDataType);
5044+
});
5045+
}
5046+
5047+
@Test
5048+
public void testRequestBodyWithStringSchema() {
5049+
// Given
5050+
OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/issue_21407.yaml");
5051+
DefaultCodegen codegen = new DefaultCodegen();
5052+
codegen.setOpenAPI(openAPI);
5053+
String path = "/v1/resource-class/send-using-string";
5054+
5055+
// When
5056+
CodegenOperation codegenOperation = codegen.fromOperation(path, "POST", openAPI.getPaths().get(path).getPost(), null);
5057+
5058+
// Then
5059+
assertThat(codegenOperation.bodyParam).satisfies(bodyParam -> {
5060+
assertThat(bodyParam).isNotNull();
5061+
assertThat(bodyParam.getDataType()).isEqualTo("String");
5062+
});
5063+
}
50185064
}
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)