Class Enricher
- java.lang.Object
-
- de.aristaflow.ilm.ws.rest.base.service.openapi.Enricher
-
- All Implemented Interfaces:
io.swagger.v3.core.converter.ModelConverter
public class Enricher extends Object implements io.swagger.v3.core.converter.ModelConverter
This class handles enriching operations, parameters and types of the OpenAPI. It adds constraints stemming from validation annotations (fromjavax.validation.constraints
) as well asDefault
. This includes more annotations than already considered by Swagger (ParameterProcessor
).
Besides it enriches operations by adding responses declared via the rather simple annotationOperationResponse
. This also includes some sophisticated handling of the default response: Its schema may be referred to byOperationResponse
. The response considered as default by the OpenAPI generator will become the response for status codeHttpURLConnection.HTTP_OK
(orHttpURLConnection.HTTP_NO_CONTENT
) if there is no explicit response declared for this status code.
An exceptional response for server errors will be added for code "5XX" containing anIlmException
.This
Enricher
also adapts type resolution forbyte[]
used in operations andString[].class
used in schema declarations. The first prevent Swagger from creating an array of strings representing bytes; the string is already a container type, so the surrounding array is nonsense.
The second adaption allows to use array classes within the properties ofSchema
expectingClass
. Without that adaption, such classes would be ignored. This enricher just adds an additionalArraySchema
to the array type (e. g.String[]
) leading to a correct JSON array.
-
-
Field Summary
Fields Modifier and Type Field Description protected static String
DEFAULT_SERVER_ERROR_RESPONSE
The name of the exceptional response which becomes the default server error response.static int
DEFAULT_SERVER_ERROR_RESPONSE_CODE
The status code of the exceptional response which becomes the default server error response.protected Map<String,String>
defValueRepl
The map containing variables to replace in default values of schemas.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description io.swagger.v3.oas.models.Operation
addDeclaredResponses(io.swagger.v3.oas.models.Operation op, Method method, io.swagger.v3.oas.annotations.responses.ApiResponse[] classResponses, Function<io.swagger.v3.oas.annotations.responses.ApiResponse[],io.swagger.v3.oas.models.Operation> createOp)
Adds theOperationResponse
s declared for the designated method and the default exceptional response to the designated operation and additionally enriches the schema of the designated default response with validation annotations as well asDefault
from the designated method.protected void
applyDefaultResponseSchema(io.swagger.v3.oas.models.Operation op, io.swagger.v3.oas.models.media.Schema<?> defSchema, List<? extends de.aristaflow.adept2.model.communication.webservice.OperationResponse> declaredResponses)
Applies the schema of the designated default response for to all responses that refer to it.protected void
applyValidationAnnotations(Annotation[] annots, io.swagger.v3.oas.models.media.Schema<?> schema)
Applies validation annotations (fromjavax.validation.constraints
) as well asDefault
from the designated annotation collection to the designated schema.protected <T extends Annotation>
TcreateAnnotationInstance(Class<T> annotClass, Map<String,Object> nonDefValues)
Creates a new instance of the designated annotation class.protected Pair<Class<?>[],List<Class<?>>>
distinguishClasses(Class<?>[] value)
Distinguishes the classes from the designated array, that is, the first element will contain all normal classes and allThrowable
which are (subclasses of)IlmException
s.protected List<de.aristaflow.adept2.model.communication.webservice.OperationResponse>
getDeclaredResponses(Method m)
Gets allOperationResponse
s declared for the designated method as well as the default exceptional response, that is a response containing a plainIlmException
with status code5XX
.protected String
getDescription(String description, List<Class<?>> causedBy)
Gets the description for a response declaration.protected Pair<io.swagger.v3.oas.annotations.responses.ApiResponse[],Boolean>
joinResponses(Collection<? extends de.aristaflow.adept2.model.communication.webservice.OperationResponse> declaredResponses, String defMediaType, io.swagger.v3.oas.annotations.responses.ApiResponse[] classResponses)
Creates instances ofApiResponse
for the designated declaredOperationResponse
s and joins them with the designatedApiResponse
s declared for the class.io.swagger.v3.oas.models.media.Schema
resolve(io.swagger.v3.core.converter.AnnotatedType type, io.swagger.v3.core.converter.ModelConverterContext context, Iterator<io.swagger.v3.core.converter.ModelConverter> chain)
-
-
-
Field Detail
-
DEFAULT_SERVER_ERROR_RESPONSE_CODE
public static final int DEFAULT_SERVER_ERROR_RESPONSE_CODE
The status code of the exceptional response which becomes the default server error response.- See Also:
- Constant Field Values
-
DEFAULT_SERVER_ERROR_RESPONSE
protected static final String DEFAULT_SERVER_ERROR_RESPONSE
The name of the exceptional response which becomes the default server error response.- See Also:
- Constant Field Values
-
-
Method Detail
-
addDeclaredResponses
public io.swagger.v3.oas.models.Operation addDeclaredResponses(io.swagger.v3.oas.models.Operation op, Method method, io.swagger.v3.oas.annotations.responses.ApiResponse[] classResponses, Function<io.swagger.v3.oas.annotations.responses.ApiResponse[],io.swagger.v3.oas.models.Operation> createOp)
Adds theOperationResponse
s declared for the designated method and the default exceptional response to the designated operation and additionally enriches the schema of the designated default response with validation annotations as well asDefault
from the designated method.- Parameters:
op
- The operation to which to add theOperationResponse
s.method
- The method implementing the operation and possibly providingOperationResponse
s.classResponses
- The responses declared for the class. These will be added to the operation and the same mechanism will be used to add theOperationResponse
s.createOp
- A function creating a new operation having the designatedclassResponses
and the additionally declared and transformedOperationResponse
s as input.- Returns:
- A new operation having the additional
OperationResponse
s declared for the designated method.
-
getDeclaredResponses
protected List<de.aristaflow.adept2.model.communication.webservice.OperationResponse> getDeclaredResponses(Method m)
Gets allOperationResponse
s declared for the designated method as well as the default exceptional response, that is a response containing a plainIlmException
with status code5XX
.- Parameters:
m
- The method for which to get the declaredOperationResponse
s.- Returns:
- All
OperationResponse
s declared for the designated method including the default exceptional response (5xx
). This contains at least the default exceptional response.
-
applyDefaultResponseSchema
protected void applyDefaultResponseSchema(io.swagger.v3.oas.models.Operation op, io.swagger.v3.oas.models.media.Schema<?> defSchema, List<? extends de.aristaflow.adept2.model.communication.webservice.OperationResponse> declaredResponses)
Applies the schema of the designated default response for to all responses that refer to it.- Parameters:
op
- The operation for which apply the default response schema.defSchema
- The schema of the default response. If this isnull
the default schema is treated as null schema, that is,void
.declaredResponses
- The list of declared responses. Usages of theOperationResponse.DefaultResponsePlaceholder
are searched within this list to replace the corresponding schema in the responses of the designated operation.- See Also:
OperationResponse.DefaultResponsePlaceholder
-
joinResponses
protected Pair<io.swagger.v3.oas.annotations.responses.ApiResponse[],Boolean> joinResponses(Collection<? extends de.aristaflow.adept2.model.communication.webservice.OperationResponse> declaredResponses, String defMediaType, io.swagger.v3.oas.annotations.responses.ApiResponse[] classResponses)
Creates instances ofApiResponse
for the designated declaredOperationResponse
s and joins them with the designatedApiResponse
s declared for the class. This simply transforms the simpleOperationResponse
s into the corresponding full-fledgedApiResponse
s. If anOperationResponse
contains several types, these will be anSchema.oneOf()
composed schema, that is, the response body will have only one of these types.- Parameters:
declaredResponses
- The declared responses which to transform toApiResponse
s. This must neither benull
nor an empty collection.defMediaType
- The media type of the default response.classResponses
- TheApiResponse
s which are declared at the corresponding class. These will be added to theApiResponse
s created for the designatedOperationResponse
s.- Returns:
- An array containing the designated
ApiResponse
s from the class as well as the correspondingOperationResponse
s transformed appropriately. The second element of the pair indicates whether one declared response replaces the default response. - See Also:
OperationResponse
-
distinguishClasses
protected Pair<Class<?>[],List<Class<?>>> distinguishClasses(Class<?>[] value)
Distinguishes the classes from the designated array, that is, the first element will contain all normal classes and allThrowable
which are (subclasses of)IlmException
s.Throwable
s which are not (subclasses of)de.aristaflow.ilm.util.Exception}
s will be wrapped, that is,WrappingException
will be added to the first element while the actual class of theThrowable
will be added to the second element.
At the end the first element will contain all classes that may be serialised while the second element will contain allThrowable
classes that will be wrapped at runtime. TheWrappingException
will only be once it the first element even if there are several wrappedThrowable
s.- Parameters:
value
- The classes declared byOperationResponse
which need to be separated into real results andThrowable
s which will be wrapped at runtime.- Returns:
- The classes declared by
OperationResponse
which are actually within the response and theThrowable
s that will be wrapped at runtime.
-
createAnnotationInstance
protected <T extends Annotation> T createAnnotationInstance(Class<T> annotClass, Map<String,Object> nonDefValues)
Creates a new instance of the designated annotation class. The designated map provides all values for the annotation instance that differ from the default values. It maps from field/method name to the corresponding value. Make sure that the value has the right type, which is the one defined in the annotation!- Parameters:
annotClass
- The annotation class for which to create an instance.nonDefValues
- The values for the annotation instance that differ from the default values of the annotation. This maps from field/method name to the corresponding value.- Returns:
- A new instance (proxy) of the designated annotation class having the designated (non-default) values.
-
getDescription
protected String getDescription(String description, List<Class<?>> causedBy)
Gets the description for a response declaration. This is either the designated description or a generated description explaining the designated exceptions as causes for aWrappingException
.- Parameters:
description
- The description as declared by theOperationResponse
.causedBy
- The list of classes which areThrowable
s that will be wrapped at runtime.- Returns:
- The designated description or a description explaining the designated causes for a wrapping exception.
-
applyValidationAnnotations
protected void applyValidationAnnotations(Annotation[] annots, io.swagger.v3.oas.models.media.Schema<?> schema)
Applies validation annotations (fromjavax.validation.constraints
) as well asDefault
from the designated annotation collection to the designated schema.
A minimal or maximal size will be set appropriately for normal and array schemas. The minimal size will be set to 1 in case ofNotEmpty
(and no explicit minimal size).- Parameters:
annots
- The annotations which to apply to the designated schemas if they contain validation annotations. This may benull
or empty.schema
- The schema to which to apply validation annotations from the designated annotations. This may benull
.
-
resolve
public io.swagger.v3.oas.models.media.Schema resolve(io.swagger.v3.core.converter.AnnotatedType type, io.swagger.v3.core.converter.ModelConverterContext context, Iterator<io.swagger.v3.core.converter.ModelConverter> chain)
- Specified by:
resolve
in interfaceio.swagger.v3.core.converter.ModelConverter
-
-