Skip to content

Commit

Permalink
Fix process JsonUnwrapped schemas (#1603)
Browse files Browse the repository at this point in the history
Fixed #1602
  • Loading branch information
altro3 authored Jun 27, 2024
1 parent 21d03b4 commit 85ac288
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@
import static io.micronaut.openapi.visitor.StringUtil.SLASH;
import static io.micronaut.openapi.visitor.StringUtil.UNDERSCORE;
import static io.micronaut.openapi.visitor.Utils.isOpenapi31;
import static io.micronaut.openapi.visitor.Utils.resolveOpenApi;
import static java.util.stream.Collectors.toMap;

/**
Expand Down Expand Up @@ -1172,9 +1173,14 @@ private Schema<?> processGenericAnnotations(Schema<?> schema, ClassElement compo
private void handleUnwrapped(VisitorContext context, Element element, ClassElement elementType, Schema<?> parentSchema, AnnotationValue<JsonUnwrapped> uw) {
Map<String, Schema> schemas = SchemaUtils.resolveSchemas(Utils.resolveOpenApi(context));
ClassElement customElementType = getCustomSchema(elementType.getName(), elementType.getTypeArguments(), context);
var elType = customElementType != null ? customElementType : elementType;
String schemaName = stringValue(element, io.swagger.v3.oas.annotations.media.Schema.class, PROP_NAME)
.orElse(computeDefaultSchemaName(null, customElementType != null ? customElementType : elementType, elementType.getTypeArguments(), context, null));
.orElse(computeDefaultSchemaName(null, elType, elementType.getTypeArguments(), context, null));
Schema<?> wrappedPropertySchema = schemas.get(schemaName);
if (wrappedPropertySchema == null) {
getSchemaDefinition(resolveOpenApi(context), context, elType, elType.getTypeArguments(), element, Collections.emptyList(), null);
wrappedPropertySchema = schemas.get(schemaName);
}
Map<String, Schema> properties = wrappedPropertySchema.getProperties();
if (CollectionUtils.isEmpty(properties)) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class OpenApiJsonUnwrappedsSpec extends AbstractOpenApiTypeElementSpec {

void "test JsonUnwrapped annotation"() {

given:"An API definition"
given: "An API definition"
when:
buildBeanDefinition('test.MyBean', '''
package test;
Expand Down Expand Up @@ -91,16 +91,16 @@ class Test {
@jakarta.inject.Singleton
class MyBean {}
''')
then:"the state is correct"
then: "the state is correct"
Utils.testReference != null

when:"The OpenAPI is retrieved"
when: "The OpenAPI is retrieved"
OpenAPI openAPI = Utils.testReference
Schema schema = openAPI.components.schemas['Test']
Schema dummySchema = openAPI.components.schemas['Dummy']
Schema petSchema = openAPI.components.schemas['Pet']

then:"the components are valid"
then: "the components are valid"
schema.type == 'object'
schema.properties.size() == 13
schema.properties['plain'].$ref == '#/components/schemas/Dummy'
Expand Down Expand Up @@ -176,4 +176,50 @@ class MyBean {}
exampleSchema.properties.nameInJson.type == 'string'
exampleSchema.properties.nameInJson.description == 'example field'
}

void "test issue with JsonUnwrapped and wildcard response type"() {

given: "An API definition"
when:
buildBeanDefinition('test.MyBean', '''
package test;
import com.fasterxml.jackson.annotation.JsonUnwrapped;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Post;
import io.reactivex.Single;
@Controller("/test")
interface TestOperations {
@Post
Single<Test<?>> save(String name, int age);
}
class Base {
public String name;
}
class Test<T extends Base> {
@JsonUnwrapped
public T wrapped;
}
@jakarta.inject.Singleton
class MyBean {}
''')
then: "the state is correct"
Utils.testReference != null

when: "The OpenAPI is retrieved"
def openApi = Utils.testReference
Schema schema = openApi.components.schemas.Test_Base_

then: "the components are valid"
schema.type == 'object'
schema.properties
schema.properties.size() == 1
schema.properties.name
}
}

0 comments on commit 85ac288

Please sign in to comment.