Skip to content

FIX #21407 - use referenced string enum schema in requestBody if available #21410

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,9 @@ public static enum ENUM_PROPERTY_NAMING_TYPE {camelCase, PascalCase, snake_case,
public static final String REMOVE_ENUM_VALUE_PREFIX = "removeEnumValuePrefix";
public static final String REMOVE_ENUM_VALUE_PREFIX_DESC = "Remove the common prefix of enum values";

public static final String USE_STRING_ENUM_SCHEMA_REF_FOR_REQUEST_BODY = "useStringEnumSchemaRefForRequestBody";
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";

public static final String SKIP_ONEOF_ANYOF_GETTER = "skipOneOfAnyOfGetter";
public static final String SKIP_ONEOF_ANYOF_GETTER_DESC = "Skip the generation of getter for sub-schemas in oneOf/anyOf models.";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7794,8 +7794,13 @@ protected void updateRequestBodyForArray(CodegenParameter codegenParameter, Sche
}
}

protected void updateRequestBodyForString(CodegenParameter codegenParameter, Schema schema, Set<String> imports, String bodyParameterName) {
updateRequestBodyForPrimitiveType(codegenParameter, schema, bodyParameterName, imports);
protected void updateRequestBodyForString(CodegenParameter codegenParameter, Schema schema, String name, Set<String> imports, String bodyParameterName) {
if (convertPropertyToBoolean(CodegenConstants.USE_STRING_ENUM_SCHEMA_REF_FOR_REQUEST_BODY) && !StringUtils.isEmpty(name)) {
addBodyModelSchema(codegenParameter, name, schema, imports, bodyParameterName, false);
} else {
updateRequestBodyForPrimitiveType(codegenParameter, schema, bodyParameterName, imports);
}

if (ModelUtils.isByteArraySchema(schema)) {
codegenParameter.setIsString(false);
codegenParameter.isByteArray = true;
Expand Down Expand Up @@ -7996,7 +8001,7 @@ public CodegenParameter fromRequestBody(RequestBody body, Set<String> imports, S
// swagger v2 only, type file
codegenParameter.isFile = true;
} else if (ModelUtils.isStringSchema(schema)) {
updateRequestBodyForString(codegenParameter, schema, imports, bodyParameterName);
updateRequestBodyForString(codegenParameter, schema, name, imports, bodyParameterName);
} else if (ModelUtils.isNumberSchema(schema)) {
updateRequestBodyForPrimitiveType(codegenParameter, schema, bodyParameterName, imports);
codegenParameter.isNumeric = Boolean.TRUE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import org.openapitools.codegen.utils.SemVer;
import org.slf4j.LoggerFactory;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Ignore;
import org.testng.annotations.Test;

Expand Down Expand Up @@ -5015,4 +5016,49 @@ public void testSingleRequestParameter_hasSingleParamTrue() {
// When & Then
assertThat(codegenOperation.getHasSingleParam()).isTrue();
}

@DataProvider(name = "testRequestBodyWithStringEnumSchemaData")
private Object[][] testRequestBodyWithStringEnumSchemaData() {
return new Object[][]{
{false, "String"},
{true, "Letter"}
};
}

@Test(dataProvider = "testRequestBodyWithStringEnumSchemaData")
public void testRequestBodyWithStringEnumSchema(boolean useStringEnumSchemaRefForRequestBody, String expectedDataType) {
// Given
OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/issue_21407.yaml");
DefaultCodegen codegen = new DefaultCodegen();
codegen.setOpenAPI(openAPI);
codegen.additionalProperties().put(CodegenConstants.USE_STRING_ENUM_SCHEMA_REF_FOR_REQUEST_BODY, useStringEnumSchemaRefForRequestBody);
String path = "/v1/resource-class/send-using-schema";

// When
CodegenOperation codegenOperation = codegen.fromOperation(path, "POST", openAPI.getPaths().get(path).getPost(), null);

// Then
assertThat(codegenOperation.bodyParam).satisfies(bodyParam -> {
assertThat(bodyParam).isNotNull();
assertThat(bodyParam.getDataType()).isEqualTo(expectedDataType);
});
}

@Test
public void testRequestBodyWithStringSchema() {
// Given
OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/issue_21407.yaml");
DefaultCodegen codegen = new DefaultCodegen();
codegen.setOpenAPI(openAPI);
String path = "/v1/resource-class/send-using-string";

// When
CodegenOperation codegenOperation = codegen.fromOperation(path, "POST", openAPI.getPaths().get(path).getPost(), null);

// Then
assertThat(codegenOperation.bodyParam).satisfies(bodyParam -> {
assertThat(bodyParam).isNotNull();
assertThat(bodyParam.getDataType()).isEqualTo("String");
});
}
}
48 changes: 48 additions & 0 deletions modules/openapi-generator/src/test/resources/3_0/issue_21407.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
openapi: 3.0.1
info:
title: sample spec
description: "Sample spec"
version: 0.0.1
tags:
- name: ResourceClass
paths:
/v1/resource-class/send-using-schema:
post:
tags:
- ResourceClass
description: Add @Operation annotation to provide a description
operationId: send-using-schema
requestBody:
content:
application/json:
schema:
$ref: "#/components/schemas/Letter"
responses:
"200":
description: OK - the request has succeeded.
content:
application/json:
schema:
$ref: "#/components/schemas/Letter"
/v1/resource-class/send-using-string:
post:
tags:
- ResourceClass
description: Add @Operation annotation to provide a description
operationId: send-using-string
requestBody:
content:
application/json:
schema:
type: string
responses:
"204":
description: "No Content - the request has been successfully processed,\
\ but there is no additional content."
components:
schemas:
Letter:
type: string
enum:
- A
- B