Skip to content

Commit

Permalink
Fixed parse enum default value (#1073)
Browse files Browse the repository at this point in the history
  • Loading branch information
altro3 authored Jun 20, 2023
1 parent 3337b20 commit 0974fe1
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -972,7 +972,7 @@ protected Schema resolveSchema(OpenAPI openAPI, @Nullable Element definingElemen
if (schema != null) {

if (isSubstitudedType) {
processShemaAnn(schema, context, definingElement, schemaAnnotationValue);
processShemaAnn(schema, context, definingElement, type, schemaAnnotationValue);
}

if (definingElement != null && StringUtils.isEmpty(schema.getDescription())) {
Expand Down Expand Up @@ -1189,7 +1189,7 @@ protected Schema bindSchemaForElement(VisitorContext context, TypedElement eleme
Schema originalSchema = schemaToBind;

if (originalSchema.get$ref() != null) {
Schema schemaFromAnn = schemaFromAnnotation(context, element, schemaAnn);
Schema schemaFromAnn = schemaFromAnnotation(context, element, elementType, schemaAnn);
if (schemaFromAnn != null) {
schemaToBind = schemaFromAnn;
}
Expand Down Expand Up @@ -1465,18 +1465,18 @@ protected void processJavaxValidationAnnotations(Element element, ClassElement e
}
}

Schema schemaFromAnnotation(VisitorContext context, Element element, AnnotationValue<io.swagger.v3.oas.annotations.media.Schema> schemaAnn) {
Schema schemaFromAnnotation(VisitorContext context, Element element, ClassElement type, AnnotationValue<io.swagger.v3.oas.annotations.media.Schema> schemaAnn) {
if (schemaAnn == null) {
return null;
}

Schema schemaToBind = new Schema();
processShemaAnn(schemaToBind, context, element, schemaAnn);
processShemaAnn(schemaToBind, context, element, type, schemaAnn);

return schemaToBind;
}

void processShemaAnn(Schema schemaToBind, VisitorContext context, Element element, @NonNull AnnotationValue<io.swagger.v3.oas.annotations.media.Schema> schemaAnn) {
void processShemaAnn(Schema schemaToBind, VisitorContext context, Element element, ClassElement type, @NonNull AnnotationValue<io.swagger.v3.oas.annotations.media.Schema> schemaAnn) {

Map<CharSequence, Object> annValues = schemaAnn.getValues();
if (annValues.containsKey("description")) {
Expand Down Expand Up @@ -1539,7 +1539,7 @@ void processShemaAnn(Schema schemaToBind, VisitorContext context, Element elemen

String schemaDefaultValue = (String) annValues.get("defaultValue");
if (schemaDefaultValue != null) {
setDefaultValueObject(schemaToBind, schemaDefaultValue, element, schemaToBind.getType(), schemaToBind.getFormat(), false, context);
setDefaultValueObject(schemaToBind, schemaDefaultValue, type, schemaToBind.getType(), schemaToBind.getFormat(), false, context);
}
String schemaExample = (String) annValues.get("example");
if (StringUtils.isNotEmpty(schemaExample)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -884,4 +884,97 @@ class MyBean {}
backupFrequencySchema.enum.size() == 8
backupFrequencySchema.type == 'string'
}

void "test enum field default schema value"() {

when:
buildBeanDefinition('test.MyBean', '''
package test;
import io.micronaut.core.annotation.Introspected;
import io.micronaut.http.HttpResponse;
import io.micronaut.http.MediaType;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Post;
import io.micronaut.http.multipart.CompletedFileUpload;
import io.swagger.v3.oas.annotations.Hidden;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotNull;
@Controller
class OpenApiController {
@Post(consumes = MediaType.MULTIPART_FORM_DATA)
public HttpResponse processAsync(
@NotNull CompletedFileUpload template,
@Schema(implementation = Parameters.class) String parameters) {
return null;
}
}
@Schema(example = "{\\n"
+ "\\"s3url\\" : \\"s3://bucket/b559615a-af21-417f-9a63-e5f6eca42342.docx\\","
+ "\\"stampWidth\\" : 220,"
+ "\\"stampHeight\\" : 85,"
+ "\\"stampAlign\\" : \\"LEFT\\","
+ "\\"pageNumber\\" : 1"
+ "}")
class Parameters {
/**
* Certificate base64 encoded
*/
@Hidden
public String certificate;
public String s3url;
@Schema(defaultValue = "220")
public Integer stampWidth;
@Schema(defaultValue = "85")
public Integer stampHeight;
@Schema(defaultValue = "RIGHT")
public ParagraphAlignment stampAlign;
}
enum ParagraphAlignment {
START(1),
CENTER(2),
END(3),
LEFT(4),
RIGHT(5);
private final int value;
ParagraphAlignment(int val) {
value = val;
}
public static ParagraphAlignment valueOf(int type) {
return null;
}
public int getValue() {
return value;
}
}
@jakarta.inject.Singleton
class MyBean {}
''')
then: "the state is correct"
Utils.testReference != null

when: "The OpenAPI is retrieved"
OpenAPI openAPI = Utils.testReference
Operation operation = openAPI.paths.get("/").post
Schema parametersSchema = openAPI.components.schemas.Parameters

then:
operation
parametersSchema
parametersSchema.properties.stampAlign.allOf
parametersSchema.properties.stampAlign.allOf[0].$ref == '#/components/schemas/ParagraphAlignment'
parametersSchema.properties.stampAlign.allOf[1].default == 'RIGHT'
}
}

0 comments on commit 0974fe1

Please sign in to comment.