Search in sources :

Example 21 with Extension

use of io.swagger.v3.oas.annotations.extensions.Extension in project carbon-apimgt by wso2.

the class OAS3Parser method updateOperations.

/**
 * Update OAS operations for Store
 *
 * @param openAPI OpenAPI to be updated
 */
private void updateOperations(OpenAPI openAPI) {
    for (String pathKey : openAPI.getPaths().keySet()) {
        PathItem pathItem = openAPI.getPaths().get(pathKey);
        for (Map.Entry<PathItem.HttpMethod, Operation> entry : pathItem.readOperationsMap().entrySet()) {
            Operation operation = entry.getValue();
            Map<String, Object> extensions = operation.getExtensions();
            if (extensions != null) {
                // remove mediation extension
                if (extensions.containsKey(APIConstants.SWAGGER_X_MEDIATION_SCRIPT)) {
                    extensions.remove(APIConstants.SWAGGER_X_MEDIATION_SCRIPT);
                }
                // set x-scope value to security definition if it not there.
                if (extensions.containsKey(APIConstants.SWAGGER_X_WSO2_SCOPES)) {
                    String scope = (String) extensions.get(APIConstants.SWAGGER_X_WSO2_SCOPES);
                    List<SecurityRequirement> security = operation.getSecurity();
                    if (security == null) {
                        security = new ArrayList<>();
                        operation.setSecurity(security);
                    }
                    for (Map<String, List<String>> requirement : security) {
                        if (requirement.get(OPENAPI_SECURITY_SCHEMA_KEY) == null || !requirement.get(OPENAPI_SECURITY_SCHEMA_KEY).contains(scope)) {
                            requirement.put(OPENAPI_SECURITY_SCHEMA_KEY, Collections.singletonList(scope));
                        }
                    }
                }
            }
        }
    }
}
Also used : Operation(io.swagger.v3.oas.models.Operation) PathItem(io.swagger.v3.oas.models.PathItem) JSONObject(org.json.simple.JSONObject) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) HttpMethod(io.swagger.models.HttpMethod) SecurityRequirement(io.swagger.v3.oas.models.security.SecurityRequirement)

Example 22 with Extension

use of io.swagger.v3.oas.annotations.extensions.Extension in project swagger-parser by swagger-api.

the class OpenAPIV3Parser method read.

public OpenAPI read(String location, List<AuthorizationValue> auths, ParseOptions resolve) {
    if (location == null) {
        return null;
    }
    final List<SwaggerParserExtension> parserExtensions = getExtensions();
    SwaggerParseResult parsed;
    for (SwaggerParserExtension extension : parserExtensions) {
        parsed = extension.readLocation(location, auths, resolve);
        for (String message : parsed.getMessages()) {
            LOGGER.info("{}: {}", extension, message);
        }
        final OpenAPI result = parsed.getOpenAPI();
        if (result != null) {
            return result;
        }
    }
    return null;
}
Also used : SwaggerParserExtension(io.swagger.v3.parser.core.extensions.SwaggerParserExtension) SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) OpenAPI(io.swagger.v3.oas.models.OpenAPI)

Example 23 with Extension

use of io.swagger.v3.oas.annotations.extensions.Extension in project swagger-parser by swagger-api.

the class OpenAPIV3Parser method getExtensions.

protected static List<SwaggerParserExtension> getExtensions(ClassLoader cl) {
    final List<SwaggerParserExtension> extensions = new ArrayList<>();
    final ServiceLoader<SwaggerParserExtension> loader = ServiceLoader.load(SwaggerParserExtension.class, cl);
    for (SwaggerParserExtension extension : loader) {
        extensions.add(extension);
    }
    return extensions;
}
Also used : ArrayList(java.util.ArrayList) SwaggerParserExtension(io.swagger.v3.parser.core.extensions.SwaggerParserExtension)

Example 24 with Extension

use of io.swagger.v3.oas.annotations.extensions.Extension in project swagger-parser by swagger-api.

the class OpenAPIDeserializerTest method readServerObject.

@Test(dataProvider = "data")
public void readServerObject(JsonNode rootNode) throws Exception {
    final OpenAPIDeserializer deserializer = new OpenAPIDeserializer();
    final SwaggerParseResult result = deserializer.deserialize(rootNode);
    Assert.assertNotNull(result);
    final OpenAPI openAPI = result.getOpenAPI();
    Assert.assertNotNull(openAPI);
    final List<Server> server = openAPI.getServers();
    Assert.assertNotNull(server);
    Assert.assertNotNull(server.get(0));
    Assert.assertNotNull(server.get(0).getUrl());
    Assert.assertEquals(server.get(0).getUrl(), "http://petstore.swagger.io/v2");
    Assert.assertNotNull(server.get(1));
    Assert.assertNotNull(server.get(1).getUrl());
    Assert.assertNotNull(server.get(1).getDescription());
    Assert.assertEquals(server.get(1).getUrl(), "https://development.gigantic-server.com/v1");
    Assert.assertEquals(server.get(1).getDescription(), "Development server");
    Assert.assertNotNull(server.get(2));
    Assert.assertNotNull(server.get(2).getVariables());
    Assert.assertNotNull(server.get(2).getVariables().values());
    Assert.assertNotNull(server.get(2).getVariables().get("username"));
    Assert.assertEquals(server.get(2).getVariables().get("username").getDefault(), "demo");
    Assert.assertEquals(server.get(2).getVariables().get("username").getDescription(), "this value is assigned by the service provider, in this example `gigantic-server.com`");
    Assert.assertNotNull(server.get(2).getVariables().get("port").getEnum());
    Assert.assertEquals(server.get(2).getVariables().get("port").getEnum().get(0), "8443");
    Assert.assertEquals(server.get(2).getVariables().get("port").getEnum().get(1), "443");
    Assert.assertEquals(server.get(2).getVariables().get("port").getDefault(), "8443");
    Assert.assertNotNull(server.get(2).getVariables().get("port"));
    Assert.assertNotNull(server.get(2).getVariables().get("basePath"));
    Assert.assertNotNull(server.get(2).getExtensions().get("x-server"));
    Assert.assertEquals(server.get(2).getExtensions().get("x-server").toString(), "server extension");
    Assert.assertEquals(server.get(2).getVariables().get("basePath").getDescription(), "testing overwriting");
    Assert.assertEquals(server.get(2).getVariables().get("basePath").getDefault(), "v2");
}
Also used : Server(io.swagger.v3.oas.models.servers.Server) SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Example 25 with Extension

use of io.swagger.v3.oas.annotations.extensions.Extension in project swagger-parser by swagger-api.

the class OpenAPIDeserializerTest method testToplevelExtension.

@Test(description = "it should read a top-level extension per https://github.com/openAPI-api/validator-badge/issues/59")
public void testToplevelExtension() throws Exception {
    String yaml = "openapi: 3.0.0\n" + "servers: []\n" + "x-foo: woof\n" + "info:\n" + "  version: 0.0.0\n" + "  title: Simple API\n" + "paths:\n" + "  /:\n" + "    get:\n" + "      responses:\n" + "        '200':\n" + "          description: OK";
    OpenAPIV3Parser parser = new OpenAPIV3Parser();
    SwaggerParseResult result = parser.readContents(yaml, null, null);
    assertNotNull(result.getOpenAPI().getExtensions());
}
Also used : SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) Test(org.testng.annotations.Test)

Aggregations

OpenAPI (io.swagger.v3.oas.models.OpenAPI)17 Test (org.testng.annotations.Test)11 SwaggerParseResult (io.swagger.v3.parser.core.models.SwaggerParseResult)9 Operation (io.swagger.v3.oas.models.Operation)8 HashMap (java.util.HashMap)8 LinkedHashMap (java.util.LinkedHashMap)8 Map (java.util.Map)8 PathItem (io.swagger.v3.oas.models.PathItem)7 Schema (io.swagger.v3.oas.models.media.Schema)6 Paths (io.swagger.v3.oas.models.Paths)5 ArrayList (java.util.ArrayList)5 JsonNode (com.fasterxml.jackson.databind.JsonNode)4 HttpMethod (io.swagger.models.HttpMethod)4 Components (io.swagger.v3.oas.models.Components)4 ArraySchema (io.swagger.v3.oas.models.media.ArraySchema)4 JSONObject (org.json.simple.JSONObject)4 Info (io.swagger.v3.oas.models.info.Info)3 ComposedSchema (io.swagger.v3.oas.models.media.ComposedSchema)3 IntegerSchema (io.swagger.v3.oas.models.media.IntegerSchema)3 MediaType (io.swagger.v3.oas.models.media.MediaType)3