Search in sources :

Example 56 with PathItem

use of io.swagger.v3.oas.models.PathItem in project swagger-core by swagger-api.

the class SpecFilter method filter.

public OpenAPI filter(OpenAPI openAPI, OpenAPISpecFilter filter, Map<String, List<String>> params, Map<String, String> cookies, Map<String, List<String>> headers) {
    OpenAPI filteredOpenAPI = filterOpenAPI(filter, openAPI, params, cookies, headers);
    if (filteredOpenAPI == null) {
        return filteredOpenAPI;
    }
    OpenAPI clone = new OpenAPI();
    clone.info(filteredOpenAPI.getInfo());
    clone.openapi(filteredOpenAPI.getOpenapi());
    clone.setExtensions(filteredOpenAPI.getExtensions());
    clone.setExternalDocs(filteredOpenAPI.getExternalDocs());
    clone.setSecurity(filteredOpenAPI.getSecurity());
    clone.setServers(filteredOpenAPI.getServers());
    clone.tags(filteredOpenAPI.getTags() == null ? null : new ArrayList<>(openAPI.getTags()));
    final Set<String> allowedTags = new HashSet<>();
    final Set<String> filteredTags = new HashSet<>();
    Paths clonedPaths = new Paths();
    if (filteredOpenAPI.getPaths() != null) {
        for (String resourcePath : filteredOpenAPI.getPaths().keySet()) {
            PathItem pathItem = filteredOpenAPI.getPaths().get(resourcePath);
            PathItem filteredPathItem = filterPathItem(filter, pathItem, resourcePath, params, cookies, headers);
            if (filteredPathItem != null) {
                PathItem clonedPathItem = new PathItem();
                clonedPathItem.set$ref(filteredPathItem.get$ref());
                clonedPathItem.setDescription(filteredPathItem.getDescription());
                clonedPathItem.setSummary(filteredPathItem.getSummary());
                clonedPathItem.setExtensions(filteredPathItem.getExtensions());
                clonedPathItem.setParameters(filteredPathItem.getParameters());
                clonedPathItem.setServers(filteredPathItem.getServers());
                Map<PathItem.HttpMethod, Operation> ops = filteredPathItem.readOperationsMap();
                for (Map.Entry<PathItem.HttpMethod, Operation> entry : ops.entrySet()) {
                    PathItem.HttpMethod key = entry.getKey();
                    Operation op = entry.getValue();
                    List<String> opTagsBeforeFilter = null;
                    if (op.getTags() != null) {
                        opTagsBeforeFilter = new ArrayList<>(op.getTags());
                    } else {
                        opTagsBeforeFilter = new ArrayList<>();
                    }
                    op = filterOperation(filter, op, resourcePath, key.toString(), params, cookies, headers);
                    clonedPathItem.operation(key, op);
                    if (op == null) {
                        filteredTags.addAll(opTagsBeforeFilter);
                    } else {
                        if (op.getTags() != null) {
                            opTagsBeforeFilter.removeAll(op.getTags());
                            allowedTags.addAll(op.getTags());
                        }
                        filteredTags.addAll(opTagsBeforeFilter);
                    }
                }
                if (!clonedPathItem.readOperations().isEmpty()) {
                    clonedPaths.addPathItem(resourcePath, clonedPathItem);
                }
            }
        }
        clone.paths(clonedPaths);
    }
    filteredTags.removeAll(allowedTags);
    final List<Tag> tags = clone.getTags();
    if (tags != null && !filteredTags.isEmpty()) {
        for (Iterator<Tag> it = tags.iterator(); it.hasNext(); ) {
            if (filteredTags.contains(it.next().getName())) {
                it.remove();
            }
        }
        if (clone.getTags().isEmpty()) {
            clone.setTags(null);
        }
    }
    if (filteredOpenAPI.getComponents() != null) {
        clone.components(new Components());
        clone.getComponents().setSchemas(filterComponentsSchema(filter, filteredOpenAPI.getComponents().getSchemas(), params, cookies, headers));
        clone.getComponents().setSecuritySchemes(filteredOpenAPI.getComponents().getSecuritySchemes());
        clone.getComponents().setCallbacks(filteredOpenAPI.getComponents().getCallbacks());
        clone.getComponents().setExamples(filteredOpenAPI.getComponents().getExamples());
        clone.getComponents().setExtensions(filteredOpenAPI.getComponents().getExtensions());
        clone.getComponents().setHeaders(filteredOpenAPI.getComponents().getHeaders());
        clone.getComponents().setLinks(filteredOpenAPI.getComponents().getLinks());
        clone.getComponents().setParameters(filteredOpenAPI.getComponents().getParameters());
        clone.getComponents().setRequestBodies(filteredOpenAPI.getComponents().getRequestBodies());
        clone.getComponents().setResponses(filteredOpenAPI.getComponents().getResponses());
    }
    if (filter.isRemovingUnreferencedDefinitions()) {
        clone = removeBrokenReferenceDefinitions(clone);
    }
    return clone;
}
Also used : ArrayList(java.util.ArrayList) Operation(io.swagger.v3.oas.models.Operation) Components(io.swagger.v3.oas.models.Components) PathItem(io.swagger.v3.oas.models.PathItem) Paths(io.swagger.v3.oas.models.Paths) Tag(io.swagger.v3.oas.models.tags.Tag) OpenAPI(io.swagger.v3.oas.models.OpenAPI) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 57 with PathItem

use of io.swagger.v3.oas.models.PathItem in project swagger-core by swagger-api.

the class ReaderTest method testResourceWithSubresources.

@Test(description = "test resource with subresources")
public void testResourceWithSubresources() {
    Reader reader = new Reader(new OpenAPI());
    OpenAPI openAPI = reader.read(ResourceWithSubResource.class);
    Paths paths = openAPI.getPaths();
    assertEquals(paths.size(), 3);
    PathItem pathItem = paths.get("/employees/{id}");
    assertNotNull(pathItem);
    Operation operation = pathItem.getGet();
    assertNotNull(operation);
    ArraySchema arraySchema = (ArraySchema) operation.getResponses().get("200").getContent().values().iterator().next().getSchema();
    assertNotNull(arraySchema);
    assertEquals(arraySchema.getItems().get$ref(), "#/components/schemas/Pet");
    pathItem = paths.get("/employees/{id}/{id}");
    assertNotNull(pathItem);
    operation = pathItem.getGet();
    assertNotNull(operation);
    Schema schema = operation.getResponses().get("200").getContent().values().iterator().next().getSchema();
    assertNotNull(schema);
    assertEquals(schema.get$ref(), "#/components/schemas/Pet");
    pathItem = paths.get("/employees/noPath");
    assertNotNull(pathItem);
    operation = pathItem.getGet();
    assertNotNull(operation);
    schema = operation.getResponses().getDefault().getContent().values().iterator().next().getSchema();
    assertNotNull(schema);
    assertEquals(schema.getType(), "string");
}
Also used : PathItem(io.swagger.v3.oas.models.PathItem) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) Schema(io.swagger.v3.oas.models.media.Schema) Paths(io.swagger.v3.oas.models.Paths) Operation(io.swagger.v3.oas.models.Operation) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Example 58 with PathItem

use of io.swagger.v3.oas.models.PathItem in project swagger-core by swagger-api.

the class ReaderTest method test2607.

@Test(description = "test ticket #2607 resource with subresources")
public void test2607() {
    Reader reader = new Reader(new OpenAPI());
    OpenAPI openAPI = reader.read(Test2607.class);
    Paths paths = openAPI.getPaths();
    assertEquals(paths.size(), 2);
    PathItem pathItem = paths.get("/swaggertest/name");
    assertNotNull(pathItem);
    Operation operation = pathItem.getGet();
    assertNotNull(operation);
    assertTrue(operation.getResponses().getDefault().getContent().keySet().contains("text/plain"));
    Schema schema = operation.getResponses().getDefault().getContent().values().iterator().next().getSchema();
    assertNotNull(schema);
    assertEquals(schema.getType(), "string");
    pathItem = paths.get("/swaggertest/subresource/version");
    assertNotNull(pathItem);
    operation = pathItem.getGet();
    assertNotNull(operation);
    assertTrue(operation.getResponses().getDefault().getContent().keySet().contains("text/plain"));
    schema = operation.getResponses().getDefault().getContent().values().iterator().next().getSchema();
    assertNotNull(schema);
    assertEquals(schema.getType(), "string");
}
Also used : PathItem(io.swagger.v3.oas.models.PathItem) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) Schema(io.swagger.v3.oas.models.media.Schema) Paths(io.swagger.v3.oas.models.Paths) Operation(io.swagger.v3.oas.models.Operation) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Example 59 with PathItem

use of io.swagger.v3.oas.models.PathItem in project snow-owl by b2ihealthcare.

the class FhirMetadataController method collectOperationDefinitions.

private Collection<OperationDefinition> collectOperationDefinitions(final OpenAPI openAPI) {
    final Paths paths = openAPI.getPaths();
    // Collect GET requests within the FHIR services hierarchy where the request path contains a '$' character
    return paths.entrySet().stream().filter(e -> {
        final String key = e.getKey();
        final PathItem value = e.getValue();
        return key.startsWith(config.getApiBaseUrl()) && key.contains("$") && (value.getGet() != null);
    }).map(e -> buildOperationDefinition(e.getKey(), e.getValue())).collect(Collectors.toList());
}
Also used : PathVariable(org.springframework.web.bind.annotation.PathVariable) UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) Iterables(com.google.common.collect.Iterables) OpenApiWebMvcResource(org.springdoc.webmvc.api.OpenApiWebMvcResource) Autowired(org.springframework.beans.factory.annotation.Autowired) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) Uri(com.b2international.snowowl.fhir.core.model.dt.Uri) SnowOwlOpenApiWebMvcResource(com.b2international.snowowl.core.rest.SnowOwlOpenApiWebMvcResource) OperationDefinition(com.b2international.snowowl.fhir.core.model.operationdefinition.OperationDefinition) Supplier(java.util.function.Supplier) FhirApiConfig(com.b2international.snowowl.core.rest.FhirApiConfig) Code(com.b2international.snowowl.fhir.core.model.dt.Code) Extension(io.swagger.v3.oas.annotations.extensions.Extension) Operation(io.swagger.v3.oas.annotations.Operation) StringUtils(com.b2international.commons.StringUtils) MvcUriComponentsBuilder(org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder) B2I_OPENAPI_PROFILE(com.b2international.snowowl.core.rest.OpenAPIExtensions.B2I_OPENAPI_PROFILE) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Map(java.util.Map) ApiResponse(io.swagger.v3.oas.annotations.responses.ApiResponse) GetMapping(org.springframework.web.bind.annotation.GetMapping) Suppliers(com.google.common.base.Suppliers) OperationKind(com.b2international.snowowl.fhir.core.codesystems.OperationKind) Schema(io.swagger.v3.oas.models.media.Schema) SnowOwlConfiguration(com.b2international.snowowl.core.config.SnowOwlConfiguration) NotFoundException(com.b2international.commons.exceptions.NotFoundException) Maps.newHashMap(com.google.common.collect.Maps.newHashMap) Collection(java.util.Collection) RestfulCapabilityMode(com.b2international.snowowl.fhir.core.codesystems.RestfulCapabilityMode) PathItem(io.swagger.v3.oas.models.PathItem) ExtensionProperty(io.swagger.v3.oas.annotations.extensions.ExtensionProperty) Paths(io.swagger.v3.oas.models.Paths) Collectors(java.util.stream.Collectors) RestController(org.springframework.web.bind.annotation.RestController) List(java.util.List) Tag(io.swagger.v3.oas.annotations.tags.Tag) CoreActivator(com.b2international.snowowl.core.CoreActivator) CapabilityStatementKind(com.b2international.snowowl.fhir.core.codesystems.CapabilityStatementKind) Platform(org.eclipse.core.runtime.Platform) Parameter(com.b2international.snowowl.fhir.core.model.operationdefinition.Parameter) B2I_OPENAPI_INTERACTION_READ(com.b2international.snowowl.core.rest.OpenAPIExtensions.B2I_OPENAPI_INTERACTION_READ) B2I_OPENAPI_X_NAME(com.b2international.snowowl.core.rest.OpenAPIExtensions.B2I_OPENAPI_X_NAME) PublicationStatus(com.b2international.snowowl.fhir.core.codesystems.PublicationStatus) Comparator(java.util.Comparator) ApplicationContext(com.b2international.snowowl.core.ApplicationContext) com.b2international.snowowl.fhir.core.model.capabilitystatement(com.b2international.snowowl.fhir.core.model.capabilitystatement) B2I_OPENAPI_X_INTERACTION(com.b2international.snowowl.core.rest.OpenAPIExtensions.B2I_OPENAPI_X_INTERACTION) ApiResponses(io.swagger.v3.oas.annotations.responses.ApiResponses) PathItem(io.swagger.v3.oas.models.PathItem) Paths(io.swagger.v3.oas.models.Paths)

Example 60 with PathItem

use of io.swagger.v3.oas.models.PathItem in project cxf by apache.

the class OpenApiCustomizer method customize.

public void customize(final OpenAPI oas) {
    if (replaceTags || javadocProvider != null) {
        Map<String, ClassResourceInfo> operations = new HashMap<>();
        Map<Pair<String, String>, OperationResourceInfo> methods = new HashMap<>();
        cris.forEach(cri -> {
            cri.getMethodDispatcher().getOperationResourceInfos().forEach(ori -> {
                String normalizedPath = getNormalizedPath(cri.getURITemplate().getValue(), ori.getURITemplate().getValue());
                operations.put(normalizedPath, cri);
                methods.put(Pair.of(ori.getHttpMethod(), normalizedPath), ori);
            });
        });
        List<Tag> tags = new ArrayList<>();
        oas.getPaths().forEach((pathKey, pathItem) -> {
            Optional<Tag> tag;
            if (replaceTags && operations.containsKey(pathKey)) {
                ClassResourceInfo cri = operations.get(pathKey);
                tag = Optional.of(new Tag());
                tag.get().setName(cri.getURITemplate().getValue().replaceAll("/", "_"));
                if (javadocProvider != null) {
                    tag.get().setDescription(javadocProvider.getClassDoc(cri));
                }
                if (!tags.contains(tag.get())) {
                    tags.add(tag.get());
                }
            } else {
                tag = Optional.empty();
            }
            pathItem.readOperationsMap().forEach((method, operation) -> {
                if (replaceTags && tag.isPresent()) {
                    operation.setTags(Collections.singletonList(tag.get().getName()));
                }
                Pair<String, String> key = Pair.of(method.name(), pathKey);
                if (methods.containsKey(key) && javadocProvider != null) {
                    OperationResourceInfo ori = methods.get(key);
                    if (StringUtils.isBlank(operation.getSummary())) {
                        operation.setSummary(javadocProvider.getMethodDoc(ori));
                    }
                    if (operation.getParameters() == null) {
                        List<Parameter> parameters = new ArrayList<>();
                        addParameters(parameters);
                        operation.setParameters(parameters);
                    }
                    for (int i = 0; i < operation.getParameters().size(); i++) {
                        if (StringUtils.isBlank(operation.getParameters().get(i).getDescription())) {
                            operation.getParameters().get(i).setDescription(extractJavadoc(operation, ori, i));
                        }
                    }
                    addParameters(operation.getParameters());
                    customizeResponses(operation, ori);
                }
            });
        });
        if (replaceTags && oas.getTags() != null) {
            oas.setTags(tags);
        }
    }
}
Also used : HashMap(java.util.HashMap) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) ArrayList(java.util.ArrayList) Parameter(io.swagger.v3.oas.models.parameters.Parameter) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo) Tag(io.swagger.v3.oas.models.tags.Tag) Pair(org.apache.commons.lang3.tuple.Pair)

Aggregations

PathItem (io.swagger.v3.oas.models.PathItem)66 Operation (io.swagger.v3.oas.models.Operation)52 OpenAPI (io.swagger.v3.oas.models.OpenAPI)50 Test (org.testng.annotations.Test)39 Paths (io.swagger.v3.oas.models.Paths)24 Map (java.util.Map)19 HashMap (java.util.HashMap)18 Schema (io.swagger.v3.oas.models.media.Schema)16 Components (io.swagger.v3.oas.models.Components)15 ApiResponse (io.swagger.v3.oas.models.responses.ApiResponse)14 ApiResponses (io.swagger.v3.oas.models.responses.ApiResponses)14 LinkedHashMap (java.util.LinkedHashMap)14 ArrayList (java.util.ArrayList)11 IntegerSchema (io.swagger.v3.oas.models.media.IntegerSchema)10 Parameter (io.swagger.v3.oas.models.parameters.Parameter)10 ArraySchema (io.swagger.v3.oas.models.media.ArraySchema)8 MediaType (io.swagger.v3.oas.models.media.MediaType)8 HttpMethod (io.swagger.models.HttpMethod)7 Callback (io.swagger.v3.oas.models.callbacks.Callback)7 Content (io.swagger.v3.oas.models.media.Content)7