Skip to content
Draft
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 @@ -7805,8 +7805,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 (!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 @@ -8007,7 +8012,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 @@ -5044,4 +5045,40 @@ private List<String> getNames(List<CodegenProperty> props) {
if (props == null) return null;
return props.stream().map(v -> v.name).collect(Collectors.toList());
}
}

@Test
public void testRequestBodyWithStringEnumSchema() {
// 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-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("Letter");
});
}

@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
Loading