use of io.swagger.v3.oas.annotations.responses.ApiResponse in project swagger-parser by swagger-api.
the class OpenAPIDeserializer method getResponse.
public ApiResponse getResponse(ObjectNode node, String location, ParseResult result) {
if (node == null) {
return null;
}
ApiResponse apiResponse = new ApiResponse();
JsonNode ref = node.get("$ref");
if (ref != null) {
if (ref.getNodeType().equals(JsonNodeType.STRING)) {
String mungedRef = mungedRef(ref.textValue());
if (mungedRef != null) {
apiResponse.set$ref(mungedRef);
} else {
apiResponse.set$ref(ref.textValue());
}
return apiResponse;
} else {
result.invalidType(location, "$ref", "string", node);
return null;
}
}
String value = getString("description", node, true, location, result);
if ((result.isAllowEmptyStrings() && value != null) || (!result.isAllowEmptyStrings() && !StringUtils.isBlank(value))) {
apiResponse.description(value);
}
ObjectNode headerObject = getObject("headers", node, false, location, result);
if (headerObject != null) {
Map<String, Header> headers = getHeaders(headerObject, location, result, false);
if (headers != null && headers.size() > 0) {
apiResponse.setHeaders(headers);
}
}
ObjectNode linksObj = getObject("links", node, false, location, result);
if (linksObj != null) {
Map<String, Link> links = getLinks(linksObj, location, result, false);
if (links != null && links.size() > 0) {
apiResponse.setLinks(links);
}
}
ObjectNode contentObject = getObject("content", node, false, location, result);
if (contentObject != null) {
apiResponse.setContent(getContent(contentObject, String.format("%s.%s", location, "content"), result));
}
Map<String, Object> extensions = getExtensions(node);
if (extensions != null && extensions.size() > 0) {
apiResponse.setExtensions(extensions);
}
Set<String> keys = getKeys(node);
for (String key : keys) {
if (!RESPONSE_KEYS.contains(key) && !key.startsWith("x-")) {
result.extra(location, key, node.get(key));
}
}
return apiResponse;
}
use of io.swagger.v3.oas.annotations.responses.ApiResponse in project swagger-parser by swagger-api.
the class NetworkReferenceTest method testIssue411.
@Test
public void testIssue411() throws Exception {
final List<AuthorizationValue> auths = new ArrayList<>();
AuthorizationValue auth = new AuthorizationValue("Authorization", "OMG_SO_SEKR3T", "header");
auths.add(auth);
new Expectations() {
{
remoteUrl.urlToString("http://remote1/resources/swagger.yaml", auths);
result = issue_411_server;
remoteUrl.urlToString("http://remote2/resources/foo", auths);
result = issue_411_components;
}
};
OpenAPIV3Parser parser = new OpenAPIV3Parser();
ParseOptions options = new ParseOptions();
options.setResolve(true);
SwaggerParseResult result = parser.readLocation("http://remote1/resources/swagger.yaml", auths, options);
OpenAPI swagger = result.getOpenAPI();
assertNotNull(swagger.getPaths().get("/health"));
PathItem health = swagger.getPaths().get("/health");
assertTrue(health.getGet().getParameters().size() == 0);
Schema responseRef = health.getGet().getResponses().get("200").getContent().get("*/*").getSchema();
assertTrue(responseRef.get$ref() != null);
assertEquals(responseRef.get$ref(), "#/components/schemas/Success");
assertNotNull(swagger.getComponents().getSchemas().get("Success"));
Parameter param = swagger.getPaths().get("/stuff").getGet().getParameters().get(0);
assertEquals(param.getIn(), "query");
assertEquals(param.getName(), "skip");
ApiResponse response = swagger.getPaths().get("/stuff").getGet().getResponses().get("200");
assertNotNull(response);
assertTrue(response.getContent().get("*/*").getSchema() instanceof StringSchema);
ApiResponse error = swagger.getPaths().get("/stuff").getGet().getResponses().get("400");
assertNotNull(error);
Schema errorProp = error.getContent().get("*/*").getSchema();
assertNotNull(errorProp);
assertTrue(errorProp.get$ref() != null);
assertEquals(errorProp.get$ref(), "#/components/schemas/Error");
assertTrue(swagger.getComponents().getSchemas().get("Error") instanceof Schema);
}
use of io.swagger.v3.oas.annotations.responses.ApiResponse in project swagger-parser by swagger-api.
the class ResolverFully method resolvePath.
public void resolvePath(PathItem pathItem) {
for (Operation op : pathItem.readOperations()) {
// inputs
if (op.getParameters() != null) {
for (Parameter parameter : op.getParameters()) {
parameter = parameter.get$ref() != null ? resolveParameter(parameter) : parameter;
if (parameter.getSchema() != null) {
Schema resolved = resolveSchema(parameter.getSchema());
if (resolved != null) {
parameter.setSchema(resolved);
}
}
if (parameter.getContent() != null) {
Map<String, MediaType> content = parameter.getContent();
for (String key : content.keySet()) {
if (content.get(key) != null && content.get(key).getSchema() != null) {
Schema resolvedSchema = resolveSchema(content.get(key).getSchema());
if (resolvedSchema != null) {
content.get(key).setSchema(resolvedSchema);
}
}
}
}
}
}
if (op.getCallbacks() != null) {
Map<String, Callback> callbacks = op.getCallbacks();
for (String name : callbacks.keySet()) {
Callback callback = callbacks.get(name);
callback = callback.get$ref() != null ? resolveCallback(callback) : callback;
if (callback != null) {
for (String callbackName : callback.keySet()) {
PathItem path = callback.get(callbackName);
if (path != null) {
resolvePath(path);
}
}
}
op.getCallbacks().put(name, callback);
}
}
RequestBody refRequestBody = op.getRequestBody();
if (refRequestBody != null) {
RequestBody requestBody = refRequestBody.get$ref() != null ? resolveRequestBody(refRequestBody) : refRequestBody;
op.setRequestBody(requestBody);
if (requestBody.getContent() != null) {
Map<String, MediaType> content = requestBody.getContent();
for (String key : content.keySet()) {
if (content.get(key) != null && content.get(key).getSchema() != null) {
Schema resolved = resolveSchema(content.get(key).getSchema());
if (resolved != null) {
content.get(key).setSchema(resolved);
}
}
}
}
}
// responses
ApiResponses responses = op.getResponses();
if (responses != null) {
for (String code : responses.keySet()) {
ApiResponse response = responses.get(code);
if (response.getContent() != null) {
Map<String, MediaType> content = response.getContent();
for (String mediaType : content.keySet()) {
if (content.get(mediaType).getSchema() != null) {
Schema resolved = resolveSchema(content.get(mediaType).getSchema());
response.getContent().get(mediaType).setSchema(resolved);
}
if (content.get(mediaType).getExamples() != null) {
Map<String, Example> resolved = resolveExample(content.get(mediaType).getExamples());
response.getContent().get(mediaType).setExamples(resolved);
}
}
}
resolveHeaders(response.getHeaders());
Map<String, Link> links = response.getLinks();
if (links != null) {
for (Map.Entry<String, Link> link : links.entrySet()) {
Link value = link.getValue();
Link resolvedValue = value.get$ref() != null ? resolveLink(value) : value;
link.setValue(resolvedValue);
}
}
}
}
}
}
use of io.swagger.v3.oas.annotations.responses.ApiResponse in project swagger-parser by swagger-api.
the class ExternalRefProcessor method processRefToExternalResponse.
public String processRefToExternalResponse(String $ref, RefFormat refFormat) {
String renamedRef = cache.getRenamedRef($ref);
if (renamedRef != null) {
return renamedRef;
}
final ApiResponse response = cache.loadRef($ref, refFormat, ApiResponse.class);
String newRef;
if (openAPI.getComponents() == null) {
openAPI.setComponents(new Components());
}
Map<String, ApiResponse> responses = openAPI.getComponents().getResponses();
if (responses == null) {
responses = new LinkedHashMap<>();
}
final String possiblyConflictingDefinitionName = computeDefinitionName($ref);
ApiResponse existingResponse = responses.get(possiblyConflictingDefinitionName);
if (existingResponse != null) {
LOGGER.debug("A model for " + existingResponse + " already exists");
if (existingResponse.get$ref() != null) {
// use the new model
existingResponse = null;
}
}
newRef = possiblyConflictingDefinitionName;
openAPI.getComponents().addResponses(newRef, response);
cache.putRenamedRef($ref, newRef);
if (response != null) {
if (response.getContent() != null) {
processRefContent(response.getContent(), $ref);
}
if (response.getHeaders() != null) {
processRefHeaders(response.getHeaders(), $ref);
}
if (response.getLinks() != null) {
processRefLinks(response.getLinks(), $ref);
}
}
return newRef;
}
use of io.swagger.v3.oas.annotations.responses.ApiResponse in project swagger-parser by swagger-api.
the class OperationProcessor method processOperation.
public void processOperation(Operation operation) {
final List<Parameter> processedOperationParameters = parameterProcessor.processParameters(operation.getParameters());
if (processedOperationParameters != null) {
operation.setParameters(processedOperationParameters);
}
final RequestBody requestBody = operation.getRequestBody();
if (requestBody != null) {
requestBodyProcessor.processRequestBody(requestBody);
}
final Map<String, ApiResponse> responses = operation.getResponses();
if (responses != null) {
for (String responseCode : responses.keySet()) {
ApiResponse response = responses.get(responseCode);
if (response != null) {
// This part allows parser to put response schema inline without the resolveFully option set to true
if (response.get$ref() != null) {
responseProcessor.processResponse(response);
RefFormat refFormat = computeRefFormat(response.get$ref());
ApiResponse resolvedResponse = cache.loadRef(response.get$ref(), refFormat, ApiResponse.class);
if (resolvedResponse != null) {
response = resolvedResponse;
responses.put(responseCode, resolvedResponse);
}
}
responseProcessor.processResponse(response);
}
}
}
final Map<String, Callback> callbacks = operation.getCallbacks();
if (callbacks != null) {
for (String name : callbacks.keySet()) {
Callback callback = callbacks.get(name);
if (callback != null) {
if (callback.get$ref() != null) {
String $ref = callback.get$ref();
RefFormat refFormat = computeRefFormat($ref);
if (isAnExternalRefFormat(refFormat)) {
final String newRef = externalRefProcessor.processRefToExternalCallback($ref, refFormat);
if (newRef != null) {
callback.set$ref(newRef);
}
}
}
for (String callbackName : callback.keySet()) {
PathItem pathItem = callback.get(callbackName);
final Map<PathItem.HttpMethod, Operation> operationMap = pathItem.readOperationsMap();
for (PathItem.HttpMethod httpMethod : operationMap.keySet()) {
Operation op = operationMap.get(httpMethod);
processOperation(op);
}
List<Parameter> parameters = pathItem.getParameters();
if (parameters != null) {
for (Parameter parameter : parameters) {
parameterProcessor.processParameter(parameter);
}
}
}
}
}
}
}
Aggregations