Search in sources :

Example 46 with Paths

use of io.swagger.v3.oas.models.Paths in project carbon-apimgt by wso2.

the class OAS3Parser method injectMgwThrottlingExtensionsToDefault.

/**
 * This method returns openAPI definition which replaced X-WSO2-throttling-tier extension comes from
 * mgw with X-throttling-tier extensions in openAPI file(openAPI version 3)
 *
 * @param swaggerContent String
 * @return String
 * @throws APIManagementException
 */
@Override
public String injectMgwThrottlingExtensionsToDefault(String swaggerContent) throws APIManagementException {
    OpenAPI openAPI = getOpenAPI(swaggerContent);
    Paths paths = openAPI.getPaths();
    for (String pathKey : paths.keySet()) {
        Map<PathItem.HttpMethod, Operation> operationsMap = paths.get(pathKey).readOperationsMap();
        for (Map.Entry<PathItem.HttpMethod, Operation> entry : operationsMap.entrySet()) {
            Operation operation = entry.getValue();
            Map<String, Object> extensions = operation.getExtensions();
            if (extensions != null && extensions.containsKey(APIConstants.X_WSO2_THROTTLING_TIER)) {
                Object tier = extensions.get(APIConstants.X_WSO2_THROTTLING_TIER);
                extensions.remove(APIConstants.X_WSO2_THROTTLING_TIER);
                extensions.put(APIConstants.SWAGGER_X_THROTTLING_TIER, tier);
            }
        }
    }
    return Json.pretty(openAPI);
}
Also used : JSONObject(org.json.simple.JSONObject) Paths(io.swagger.v3.oas.models.Paths) Operation(io.swagger.v3.oas.models.Operation) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) HttpMethod(io.swagger.models.HttpMethod)

Example 47 with Paths

use of io.swagger.v3.oas.models.Paths in project flow by vaadin.

the class AbstractEndpointGenerationTest method assertClassPathsRecursive.

private int assertClassPathsRecursive(Paths actualPaths, Class<?> testEndpointClass, Class<?> testMethodsClass, HashMap<String, Class<?>> typeArguments) {
    if (!testMethodsClass.equals(testEndpointClass) && !testMethodsClass.isAnnotationPresent(EndpointExposed.class)) {
        return 0;
    }
    int pathCount = 0;
    for (Method expectedEndpointMethod : testMethodsClass.getDeclaredMethods()) {
        if (!Modifier.isPublic(expectedEndpointMethod.getModifiers()) || accessChecker.getAccessAnnotationChecker().getSecurityTarget(expectedEndpointMethod).isAnnotationPresent(DenyAll.class)) {
            continue;
        }
        pathCount++;
        String expectedEndpointUrl = String.format("/%s/%s", getEndpointName(testEndpointClass), expectedEndpointMethod.getName());
        PathItem actualPath = actualPaths.get(expectedEndpointUrl);
        assertNotNull(String.format("Expected to find a path '%s' for the endpoint method '%s' in the class '%s'", expectedEndpointUrl, expectedEndpointMethod, testEndpointClass), actualPath);
        assertPath(testEndpointClass, expectedEndpointMethod, actualPath, typeArguments);
    }
    Type genericSuperClass = testMethodsClass.getGenericSuperclass();
    pathCount += assertClassPathsRecursive(actualPaths, testEndpointClass, applyTypeArguments(genericSuperClass, typeArguments), extractTypeArguments(genericSuperClass, typeArguments));
    for (Type genericInterface : testMethodsClass.getGenericInterfaces()) {
        pathCount += assertClassPathsRecursive(actualPaths, testEndpointClass, applyTypeArguments(genericInterface, typeArguments), extractTypeArguments(genericInterface, typeArguments));
    }
    return pathCount;
}
Also used : PathItem(io.swagger.v3.oas.models.PathItem) DenyAll(javax.annotation.security.DenyAll) GenericArrayType(java.lang.reflect.GenericArrayType) Type(java.lang.reflect.Type) ParameterizedType(java.lang.reflect.ParameterizedType) Method(java.lang.reflect.Method) Endpoint(dev.hilla.Endpoint)

Example 48 with Paths

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

the class FhirMetadataController method collectResources.

private Collection<Resource> collectResources(final OpenAPI openAPI) {
    final Paths paths = openAPI.getPaths();
    final List<io.swagger.v3.oas.models.tags.Tag> tags = openAPI.getTags();
    return tags.stream().filter(t -> t.getExtensions() != null && t.getExtensions().containsKey(B2I_OPENAPI_X_NAME)).map(t -> {
        final Map<?, ?> nameExtensionMap = (Map<?, ?>) t.getExtensions().get(B2I_OPENAPI_X_NAME);
        final String profile = (String) nameExtensionMap.get(B2I_OPENAPI_PROFILE);
        final Resource.Builder resourceBuilder = Resource.builder().type(t.getName()).profile(profile);
        // Collect the operations that belong to the same tagged class resource
        paths.values().stream().flatMap(pi -> pi.readOperations().stream()).filter(o -> o.getTags().contains(t.getName()) && o.getExtensions() != null && o.getExtensions().containsKey(B2I_OPENAPI_X_INTERACTION)).forEachOrdered(op -> {
            final Map<String, Object> operationExtensionMap = op.getExtensions();
            final Map<?, ?> interactionMap = (Map<?, ?>) operationExtensionMap.get(B2I_OPENAPI_X_INTERACTION);
            interactionMap.entrySet().forEach(e -> {
                final Interaction.Builder interactionBuilder = Interaction.builder().code((String) e.getKey());
                final String value = (String) e.getValue();
                if (!StringUtils.isEmpty(value)) {
                    interactionBuilder.documentation((String) value);
                }
                resourceBuilder.addInteraction(interactionBuilder.build());
            });
        });
        return resourceBuilder.build();
    }).sorted(Comparator.comparing(r -> r.getType().getCodeValue())).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) OpenApiWebMvcResource(org.springdoc.webmvc.api.OpenApiWebMvcResource) SnowOwlOpenApiWebMvcResource(com.b2international.snowowl.core.rest.SnowOwlOpenApiWebMvcResource) Paths(io.swagger.v3.oas.models.Paths) Tag(io.swagger.v3.oas.annotations.tags.Tag) Map(java.util.Map) Maps.newHashMap(com.google.common.collect.Maps.newHashMap)

Example 49 with Paths

use of io.swagger.v3.oas.models.Paths in project swagger-parser by swagger-api.

the class SwaggerConverter method convert.

public SwaggerParseResult convert(SwaggerDeserializationResult parse) {
    if (parse == null) {
        return null;
    }
    SwaggerParseResult output = new SwaggerParseResult().messages(parse.getMessages());
    if (parse.getSwagger() == null) {
        return output;
    }
    OpenAPI openAPI = new OpenAPI();
    SwaggerInventory inventory = new SwaggerInventory().process(parse.getSwagger());
    Swagger swagger = parse.getSwagger();
    if (swagger.getVendorExtensions() != null) {
        openAPI.setExtensions(convert(swagger.getVendorExtensions()));
    }
    // Set extension to retain original version of OAS document.
    openAPI.addExtension("x-original-swagger-version", swagger.getSwagger());
    if (swagger.getExternalDocs() != null) {
        openAPI.setExternalDocs(convert(swagger.getExternalDocs()));
    }
    if (swagger.getInfo() != null) {
        openAPI.setInfo(convert(swagger.getInfo()));
    }
    openAPI.setServers(convert(swagger.getSchemes(), swagger.getHost(), swagger.getBasePath()));
    if (swagger.getTags() != null) {
        openAPI.setTags(convertTags(swagger.getTags()));
    }
    if (swagger.getConsumes() != null) {
        this.globalConsumes.addAll(swagger.getConsumes());
    }
    if (swagger.getProduces() != null) {
        this.globalProduces.addAll(swagger.getProduces());
    }
    if (swagger.getSecurity() != null && swagger.getSecurity().size() > 0) {
        openAPI.setSecurity(convertSecurityRequirements(swagger.getSecurity()));
    }
    List<Model> models = inventory.getModels();
    // TODO until we have the example object working correctly in v3 pojos...
    for (Model model : models) {
        if (model instanceof RefModel) {
            RefModel ref = (RefModel) model;
            if (ref.get$ref().indexOf("#/definitions") == 0) {
                String updatedRef = "#/components/schemas" + ref.get$ref().substring("#/definitions".length());
                ref.set$ref(updatedRef);
            }
        }
    }
    for (Property property : inventory.getProperties()) {
        if (property instanceof RefProperty) {
            RefProperty ref = (RefProperty) property;
            if (ref.get$ref().indexOf("#/definitions") == 0) {
                String updatedRef = "#/components/schemas" + ref.get$ref().substring("#/definitions".length());
                ref.set$ref(updatedRef);
            }
        }
        if (property instanceof ComposedProperty) {
            ComposedProperty comprop = (ComposedProperty) property;
            if (comprop.getAllOf() != null) {
                for (Property item : comprop.getAllOf()) {
                    if (item instanceof RefProperty) {
                        RefProperty ref = (RefProperty) item;
                        if (ref.get$ref().indexOf("#/definitions") == 0) {
                            String updatedRef = "#/components/schemas" + ref.get$ref().substring("#/definitions".length());
                            ref.set$ref(updatedRef);
                        }
                    }
                }
            }
        }
    }
    if (swagger.getParameters() != null) {
        globalV2Parameters.putAll(swagger.getParameters());
        swagger.getParameters().forEach((k, v) -> {
            if ("body".equals(v.getIn())) {
                components.addRequestBodies(k, convertParameterToRequestBody(v));
            } else if ("formData".equals(v.getIn())) {
                // formData_ is added not to overwrite existing schemas
                components.addSchemas("formData_" + k, convertFormDataToSchema(v));
            } else {
                components.addParameters(k, convert(v));
            }
        });
    }
    Paths v3Paths = new Paths();
    Map<String, Path> pathMap = Optional.ofNullable(swagger.getPaths()).orElse(new HashMap<>());
    for (String pathname : pathMap.keySet()) {
        io.swagger.models.Path v2Path = swagger.getPath(pathname);
        PathItem v3Path = convert(v2Path);
        v3Paths.put(pathname, v3Path);
    }
    openAPI.setPaths(v3Paths);
    if (swagger.getResponses() != null) {
        swagger.getResponses().forEach((k, v) -> components.addResponses(k, convert(v)));
    }
    if (swagger.getDefinitions() != null) {
        for (String key : swagger.getDefinitions().keySet()) {
            Model model = swagger.getDefinitions().get(key);
            Schema schema = convert(model);
            components.addSchemas(key, schema);
        }
    }
    if (swagger.getSecurityDefinitions() != null) {
        swagger.getSecurityDefinitions().forEach((k, v) -> components.addSecuritySchemes(k, convert(v)));
    }
    openAPI.setComponents(components);
    output.setOpenAPI(openAPI);
    return output;
}
Also used : RefPath(io.swagger.models.RefPath) Path(io.swagger.models.Path) RefModel(io.swagger.models.RefModel) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) Schema(io.swagger.v3.oas.models.media.Schema) FileSchema(io.swagger.v3.oas.models.media.FileSchema) SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) PathItem(io.swagger.v3.oas.models.PathItem) Path(io.swagger.models.Path) Swagger(io.swagger.models.Swagger) Model(io.swagger.models.Model) RefModel(io.swagger.models.RefModel) ComposedModel(io.swagger.models.ComposedModel) ArrayModel(io.swagger.models.ArrayModel) Paths(io.swagger.v3.oas.models.Paths) OpenAPI(io.swagger.v3.oas.models.OpenAPI)

Example 50 with Paths

use of io.swagger.v3.oas.models.Paths in project swagger-parser by swagger-api.

the class V2ConverterTest method testIssue25.

@Test(description = "Covert path item $refs")
public void testIssue25() throws Exception {
    OpenAPI oas = getConvertedOpenAPIFromJsonFile(ISSUE_25_JSON);
    assertNotNull(oas);
    assertEquals(oas.getPaths().get("/foo2").get$ref(), "#/paths/~1foo");
}
Also used : OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Aggregations

Test (org.testng.annotations.Test)154 OpenAPI (io.swagger.v3.oas.models.OpenAPI)145 SwaggerParseResult (io.swagger.v3.parser.core.models.SwaggerParseResult)75 OpenAPIV3Parser (io.swagger.v3.parser.OpenAPIV3Parser)70 PathItem (io.swagger.v3.oas.models.PathItem)61 Operation (io.swagger.v3.oas.models.Operation)46 Paths (io.swagger.v3.oas.models.Paths)45 Schema (io.swagger.v3.oas.models.media.Schema)40 ArraySchema (io.swagger.v3.oas.models.media.ArraySchema)36 IntegerSchema (io.swagger.v3.oas.models.media.IntegerSchema)33 Components (io.swagger.v3.oas.models.Components)32 ComposedSchema (io.swagger.v3.oas.models.media.ComposedSchema)27 ObjectSchema (io.swagger.v3.oas.models.media.ObjectSchema)27 StringSchema (io.swagger.v3.oas.models.media.StringSchema)25 ByteArraySchema (io.swagger.v3.oas.models.media.ByteArraySchema)23 Parameter (io.swagger.v3.oas.models.parameters.Parameter)23 Info (io.swagger.v3.oas.models.info.Info)21 MapSchema (io.swagger.v3.oas.models.media.MapSchema)21 ParseOptions (io.swagger.v3.parser.core.models.ParseOptions)21 DateSchema (io.swagger.v3.oas.models.media.DateSchema)19