Skip to content
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

Fixed request body enum #1913

Open
wants to merge 1 commit into
base: 6.13.x
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 @@ -26,6 +26,7 @@
import io.swagger.v3.oas.models.media.MediaType;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.parameters.Parameter;
import io.swagger.v3.oas.models.parameters.RequestBody;
import io.swagger.v3.oas.models.responses.ApiResponse;
import io.swagger.v3.oas.models.servers.Server;
import io.swagger.v3.parser.util.SchemaTypeUtil;
Expand Down Expand Up @@ -732,6 +733,62 @@ public String toModelTestFilename(String name) {
return toModelName(name) + "Test";
}

@Override
public CodegenParameter fromRequestBody(RequestBody body, Set<String> imports, String bodyParameterName) {
var rqBody = super.fromRequestBody(body, imports, bodyParameterName);

var rqBodySchema = body.getContent() != null && !body.getContent().isEmpty() ? body.getContent().entrySet().iterator().next().getValue().getSchema() : null;
CodegenProperty codegenProperty = fromProperty(bodyParameterName, rqBodySchema, false);

if (rqBodySchema != null) {
rqBodySchema = unaliasSchema(rqBodySchema);

if (getUseInlineModelResolver()) {
codegenProperty = fromProperty(bodyParameterName, getReferencedSchemaWhenNotEnum(rqBodySchema), false);
} else {
codegenProperty = fromProperty(bodyParameterName, rqBodySchema, false);
}
rqBody.setSchema(codegenProperty);
}

if (Boolean.TRUE.equals(codegenProperty.isModel)) {
rqBody.isModel = true;
}

rqBody.dataFormat = codegenProperty.dataFormat;
if (body.getRequired() != null) {
rqBody.required = body.getRequired();
}

// set containerType
rqBody.containerType = codegenProperty.containerType;
rqBody.containerTypeMapped = codegenProperty.containerTypeMapped;

// enum
updateCodegenPropertyEnum(codegenProperty);
rqBody.isEnum = codegenProperty.isEnum;
rqBody.isEnumRef = codegenProperty.isEnumRef;
rqBody._enum = codegenProperty._enum;
rqBody.allowableValues = codegenProperty.allowableValues;

if (codegenProperty.isEnum || codegenProperty.isEnumRef) {
rqBody.datatypeWithEnum = codegenProperty.datatypeWithEnum;
rqBody.enumName = codegenProperty.enumName;
if (codegenProperty.defaultValue != null) {
rqBody.enumDefaultValue = codegenProperty.defaultValue.replace(codegenProperty.enumName + ".", "");
}
}
return rqBody;
}

private Schema getReferencedSchemaWhenNotEnum(Schema parameterSchema) {
Schema referencedSchema = ModelUtils.getReferencedSchema(openAPI, parameterSchema);
if (referencedSchema.getEnum() != null && !referencedSchema.getEnum().isEmpty()) {
referencedSchema = parameterSchema;
}
return referencedSchema;
}

@Override
public CodegenParameter fromParameter(Parameter p, Set<String> imports) {
var parameter = super.fromParameter(p, imports);
Expand Down Expand Up @@ -811,7 +868,11 @@ public CodegenProperty fromProperty(String name, Schema schema, boolean required
property.vendorExtensions.put("realName", realName);

if (schema != null && schema.get$ref() != null) {
schema = ModelUtils.getSchemaFromRefToSchemaWithProperties(openAPI, schema.get$ref());
var refSchema = ModelUtils.getSchemaFromRefToSchemaWithProperties(openAPI, schema.get$ref());
if (refSchema == null) {
refSchema = ModelUtils.getReferencedSchema(openAPI, schema);
}
schema = refSchema;
}

String defaultValueInit;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.media.StringSchema;
import io.swagger.v3.oas.models.parameters.Parameter;
import io.swagger.v3.oas.models.parameters.RequestBody;
import io.swagger.v3.oas.models.responses.ApiResponse;
import io.swagger.v3.oas.models.servers.Server;
import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -1416,6 +1417,62 @@ public String toModelName(final String name) {
return schemaKeyToModelNameCache.get(name);
}

@Override
public CodegenParameter fromRequestBody(RequestBody body, Set<String> imports, String bodyParameterName) {
var rqBody = super.fromRequestBody(body, imports, bodyParameterName);

var rqBodySchema = body.getContent() != null && !body.getContent().isEmpty() ? body.getContent().entrySet().iterator().next().getValue().getSchema() : null;
CodegenProperty codegenProperty = fromProperty(bodyParameterName, rqBodySchema, false);

if (rqBodySchema != null) {
rqBodySchema = unaliasSchema(rqBodySchema);

if (getUseInlineModelResolver()) {
codegenProperty = fromProperty(bodyParameterName, getReferencedSchemaWhenNotEnum(rqBodySchema), false);
} else {
codegenProperty = fromProperty(bodyParameterName, rqBodySchema, false);
}
rqBody.setSchema(codegenProperty);
}

if (Boolean.TRUE.equals(codegenProperty.isModel)) {
rqBody.isModel = true;
}

rqBody.dataFormat = codegenProperty.dataFormat;
if (body.getRequired() != null) {
rqBody.required = body.getRequired();
}

// set containerType
rqBody.containerType = codegenProperty.containerType;
rqBody.containerTypeMapped = codegenProperty.containerTypeMapped;

// enum
updateCodegenPropertyEnum(codegenProperty);
rqBody.isEnum = codegenProperty.isEnum;
rqBody.isEnumRef = codegenProperty.isEnumRef;
rqBody._enum = codegenProperty._enum;
rqBody.allowableValues = codegenProperty.allowableValues;

if (codegenProperty.isEnum || codegenProperty.isEnumRef) {
rqBody.datatypeWithEnum = codegenProperty.datatypeWithEnum;
rqBody.enumName = codegenProperty.enumName;
if (codegenProperty.defaultValue != null) {
rqBody.enumDefaultValue = codegenProperty.defaultValue.replace(codegenProperty.enumName + ".", "");
}
}
return rqBody;
}

private Schema getReferencedSchemaWhenNotEnum(Schema parameterSchema) {
Schema referencedSchema = ModelUtils.getReferencedSchema(openAPI, parameterSchema);
if (referencedSchema.getEnum() != null && !referencedSchema.getEnum().isEmpty()) {
referencedSchema = parameterSchema;
}
return referencedSchema;
}

@Override
public CodegenParameter fromParameter(Parameter p, Set<String> imports) {
var parameter = super.fromParameter(p, imports);
Expand Down Expand Up @@ -1488,7 +1545,11 @@ public CodegenProperty fromProperty(String name, Schema schema, boolean required
property.vendorExtensions.put("realName", realName);

if (schema != null && schema.get$ref() != null) {
schema = ModelUtils.getSchemaFromRefToSchemaWithProperties(openAPI, schema.get$ref());
var refSchema = ModelUtils.getSchemaFromRefToSchemaWithProperties(openAPI, schema.get$ref());
if (refSchema == null) {
refSchema = ModelUtils.getReferencedSchema(openAPI, schema);
}
schema = refSchema;
}

String defaultValueInit;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
class JavaMicronautClientCodegenTest extends AbstractMicronautCodegenTest {

@Test
void clientOptsUnicity() {
void clientOptsUniqueness() {
var codegen = new JavaMicronautClientCodegen();
codegen.cliOptions()
.stream()
Expand Down Expand Up @@ -557,7 +557,7 @@ void testParamsWithDefaultValue() {

assertFileContains(path + "api/DefaultApi.java",
"@QueryValue(\"ids\") @Nullable List<@NotNull Integer> ids",
"@PathVariable(name = \"apiVersion\", defaultValue = \"v5\") @Nullable BrowseSearchOrdersApiVersionParameter apiVersio",
"@PathVariable(name = \"apiVersion\", defaultValue = \"v5\") @Nullable BrowseSearchOrdersApiVersionParameter apiVersion",
"@Header(name = \"Content-Type\", defaultValue = \"application/json\") @Nullable String contentType"
);
}
Expand Down Expand Up @@ -1460,4 +1460,20 @@ public int hashCode() {
}
""");
}

@Test
void testBodyEnum() {

var codegen = new JavaMicronautClientCodegen();
String outputPath = generateFiles(codegen, "src/test/resources/3_0/body-enum.yml", CodegenConstants.APIS, CodegenConstants.MODELS);
String path = outputPath + "src/main/java/org/openapitools/";

assertFileContains(path + "api/MyCustomApi.java", """
@Post("/api/v1/colors/{name}")
Mono<@NotNull String> selectColor(
@Body @NotNull Color body
);
""");
assertFileContains(path + "model/Color.java", "public enum Color {");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class JavaMicronautServerCodegenTest extends AbstractMicronautCodegenTest {
static String MULTI_TAGS_TEST_PATH = "src/test/resources/3_0/micronaut/multi-tags-test.yaml";

@Test
void clientOptsUnicity() {
void clientOptsUniqueness() {
var codegen = new JavaMicronautServerCodegen();
codegen.cliOptions()
.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
class KotlinMicronautClientCodegenTest extends AbstractMicronautCodegenTest {

@Test
void clientOptsUnicity() {
void clientOptsUniqueness() {
var codegen = new KotlinMicronautClientCodegen();
codegen.cliOptions()
.stream()
Expand Down Expand Up @@ -1363,4 +1363,20 @@ override fun hashCode(): Int =
Objects.hash(super.hashCode())
""");
}

@Test
void testBodyEnum() {

var codegen = new KotlinMicronautClientCodegen();
String outputPath = generateFiles(codegen, "src/test/resources/3_0/body-enum.yml", CodegenConstants.APIS, CodegenConstants.MODELS);
String path = outputPath + "src/main/kotlin/org/openapitools/";

assertFileContains(path + "api/MyCustomApi.kt", """
@Post("/api/v1/colors/{name}")
fun selectColor(
@Body @NotNull body: Color
): Mono<String>
""");
assertFileContains(path + "model/Color.kt", "enum class Color(");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class KotlinMicronautServerCodegenTest extends AbstractMicronautCodegenTest {
static String MULTI_TAGS_TEST_PATH = "src/test/resources/3_0/micronaut/multi-tags-test.yaml";

@Test
void clientOptsUnicity() {
void clientOptsUniqueness() {
var codegen = new KotlinMicronautServerCodegen();
codegen.cliOptions()
.stream()
Expand Down
42 changes: 42 additions & 0 deletions openapi-generator/src/test/resources/3_0/body-enum.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
openapi: 3.1.0
info:
version: '1.0.0'
title: 'OpenAPI BUG REST API'
servers:
- url: 'localhost:3000'

paths:
/api/v1/colors/{name}:
post:
tags: [ my-custom ]
operationId: "selectColor"
requestBody:
required: true
content:
application/json:
schema:
$ref: "#/components/schemas/Color"
responses:
"200":
description: "OK"
content:
application/json:
schema:
type: string

components:
schemas:

Color:
type: string
enum:
- GREEN
- RED
- WHITE
- BLACK
- YELLOW
- BLUE

tags:
- name: my-custom
description: 'All API operations'
Loading