Java Code Examples for io.swagger.models.Swagger#getVendorExtensions()

The following examples show how to use io.swagger.models.Swagger#getVendorExtensions() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MultiOpenApiParser.java    From api-compiler with Apache License 2.0 6 votes vote down vote up
public static OpenApiFile create(
    Service.Builder serviceBuilder, Swagger swagger, String filename, String typeNamespace)
    throws OpenApiConversionException {
  String hostname = Strings.nullToEmpty(swagger.getHost());
  String version = Strings.nullToEmpty(swagger.getInfo().getVersion());
  String googleApiName = "";
  if (swagger.getVendorExtensions() != null) {
    googleApiName = Strings.nullToEmpty(
        (String) swagger.getVendorExtensions().get(ExtensionNames.API_NAME));
  }
  String apiName = ApiNameGenerator.generate(hostname, googleApiName, version);
  return new AutoValue_MultiOpenApiParser_OpenApiFile(
      serviceBuilder,
      swagger,
      filename,
      apiName,
      OpenApiConversionResources.create(swagger, filename, apiName, typeNamespace));
}
 
Example 2
Source File: OAS2Parser.java    From carbon-apimgt with Apache License 2.0 6 votes vote down vote up
/**
 * Get scope information from the extensions
 *
 * @param swagger swagger object
 * @return Scope set
 */
private Set<Scope> getScopesFromExtensions(Swagger swagger) throws APIManagementException {
    Set<Scope> scopeList = new LinkedHashSet<>();
    Map<String, Object> extensions = swagger.getVendorExtensions();
    if (extensions != null && extensions.containsKey(APIConstants.SWAGGER_X_WSO2_SECURITY)) {
        Map<String, Object> securityDefinitions =
                (Map<String, Object>) extensions.get(APIConstants.SWAGGER_X_WSO2_SECURITY);
        for (Map.Entry<String, Object> entry : securityDefinitions.entrySet()) {
            Map<String, Object> securityDefinition = (Map<String, Object>) entry.getValue();
            if (securityDefinition.containsKey(APIConstants.SWAGGER_X_WSO2_SCOPES)) {
                List<Map<String, String>> oauthScope =
                        (List<Map<String, String>>) securityDefinition.get(APIConstants.SWAGGER_X_WSO2_SCOPES);
                for (Map<String, String> anOauthScope : oauthScope) {
                    Scope scope = new Scope();
                    scope.setKey(anOauthScope.get(APIConstants.SWAGGER_SCOPE_KEY));
                    scope.setName(anOauthScope.get(APIConstants.SWAGGER_NAME));
                    scope.setDescription(anOauthScope.get(APIConstants.SWAGGER_DESCRIPTION));
                    scope.setRoles(anOauthScope.get(APIConstants.SWAGGER_ROLES));

                    scopeList.add(scope);
                }
            }
        }
    }
    return scopeList;
}
 
Example 3
Source File: ProtoApiFromOpenApi.java    From api-compiler with Apache License 2.0 5 votes vote down vote up
private boolean isDeprecated(Swagger swagger) {
  if (swagger.getVendorExtensions() != null
      && swagger.getVendorExtensions().containsKey(ExtensionNames.X_GOOGLE_DEPRECATED)) {
    return VendorExtensionUtils.getExtensionValue(
        swagger.getVendorExtensions(),
        Boolean.class,
        diagCollector,
        ExtensionNames.X_GOOGLE_DEPRECATED);
  }
  return false;
}
 
Example 4
Source File: AuthzBuilder.java    From api-compiler with Apache License 2.0 5 votes vote down vote up
@Override
public void addFromSwagger(Service.Builder serviceBuilder, Swagger swagger) {
  VendorExtensionProtoConverter extensionConverter =
      new VendorExtensionProtoConverter(swagger.getVendorExtensions(), diagCollector);

  if (extensionConverter.hasExtension(ExtensionNames.AUTHORIZATION_EXTENSION_NAME)) {
    Experimental.Builder eBuilder = serviceBuilder.getExperimentalBuilder();

    AuthorizationConfig config =
        extensionConverter.convertExtensionToProto(
            AuthorizationConfig.getDefaultInstance(),
            ExtensionNames.AUTHORIZATION_EXTENSION_NAME);
    serviceBuilder.setExperimental(eBuilder.setAuthorization(config));
  }
}
 
Example 5
Source File: QuotaBuilder.java    From api-compiler with Apache License 2.0 5 votes vote down vote up
@Override
public void addFromSwagger(Service.Builder serviceBuilder, Swagger swagger) {
  VendorExtensionProtoConverter extensionConverter =
      new VendorExtensionProtoConverter(swagger.getVendorExtensions(), diagCollector);
  if (extensionConverter.hasExtension(ExtensionNames.MANAGEMENT_SWAGGER_EXTENSION)) {
    ServiceManagementExtension serviceManagementExtension = readExtension(swagger);
    Quota quota = parseQuota(serviceManagementExtension, extensionConverter);
    if (!quota.equals(Quota.getDefaultInstance())) {
      serviceBuilder.setQuota(quota);
    }
    serviceBuilder.addAllMetrics(parseMetrics(serviceManagementExtension, extensionConverter));
  }
}
 
Example 6
Source File: EndpointBuilder.java    From api-compiler with Apache License 2.0 5 votes vote down vote up
@Override
public void addFromSwagger(Service.Builder serviceBuilder, Swagger swagger) {

  VendorExtensionProtoConverter extensionConverter =
      new VendorExtensionProtoConverter(swagger.getVendorExtensions(), diagCollector);
  if (extensionConverter.hasExtension(ExtensionNames.ENDPOINTS_EXTENSION_NAME)) {
    serviceBuilder.addAllEndpoints(
        extensionConverter.convertExtensionToProtos(
            Endpoint.getDefaultInstance(), ExtensionNames.ENDPOINTS_EXTENSION_NAME));
    validEndpointExtension(serviceBuilder, swagger.getHost());
  }
}
 
Example 7
Source File: OAS2Parser.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
/**
 * Remove MG related information
 *
 * @param swagger Swagger
 */
private void removePublisherSpecificInfo(Swagger swagger) {
    Map<String, Object> extensions = swagger.getVendorExtensions();
    OASParserUtil.removePublisherSpecificInfo(extensions);
    for (String pathKey : swagger.getPaths().keySet()) {
        Path path = swagger.getPath(pathKey);
        Map<HttpMethod, Operation> operationMap = path.getOperationMap();
        for (Map.Entry<HttpMethod, Operation> entry : operationMap.entrySet()) {
            Operation operation = entry.getValue();
            OASParserUtil.removePublisherSpecificInfofromOperation(operation.getVendorExtensions());
        }
    }
}
 
Example 8
Source File: OAS2Parser.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
/**
 * Remove legacy scope from swagger
 *
 * @param swagger
 */
private void updateLegacyScopesFromSwagger(Swagger swagger, SwaggerData swaggerData) {

    Map<String, Object> extensions = swagger.getVendorExtensions();
    if (extensions != null && extensions.containsKey(APIConstants.SWAGGER_X_WSO2_SECURITY)) {
        extensions.remove(APIConstants.SWAGGER_X_WSO2_SECURITY);
    }
}
 
Example 9
Source File: OAS2ParserTest.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
@Test
public void testUpdateAPIDefinitionWithExtensions() throws Exception {
    String relativePath = "definitions" + File.separator + "oas2" + File.separator + "oas2Resources.json";
    String oas2Resources = IOUtils.toString(getClass().getClassLoader().getResourceAsStream(relativePath), "UTF-8");
    SwaggerParser swaggerParser = new SwaggerParser();

    // check remove vendor extensions
    String definition = testGenerateAPIDefinitionWithExtension(oas2Parser, oas2Resources);
    Swagger swaggerObj = swaggerParser.parse(definition);
    boolean isExtensionNotFound =
            swaggerObj.getVendorExtensions() == null || swaggerObj.getVendorExtensions().isEmpty();
    Assert.assertTrue(isExtensionNotFound);
    Assert.assertEquals(2, swaggerObj.getPaths().size());

    Iterator<Map.Entry<String, Path>> itr = swaggerObj.getPaths().entrySet().iterator();
    while (itr.hasNext()) {
        Map.Entry<String, Path> pathEntry = itr.next();
        Path path = pathEntry.getValue();
        for (Map.Entry<HttpMethod, Operation> operationEntry : path.getOperationMap().entrySet()) {
            Operation operation = operationEntry.getValue();
            Assert.assertFalse(operation.getVendorExtensions().containsKey(APIConstants.SWAGGER_X_SCOPE));
        }
    }

    // check updated scopes in security definition
    Operation itemGet = swaggerObj.getPath("/items").getGet();
    Assert.assertTrue(itemGet.getSecurity().get(0).get("default").contains("newScope"));

    // check available scopes in security definition
    OAuth2Definition oAuth2Definition = (OAuth2Definition) swaggerObj.getSecurityDefinitions().get("default");
    Assert.assertTrue(oAuth2Definition.getScopes().containsKey("newScope"));
    Assert.assertEquals("newScopeDescription", oAuth2Definition.getScopes().get("newScope"));

    Assert.assertTrue(oAuth2Definition.getVendorExtensions().containsKey(APIConstants.SWAGGER_X_SCOPES_BINDINGS));
    Map<String, String> scopeBinding = (Map<String, String>) oAuth2Definition.getVendorExtensions()
            .get(APIConstants.SWAGGER_X_SCOPES_BINDINGS);
    Assert.assertTrue(scopeBinding.containsKey("newScope"));
    Assert.assertEquals("admin", scopeBinding.get("newScope"));
}
 
Example 10
Source File: OAS2Parser.java    From carbon-apimgt with Apache License 2.0 4 votes vote down vote up
/**
 * This method returns api that is attached with api extensions related to micro-gw
 *
 * @param apiDefinition                  String
 * @param api                            API
 * @param isBasepathExtractedFromSwagger boolean
 * @return API
 */
@Override
public API setExtensionsToAPI(String apiDefinition, API api, boolean isBasepathExtractedFromSwagger) throws APIManagementException {
    Swagger swagger = getSwagger(apiDefinition);
    Map<String, Object> extensions = swagger.getVendorExtensions();
    if (extensions == null) {
        return api;
    }

    //Setup Custom auth header for API
    String authHeader = OASParserUtil.getAuthorizationHeaderFromSwagger(extensions);
    if (StringUtils.isNotBlank(authHeader)) {
        api.setAuthorizationHeader(authHeader);
    }
    //Setup mutualSSL configuration
    String mutualSSL = OASParserUtil.getMutualSSLEnabledFromSwagger(extensions);
    if (StringUtils.isNotBlank(mutualSSL)) {
        String securityList = api.getApiSecurity();
        if (StringUtils.isBlank(securityList)) {
            securityList = APIConstants.DEFAULT_API_SECURITY_OAUTH2;
        }
        if (APIConstants.OPTIONAL.equals(mutualSSL)) {
            securityList = securityList + "," + APIConstants.API_SECURITY_MUTUAL_SSL;
        } else if (APIConstants.MANDATORY.equals(mutualSSL)) {
            securityList = securityList + "," + APIConstants.API_SECURITY_MUTUAL_SSL_MANDATORY;
        }
        api.setApiSecurity(securityList);
    }
    //Setup CORSConfigurations
    CORSConfiguration corsConfiguration = OASParserUtil.getCorsConfigFromSwagger(extensions);
    if (corsConfiguration != null) {
        api.setCorsConfiguration(corsConfiguration);
    }
    //Setup Response cache enabling
    boolean responseCacheEnable = OASParserUtil.getResponseCacheFromSwagger(extensions);
    if (responseCacheEnable) {
        api.setResponseCache(APIConstants.ENABLED);
    }
    //Setup cache timeOut
    int cacheTimeOut = OASParserUtil.getCacheTimeOutFromSwagger(extensions);
    if (cacheTimeOut != 0) {
        api.setCacheTimeout(cacheTimeOut);
    }
    //Setup Transports
    String transports = OASParserUtil.getTransportsFromSwagger(extensions);
    if (StringUtils.isNotBlank(transports)) {
        api.setTransports(transports);
    }
    //Setup Throttlingtiers
    String throttleTier = OASParserUtil.getThrottleTierFromSwagger(extensions);
    if (StringUtils.isNotBlank(throttleTier)) {
        api.setApiLevelPolicy(throttleTier);
    }
    //Setup Basepath
    String basePath = OASParserUtil.getBasePathFromSwagger(extensions);
    if (StringUtils.isNotBlank(basePath) && isBasepathExtractedFromSwagger) {
        basePath = basePath.replace("{version}", api.getId().getVersion());
        api.setContextTemplate(basePath);
        api.setContext(basePath);
    }
    return api;
}