use of io.swagger.v3.oas.models.callbacks.Callback in project swagger-core by swagger-api.
the class CallbackDeserializer method deserialize.
@Override
public Callback deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
final ObjectMapper mapper;
if (openapi31) {
mapper = Json31.mapper();
} else {
mapper = Json.mapper();
}
Callback result = new Callback();
JsonNode node = jp.getCodec().readTree(jp);
ObjectNode objectNode = (ObjectNode) node;
Map<String, Object> extensions = new LinkedHashMap<>();
for (Iterator<String> it = objectNode.fieldNames(); it.hasNext(); ) {
String childName = it.next();
JsonNode child = objectNode.get(childName);
// if name start with `x-` consider it an extension
if (childName.startsWith("x-")) {
extensions.put(childName, mapper.convertValue(child, Object.class));
} else if (childName.equals("$ref")) {
result.$ref(child.asText());
} else {
result.put(childName, mapper.convertValue(child, PathItem.class));
}
}
if (!extensions.isEmpty()) {
result.setExtensions(extensions);
}
return result;
}
use of io.swagger.v3.oas.models.callbacks.Callback in project swagger-core by swagger-api.
the class Ticket2926Test method testExtensionsInMapDeserializeAndSerialize.
@Test
public void testExtensionsInMapDeserializeAndSerialize() throws Exception {
String yaml = "openapi: 3.0.1\n" + "info:\n" + " title: My title\n" + " description: API under test\n" + " version: 1.0.7\n" + " x-info: test\n" + "servers:\n" + "- url: http://localhost:9999/api\n" + " x-server: test\n" + " description: desc\n" + " variables: \n" + " serVar: \n" + " description: desc\n" + " x-serverVariable: test\n" + "paths:\n" + " /foo/bar:\n" + " get:\n" + " callbacks:\n" + " /foo/bar:\n" + " get:\n" + " description: getoperation\n" + " x-callback: test\n" + " responses:\n" + " default:\n" + " description: it works!\n" + " content:\n" + " application/json:\n" + " schema:\n" + " title: inline_response_200\n" + " type: object\n" + " properties:\n" + " name:\n" + " type: string\n" + " x-mediatype: test\n" + " x-response: test\n" + " x-responses: test\n" + " x-responses-object: \n" + " aaa: bbb\n" + " x-responses-array: \n" + " - aaa\n" + " - bbb\n" + " x-operation: test\n" + " x-pathitem: test\n" + " x-paths: test\n" + "x-openapi-object: \n" + " aaa: bbb\n" + "x-openapi-array: \n" + " - aaa\n" + " - bbb\n" + "x-openapi: test";
OpenAPI aa = Yaml.mapper().readValue(yaml, OpenAPI.class);
SerializationMatchers.assertEqualsToYaml(aa, yaml);
}
use of io.swagger.v3.oas.models.callbacks.Callback in project swagger-core by swagger-api.
the class ReaderTest method testCallbackWithRef.
@Test(description = "Callback with Ref")
public void testCallbackWithRef() {
Components components = new Components();
components.addCallbacks("Callback", new Callback().addPathItem("/post", new PathItem().description("Post Path Item")));
OpenAPI oas = new OpenAPI().info(new Info().description("info")).components(components);
Reader reader = new Reader(oas);
OpenAPI openAPI = reader.read(RefCallbackResource.class);
String yaml = "openapi: 3.0.1\n" + "info:\n" + " description: info\n" + "paths:\n" + " /simplecallback:\n" + " get:\n" + " summary: Simple get operation\n" + " operationId: getWithNoParameters\n" + " responses:\n" + " \"200\":\n" + " description: voila!\n" + " callbacks:\n" + " testCallback1:\n" + " $ref: '#/components/callbacks/Callback'\n" + "components:\n" + " callbacks:\n" + " Callback:\n" + " /post:\n" + " description: Post Path Item\n";
SerializationMatchers.assertEqualsToYaml(openAPI, yaml);
}
use of io.swagger.v3.oas.models.callbacks.Callback in project swagger-core by swagger-api.
the class Reader method getCallbacks.
private Map<String, Callback> getCallbacks(io.swagger.v3.oas.annotations.callbacks.Callback apiCallback, Produces methodProduces, Produces classProduces, Consumes methodConsumes, Consumes classConsumes, JsonView jsonViewAnnotation) {
Map<String, Callback> callbackMap = new HashMap<>();
if (apiCallback == null) {
return callbackMap;
}
Callback callbackObject = new Callback();
if (StringUtils.isNotBlank(apiCallback.ref())) {
callbackObject.set$ref(apiCallback.ref());
callbackMap.put(apiCallback.name(), callbackObject);
return callbackMap;
}
PathItem pathItemObject = new PathItem();
for (io.swagger.v3.oas.annotations.Operation callbackOperation : apiCallback.operation()) {
Operation callbackNewOperation = new Operation();
setOperationObjectFromApiOperationAnnotation(callbackNewOperation, callbackOperation, methodProduces, classProduces, methodConsumes, classConsumes, jsonViewAnnotation);
setPathItemOperation(pathItemObject, callbackOperation.method(), callbackNewOperation);
}
callbackObject.addPathItem(apiCallback.callbackUrlExpression(), pathItemObject);
callbackMap.put(apiCallback.name(), callbackObject);
return callbackMap;
}
use of io.swagger.v3.oas.models.callbacks.Callback in project swagger-core by swagger-api.
the class Reader method parseMethod.
protected Operation parseMethod(Class<?> cls, Method method, List<Parameter> globalParameters, Produces methodProduces, Produces classProduces, Consumes methodConsumes, Consumes classConsumes, List<SecurityRequirement> classSecurityRequirements, Optional<io.swagger.v3.oas.models.ExternalDocumentation> classExternalDocs, Set<String> classTags, List<io.swagger.v3.oas.models.servers.Server> classServers, boolean isSubresource, RequestBody parentRequestBody, ApiResponses parentResponses, JsonView jsonViewAnnotation, io.swagger.v3.oas.annotations.responses.ApiResponse[] classResponses, AnnotatedMethod annotatedMethod) {
Operation operation = new Operation();
io.swagger.v3.oas.annotations.Operation apiOperation = ReflectionUtils.getAnnotation(method, io.swagger.v3.oas.annotations.Operation.class);
List<io.swagger.v3.oas.annotations.security.SecurityRequirement> apiSecurity = ReflectionUtils.getRepeatableAnnotations(method, io.swagger.v3.oas.annotations.security.SecurityRequirement.class);
List<io.swagger.v3.oas.annotations.callbacks.Callback> apiCallbacks = ReflectionUtils.getRepeatableAnnotations(method, io.swagger.v3.oas.annotations.callbacks.Callback.class);
List<Server> apiServers = ReflectionUtils.getRepeatableAnnotations(method, Server.class);
List<io.swagger.v3.oas.annotations.tags.Tag> apiTags = ReflectionUtils.getRepeatableAnnotations(method, io.swagger.v3.oas.annotations.tags.Tag.class);
List<io.swagger.v3.oas.annotations.Parameter> apiParameters = ReflectionUtils.getRepeatableAnnotations(method, io.swagger.v3.oas.annotations.Parameter.class);
List<io.swagger.v3.oas.annotations.responses.ApiResponse> apiResponses = ReflectionUtils.getRepeatableAnnotations(method, io.swagger.v3.oas.annotations.responses.ApiResponse.class);
io.swagger.v3.oas.annotations.parameters.RequestBody apiRequestBody = ReflectionUtils.getAnnotation(method, io.swagger.v3.oas.annotations.parameters.RequestBody.class);
ExternalDocumentation apiExternalDocumentation = ReflectionUtils.getAnnotation(method, ExternalDocumentation.class);
// callbacks
Map<String, Callback> callbacks = new LinkedHashMap<>();
if (apiCallbacks != null) {
for (io.swagger.v3.oas.annotations.callbacks.Callback methodCallback : apiCallbacks) {
Map<String, Callback> currentCallbacks = getCallbacks(methodCallback, methodProduces, classProduces, methodConsumes, classConsumes, jsonViewAnnotation);
callbacks.putAll(currentCallbacks);
}
}
if (callbacks.size() > 0) {
operation.setCallbacks(callbacks);
}
// security
classSecurityRequirements.forEach(operation::addSecurityItem);
if (apiSecurity != null) {
Optional<List<SecurityRequirement>> requirementsObject = SecurityParser.getSecurityRequirements(apiSecurity.toArray(new io.swagger.v3.oas.annotations.security.SecurityRequirement[apiSecurity.size()]));
if (requirementsObject.isPresent()) {
requirementsObject.get().stream().filter(r -> operation.getSecurity() == null || !operation.getSecurity().contains(r)).forEach(operation::addSecurityItem);
}
}
// servers
if (classServers != null) {
classServers.forEach(operation::addServersItem);
}
if (apiServers != null) {
AnnotationsUtils.getServers(apiServers.toArray(new Server[apiServers.size()])).ifPresent(servers -> servers.forEach(operation::addServersItem));
}
// external docs
AnnotationsUtils.getExternalDocumentation(apiExternalDocumentation).ifPresent(operation::setExternalDocs);
// method tags
if (apiTags != null) {
apiTags.stream().filter(t -> operation.getTags() == null || (operation.getTags() != null && !operation.getTags().contains(t.name()))).map(io.swagger.v3.oas.annotations.tags.Tag::name).forEach(operation::addTagsItem);
AnnotationsUtils.getTags(apiTags.toArray(new io.swagger.v3.oas.annotations.tags.Tag[apiTags.size()]), true).ifPresent(tags -> openApiTags.addAll(tags));
}
// parameters
if (globalParameters != null) {
for (Parameter globalParameter : globalParameters) {
operation.addParametersItem(globalParameter);
}
}
if (apiParameters != null) {
getParametersListFromAnnotation(apiParameters.toArray(new io.swagger.v3.oas.annotations.Parameter[apiParameters.size()]), classConsumes, methodConsumes, operation, jsonViewAnnotation).ifPresent(p -> p.forEach(operation::addParametersItem));
}
// RequestBody in Method
if (apiRequestBody != null && operation.getRequestBody() == null) {
OperationParser.getRequestBody(apiRequestBody, classConsumes, methodConsumes, components, jsonViewAnnotation).ifPresent(operation::setRequestBody);
}
// operation id
if (StringUtils.isBlank(operation.getOperationId())) {
operation.setOperationId(getOperationId(method.getName()));
}
// classResponses
if (classResponses != null && classResponses.length > 0) {
OperationParser.getApiResponses(classResponses, classProduces, methodProduces, components, jsonViewAnnotation).ifPresent(responses -> {
if (operation.getResponses() == null) {
operation.setResponses(responses);
} else {
responses.forEach(operation.getResponses()::addApiResponse);
}
});
}
if (apiOperation != null) {
setOperationObjectFromApiOperationAnnotation(operation, apiOperation, methodProduces, classProduces, methodConsumes, classConsumes, jsonViewAnnotation);
}
// apiResponses
if (apiResponses != null && !apiResponses.isEmpty()) {
OperationParser.getApiResponses(apiResponses.toArray(new io.swagger.v3.oas.annotations.responses.ApiResponse[apiResponses.size()]), classProduces, methodProduces, components, jsonViewAnnotation).ifPresent(responses -> {
if (operation.getResponses() == null) {
operation.setResponses(responses);
} else {
responses.forEach(operation.getResponses()::addApiResponse);
}
});
}
// class tags after tags defined as field of @Operation
if (classTags != null) {
classTags.stream().filter(t -> operation.getTags() == null || (operation.getTags() != null && !operation.getTags().contains(t))).forEach(operation::addTagsItem);
}
// external docs of class if not defined in annotation of method or as field of Operation annotation
if (operation.getExternalDocs() == null) {
classExternalDocs.ifPresent(operation::setExternalDocs);
}
// if subresource, merge parent requestBody
if (isSubresource && parentRequestBody != null) {
if (operation.getRequestBody() == null) {
operation.requestBody(parentRequestBody);
} else {
Content content = operation.getRequestBody().getContent();
if (content == null) {
content = parentRequestBody.getContent();
operation.getRequestBody().setContent(content);
} else if (parentRequestBody.getContent() != null) {
for (String parentMediaType : parentRequestBody.getContent().keySet()) {
if (content.get(parentMediaType) == null) {
content.addMediaType(parentMediaType, parentRequestBody.getContent().get(parentMediaType));
}
}
}
}
}
// handle return type, add as response in case.
Type returnType = method.getGenericReturnType();
if (annotatedMethod != null && annotatedMethod.getType() != null) {
returnType = annotatedMethod.getType();
}
final Class<?> subResource = getSubResourceWithJaxRsSubresourceLocatorSpecs(method);
Schema returnTypeSchema = null;
if (!shouldIgnoreClass(returnType.getTypeName()) && !method.getGenericReturnType().equals(subResource)) {
ResolvedSchema resolvedSchema = ModelConverters.getInstance().resolveAsResolvedSchema(new AnnotatedType(returnType).resolveAsRef(true).jsonViewAnnotation(jsonViewAnnotation));
if (resolvedSchema.schema != null) {
returnTypeSchema = resolvedSchema.schema;
Content content = new Content();
MediaType mediaType = new MediaType().schema(returnTypeSchema);
AnnotationsUtils.applyTypes(classProduces == null ? new String[0] : classProduces.value(), methodProduces == null ? new String[0] : methodProduces.value(), content, mediaType);
if (operation.getResponses() == null) {
operation.responses(new ApiResponses()._default(new ApiResponse().description(DEFAULT_DESCRIPTION).content(content)));
}
if (operation.getResponses().getDefault() != null && StringUtils.isBlank(operation.getResponses().getDefault().get$ref())) {
if (operation.getResponses().getDefault().getContent() == null) {
operation.getResponses().getDefault().content(content);
} else {
for (String key : operation.getResponses().getDefault().getContent().keySet()) {
if (operation.getResponses().getDefault().getContent().get(key).getSchema() == null) {
operation.getResponses().getDefault().getContent().get(key).setSchema(returnTypeSchema);
}
}
}
}
Map<String, Schema> schemaMap = resolvedSchema.referencedSchemas;
if (schemaMap != null) {
schemaMap.forEach((key, schema) -> components.addSchemas(key, schema));
}
}
}
if (operation.getResponses() == null || operation.getResponses().isEmpty()) {
Content content = resolveEmptyContent(classProduces, methodProduces);
ApiResponse apiResponseObject = new ApiResponse().description(DEFAULT_DESCRIPTION).content(content);
operation.setResponses(new ApiResponses()._default(apiResponseObject));
}
if (returnTypeSchema != null) {
resolveResponseSchemaFromReturnType(operation, classResponses, returnTypeSchema, classProduces, methodProduces);
if (apiResponses != null) {
resolveResponseSchemaFromReturnType(operation, apiResponses.stream().toArray(io.swagger.v3.oas.annotations.responses.ApiResponse[]::new), returnTypeSchema, classProduces, methodProduces);
}
}
return operation;
}
Aggregations