use of io.swagger.v3.oas.models.PathItem in project swagger-parser by swagger-api.
the class InlineModelResolver method flattenPaths.
private void flattenPaths(Map<String, PathItem> paths) {
if (paths == null) {
return;
}
for (String pathname : paths.keySet()) {
PathItem path = paths.get(pathname);
for (Operation operation : path.readOperations()) {
flattenBody(pathname, operation.getRequestBody());
flattenParams(pathname, operation.getParameters());
flattenResponses(pathname, operation.getResponses());
}
}
}
use of io.swagger.v3.oas.models.PathItem 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);
}
}
}
}
}
}
}
use of io.swagger.v3.oas.models.PathItem in project swagger-parser by swagger-api.
the class PathsProcessor method processPaths.
public void processPaths() {
final Map<String, PathItem> pathMap = openAPI.getPaths();
if (pathMap == null) {
return;
}
for (String pathStr : pathMap.keySet()) {
PathItem pathItem = pathMap.get(pathStr);
addParametersToEachOperation(pathItem);
if (pathItem.get$ref() != null) {
PathItem resolvedPath = processReferencePath(pathItem);
String pathRef = pathItem.get$ref().split("#")[0];
if (resolvedPath != null) {
updateRefs(resolvedPath, pathRef);
// we need to put the resolved path into swagger object
openAPI.path(pathStr, resolvedPath);
pathItem = resolvedPath;
}
}
// at this point we can process this path
final List<Parameter> processedPathParameters = parameterProcessor.processParameters(pathItem.getParameters());
pathItem.setParameters(processedPathParameters);
final Map<PathItem.HttpMethod, Operation> operationMap = pathItem.readOperationsMap();
for (PathItem.HttpMethod httpMethod : operationMap.keySet()) {
Operation operation = operationMap.get(httpMethod);
operationProcessor.processOperation(operation);
}
}
}
use of io.swagger.v3.oas.models.PathItem in project swagger-parser by swagger-api.
the class OpenAPIResolverTest method testResponseRemoteRefs.
private void testResponseRemoteRefs(String remoteRef) {
final OpenAPI swagger = new OpenAPI();
swagger.path("/fun", new PathItem().get(new Operation().responses(new ApiResponses().addApiResponse("200", new ApiResponse().content(new Content().addMediaType("*/*", new MediaType().schema(new Schema().$ref(remoteRef))))))));
final OpenAPI resolved = new OpenAPIResolver(swagger, null).resolve();
final ApiResponse response = swagger.getPaths().get("/fun").getGet().getResponses().get("200");
final Schema ref = response.getContent().get("*/*").getSchema();
assertEquals(ref.get$ref(), "#/components/schemas/Tag");
assertNotNull(swagger.getComponents().getSchemas().get("Tag"));
}
use of io.swagger.v3.oas.models.PathItem in project swagger-parser by swagger-api.
the class InlineModelResolverTest method testInlineMapResponseWithObjectSchema.
@Test
public void testInlineMapResponseWithObjectSchema() throws Exception {
OpenAPI openAPI = new OpenAPI();
Schema schema = new Schema();
schema.setAdditionalProperties(new ObjectSchema().addProperties("name", new StringSchema()));
schema.addExtension("x-ext", "ext-prop");
ApiResponse apiResponse = new ApiResponse().description("it works!").content(new Content().addMediaType("*/*", new MediaType().schema(schema)));
apiResponse.addExtension("x-foo", "bar");
ApiResponses apiResponses = new ApiResponses().addApiResponse("200", apiResponse);
openAPI.path("/foo/baz", new PathItem().get(new Operation().responses(apiResponses)));
new InlineModelResolver().flatten(openAPI);
ApiResponse response = openAPI.getPaths().get("/foo/baz").getGet().getResponses().get("200");
Schema property = response.getContent().get("*/*").getSchema();
assertTrue(property.getAdditionalProperties() != null);
assertEquals(1, property.getExtensions().size());
assertEquals("ext-prop", property.getExtensions().get("x-ext"));
assertTrue(openAPI.getComponents().getSchemas().size() == 1);
Schema inline = openAPI.getComponents().getSchemas().get("inline_response_map200");
assertTrue(inline instanceof Schema);
assertNotNull(inline.getProperties().get("name"));
assertTrue(inline.getProperties().get("name") instanceof StringSchema);
}
Aggregations