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

Fix process JsonUnwrapped schemas #1603

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