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

Moves the logic that handles the registration for serialization of OpenAPI schema class to smallrye-openapi-extension #1529

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 @@ -49,7 +49,6 @@
import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.AnnotationTarget;
import org.jboss.jandex.AnnotationTarget.Kind;
import org.jboss.jandex.AnnotationValue;
import org.jboss.jandex.ClassInfo;
import org.jboss.jandex.DotName;
import org.jboss.jandex.IndexView;
Expand Down Expand Up @@ -137,14 +136,6 @@ public class ResteasyScanningProcessor {

private static final DotName JSONB_ANNOTATION = DotName.createSimple("javax.json.bind.annotation.JsonbAnnotation");

private static final DotName OPENAPI_SCHEMA = DotName
.createSimple("org.eclipse.microprofile.openapi.annotations.media.Schema");
private static final DotName OPENAPI_RESPONSE = DotName
.createSimple("org.eclipse.microprofile.openapi.annotations.responses.APIResponse");

private static final DotName OPENAPI_RESPONSES = DotName
.createSimple("org.eclipse.microprofile.openapi.annotations.responses.APIResponses");

private static final Set<DotName> TYPES_IGNORED_FOR_REFLECTION = new HashSet<>(Arrays.asList(
// javax.json
DotName.createSimple("javax.json.JsonObject"),
Expand Down Expand Up @@ -183,14 +174,6 @@ public class ResteasyScanningProcessor {
};
private static final DotName SINGLETON_SCOPE = DotName.createSimple(Singleton.class.getName());

private static final String OPENAPI_RESPONSE_CONTENT = "content";
private static final String OPENAPI_RESPONSE_SCHEMA = "schema";
private static final String OPENAPI_SCHEMA_NOT = "not";
private static final String OPENAPI_SCHEMA_ONE_OF = "oneOf";
private static final String OPENAPI_SCHEMA_ANY_OF = "anyOf";
private static final String OPENAPI_SCHEMA_ALL_OF = "allOf";
private static final String OPENAPI_SCHEMA_IMPLEMENTATION = "implementation";

/**
* JAX-RS configuration.
*/
Expand Down Expand Up @@ -553,89 +536,11 @@ private void registerReflectionForSerialization(BuildProducer<ReflectiveClassBui
}
}

// Generate reflection declaration from MP OpenAPI Schema definition
// They are needed for serialization.
Collection<AnnotationInstance> schemaAnnotationInstances = index.getAnnotations(OPENAPI_SCHEMA);
for (AnnotationInstance schemaAnnotationInstance : schemaAnnotationInstances) {
AnnotationTarget typeTarget = schemaAnnotationInstance.target();
if (typeTarget.kind() != Kind.CLASS) {
continue;
}
reflectiveHierarchy
.produce(new ReflectiveHierarchyBuildItem(Type.create(typeTarget.asClass().name(), Type.Kind.CLASS)));
}

// Generate reflection declaration from MP OpenAPI APIResponse schema definition
// They are needed for serialization
Collection<AnnotationInstance> apiResponseAnnotationInstances = index.getAnnotations(OPENAPI_RESPONSE);
registerReflectionForApiResponseSchemaSerialization(reflectiveClass, reflectiveHierarchy,
apiResponseAnnotationInstances);

// Generate reflection declaration from MP OpenAPI APIResponses schema definition
// They are needed for serialization
Collection<AnnotationInstance> apiResponsesAnnotationInstances = index.getAnnotations(OPENAPI_RESPONSES);
for (AnnotationInstance apiResponsesAnnotationInstance : apiResponsesAnnotationInstances) {
AnnotationValue apiResponsesAnnotationValue = apiResponsesAnnotationInstance.value();
if (apiResponsesAnnotationValue == null) {
continue;
}
registerReflectionForApiResponseSchemaSerialization(reflectiveClass, reflectiveHierarchy,
Arrays.asList(apiResponsesAnnotationValue.asNestedArray()));
}

// In the case of a constraint violation, these elements might be returned as entities and will be serialized
reflectiveClass.produce(new ReflectiveClassBuildItem(true, true, ViolationReport.class.getName()));
reflectiveClass.produce(new ReflectiveClassBuildItem(true, true, ResteasyConstraintViolation.class.getName()));
}

private void registerReflectionForApiResponseSchemaSerialization(BuildProducer<ReflectiveClassBuildItem> reflectiveClass,
BuildProducer<ReflectiveHierarchyBuildItem> reflectiveHierarchy,
Collection<AnnotationInstance> apiResponseAnnotationInstances) {
for (AnnotationInstance apiResponseAnnotationInstance : apiResponseAnnotationInstances) {
AnnotationInstance[] contents = apiResponseAnnotationInstance.value(OPENAPI_RESPONSE_CONTENT).asNestedArray();
if (contents == null) {
continue;
}
for (AnnotationInstance content : contents) {
AnnotationInstance schema = content.value(OPENAPI_RESPONSE_SCHEMA).asNested();
if (schema == null) {
continue;
}

AnnotationValue schemaImplementationClass = schema.value(OPENAPI_SCHEMA_IMPLEMENTATION);
if (schemaImplementationClass != null) {
reflectiveHierarchy.produce(new ReflectiveHierarchyBuildItem(schemaImplementationClass.asClass()));
}

AnnotationValue schemaNotClass = schema.value(OPENAPI_SCHEMA_NOT);
if (schemaNotClass != null) {
reflectiveClass.produce(new ReflectiveClassBuildItem(true, true, schemaNotClass.asString()));
}

AnnotationValue schemaOneOfClasses = schema.value(OPENAPI_SCHEMA_ONE_OF);
if (schemaOneOfClasses != null) {
for (Type schemaOneOfClass : schemaOneOfClasses.asClassArray()) {
reflectiveHierarchy.produce(new ReflectiveHierarchyBuildItem(schemaOneOfClass));
}
}

AnnotationValue schemaAnyOfClasses = schema.value(OPENAPI_SCHEMA_ANY_OF);
if (schemaAnyOfClasses != null) {
for (Type schemaAnyOfClass : schemaAnyOfClasses.asClassArray()) {
reflectiveHierarchy.produce(new ReflectiveHierarchyBuildItem(schemaAnyOfClass));
}
}

AnnotationValue schemaAllOfClasses = schema.value(OPENAPI_SCHEMA_ALL_OF);
if (schemaAllOfClasses != null) {
for (Type schemaAllOfClass : schemaAllOfClasses.asClassArray()) {
reflectiveHierarchy.produce(new ReflectiveHierarchyBuildItem(schemaAllOfClass));
}
}
}
}
}

private static void categorizeProviders(Set<String> availableProviders, MediaTypeMap<String> categorizedReaders,
MediaTypeMap<String> categorizedWriters, MediaTypeMap<String> categorizedContextResolvers,
Set<String> otherProviders) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,24 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.eclipse.microprofile.config.Config;
import org.eclipse.microprofile.config.ConfigProvider;
import org.eclipse.microprofile.openapi.annotations.media.Schema;
import org.eclipse.microprofile.openapi.annotations.responses.APIResponse;
import org.eclipse.microprofile.openapi.annotations.responses.APIResponses;
import org.eclipse.microprofile.openapi.models.OpenAPI;
import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.AnnotationTarget;
import org.jboss.jandex.AnnotationValue;
import org.jboss.jandex.DotName;
import org.jboss.jandex.IndexView;
import org.jboss.jandex.Type;

import io.quarkus.arc.deployment.AdditionalBeanBuildItem;
import io.quarkus.arc.deployment.BeanContainerListenerBuildItem;
Expand All @@ -42,6 +51,8 @@
import io.quarkus.deployment.builditem.CombinedIndexBuildItem;
import io.quarkus.deployment.builditem.FeatureBuildItem;
import io.quarkus.deployment.builditem.HotDeploymentConfigFileBuildItem;
import io.quarkus.deployment.builditem.substrate.ReflectiveClassBuildItem;
import io.quarkus.deployment.builditem.substrate.ReflectiveHierarchyBuildItem;
import io.quarkus.resteasy.deployment.ResteasyJaxrsConfig;
import io.quarkus.runtime.annotations.ConfigItem;
import io.quarkus.runtime.annotations.ConfigRoot;
Expand All @@ -67,6 +78,18 @@ public class SmallRyeOpenApiProcessor {
private static final String META_INF_OPENAPI_JSON = "META-INF/openapi.json";
private static final String WEB_INF_CLASSES_META_INF_OPENAPI_JSON = "WEB-INF/classes/META-INF/openapi.json";

private static final DotName OPENAPI_SCHEMA = DotName.createSimple(Schema.class.getName());
private static final DotName OPENAPI_RESPONSE = DotName.createSimple(APIResponse.class.getName());
private static final DotName OPENAPI_RESPONSES = DotName.createSimple(APIResponses.class.getName());

private static final String OPENAPI_RESPONSE_CONTENT = "content";
private static final String OPENAPI_RESPONSE_SCHEMA = "schema";
private static final String OPENAPI_SCHEMA_NOT = "not";
private static final String OPENAPI_SCHEMA_ONE_OF = "oneOf";
private static final String OPENAPI_SCHEMA_ANY_OF = "anyOf";
private static final String OPENAPI_SCHEMA_ALL_OF = "allOf";
private static final String OPENAPI_SCHEMA_IMPLEMENTATION = "implementation";

SmallRyeOpenApiConfig openapi;

@ConfigRoot(name = "smallrye-openapi")
Expand Down Expand Up @@ -97,6 +120,90 @@ List<AdditionalBeanBuildItem> beans() {
new AdditionalBeanBuildItem(OpenApiDocumentProducer.class));
}

@BuildStep
public void registerOpenApiSchemaClassesForReflection(BuildProducer<ReflectiveClassBuildItem> reflectiveClass,
BuildProducer<ReflectiveHierarchyBuildItem> reflectiveHierarchy, CombinedIndexBuildItem combinedIndexBuildItem) {
IndexView index = combinedIndexBuildItem.getIndex();

// Generate reflection declaration from MP OpenAPI Schema definition
// They are needed for serialization.
Collection<AnnotationInstance> schemaAnnotationInstances = index.getAnnotations(OPENAPI_SCHEMA);
for (AnnotationInstance schemaAnnotationInstance : schemaAnnotationInstances) {
AnnotationTarget typeTarget = schemaAnnotationInstance.target();
if (typeTarget.kind() != AnnotationTarget.Kind.CLASS) {
continue;
}
reflectiveHierarchy
.produce(new ReflectiveHierarchyBuildItem(Type.create(typeTarget.asClass().name(), Type.Kind.CLASS)));
}

// Generate reflection declaration from MP OpenAPI APIResponse schema definition
// They are needed for serialization
Collection<AnnotationInstance> apiResponseAnnotationInstances = index.getAnnotations(OPENAPI_RESPONSE);
registerReflectionForApiResponseSchemaSerialization(reflectiveClass, reflectiveHierarchy,
apiResponseAnnotationInstances);

// Generate reflection declaration from MP OpenAPI APIResponses schema definition
// They are needed for serialization
Collection<AnnotationInstance> apiResponsesAnnotationInstances = index.getAnnotations(OPENAPI_RESPONSES);
for (AnnotationInstance apiResponsesAnnotationInstance : apiResponsesAnnotationInstances) {
AnnotationValue apiResponsesAnnotationValue = apiResponsesAnnotationInstance.value();
if (apiResponsesAnnotationValue == null) {
continue;
}
registerReflectionForApiResponseSchemaSerialization(reflectiveClass, reflectiveHierarchy,
Arrays.asList(apiResponsesAnnotationValue.asNestedArray()));
}
}

private void registerReflectionForApiResponseSchemaSerialization(BuildProducer<ReflectiveClassBuildItem> reflectiveClass,
BuildProducer<ReflectiveHierarchyBuildItem> reflectiveHierarchy,
Collection<AnnotationInstance> apiResponseAnnotationInstances) {
for (AnnotationInstance apiResponseAnnotationInstance : apiResponseAnnotationInstances) {
AnnotationInstance[] contents = apiResponseAnnotationInstance.value(OPENAPI_RESPONSE_CONTENT).asNestedArray();
if (contents == null) {
continue;
}
for (AnnotationInstance content : contents) {
AnnotationInstance schema = content.value(OPENAPI_RESPONSE_SCHEMA).asNested();
if (schema == null) {
continue;
}

AnnotationValue schemaImplementationClass = schema.value(OPENAPI_SCHEMA_IMPLEMENTATION);
if (schemaImplementationClass != null) {
reflectiveHierarchy.produce(new ReflectiveHierarchyBuildItem(schemaImplementationClass.asClass()));
}

AnnotationValue schemaNotClass = schema.value(OPENAPI_SCHEMA_NOT);
if (schemaNotClass != null) {
reflectiveClass.produce(new ReflectiveClassBuildItem(true, true, schemaNotClass.asString()));
}

AnnotationValue schemaOneOfClasses = schema.value(OPENAPI_SCHEMA_ONE_OF);
if (schemaOneOfClasses != null) {
for (Type schemaOneOfClass : schemaOneOfClasses.asClassArray()) {
reflectiveHierarchy.produce(new ReflectiveHierarchyBuildItem(schemaOneOfClass));
}
}

AnnotationValue schemaAnyOfClasses = schema.value(OPENAPI_SCHEMA_ANY_OF);
if (schemaAnyOfClasses != null) {
for (Type schemaAnyOfClass : schemaAnyOfClasses.asClassArray()) {
reflectiveHierarchy.produce(new ReflectiveHierarchyBuildItem(schemaAnyOfClass));
}
}

AnnotationValue schemaAllOfClasses = schema.value(OPENAPI_SCHEMA_ALL_OF);
if (schemaAllOfClasses != null) {
for (Type schemaAllOfClass : schemaAllOfClasses.asClassArray()) {
reflectiveHierarchy.produce(new ReflectiveHierarchyBuildItem(schemaAllOfClass));
}
}
}
}
}

@BuildStep
@Record(STATIC_INIT)
public BeanContainerListenerBuildItem build(SmallRyeOpenApiTemplate template,
Expand Down