Search in sources :

Example 81 with Swagger

use of io.swagger.models.Swagger in project java-chassis by ServiceComb.

the class TestDefinition method testMicroserviceMetaManager.

@Test
public void testMicroserviceMetaManager() throws Exception {
    MicroserviceMetaManager microserviceMetaManager = new MicroserviceMetaManager();
    microserviceMetaManager.getOrCreateMicroserviceMeta("testname");
    Assert.assertEquals("microservice meta manager", microserviceMetaManager.getName());
    Assert.assertEquals("Not allow regsiter repeat data, name=%s, key=%s", microserviceMetaManager.getRegisterErrorFmt());
    Assert.assertEquals(0, microserviceMetaManager.getAllSchemaMeta("testname").size());
    Swagger oSwagger = new Swagger();
    Info oInfo = new Info();
    oInfo.setVendorExtension("x-java-interface", "java.lang.String");
    oSwagger.setInfo(oInfo);
    Assert.assertEquals("java.lang.String", (ClassUtils.getJavaInterface(oSwagger)).getName());
    oInfo.setVendorExtension("x-java-class", "java.lang.String");
}
Also used : MicroserviceMetaManager(io.servicecomb.core.definition.MicroserviceMetaManager) Swagger(io.swagger.models.Swagger) Info(io.swagger.models.Info) Test(org.junit.Test)

Example 82 with Swagger

use of io.swagger.models.Swagger in project java-chassis by ServiceComb.

the class RestOperationMeta method init.

public void init(OperationMeta operationMeta) {
    this.operationMeta = operationMeta;
    Swagger swagger = operationMeta.getSchemaMeta().getSwagger();
    Operation operation = operationMeta.getSwaggerOperation();
    this.produces = operation.getProduces();
    if (produces == null) {
        this.produces = swagger.getProduces();
    }
    setAbsolutePath(concatPath(swagger.getBasePath(), operationMeta.getOperationPath()));
    this.createProduceProcessors();
    Method method = operationMeta.getMethod();
    Type[] genericParamTypes = method.getGenericParameterTypes();
    if (genericParamTypes.length != operation.getParameters().size()) {
        throw new Error("Param count is not equal between swagger and method,  path=" + absolutePath);
    }
    // 初始化所有rest param
    for (int idx = 0; idx < genericParamTypes.length; idx++) {
        Parameter parameter = operation.getParameters().get(idx);
        Type genericParamType = genericParamTypes[idx];
        RestParam param = new RestParam(idx, parameter, genericParamType);
        addParam(param);
    }
    this.pathBuilder = new URLPathBuilder(absolutePath, paramMap);
}
Also used : MediaType(javax.ws.rs.core.MediaType) Type(java.lang.reflect.Type) Swagger(io.swagger.models.Swagger) Parameter(io.swagger.models.parameters.Parameter) URLPathBuilder(io.servicecomb.common.rest.definition.path.URLPathBuilder) Operation(io.swagger.models.Operation) Method(java.lang.reflect.Method)

Example 83 with Swagger

use of io.swagger.models.Swagger in project ORCID-Source by ORCID.

the class PublicSwaggerResource method scan.

/**
     * Scan the classes and add in the OAuth information
     * 
     */
@Override
protected synchronized Swagger scan(Application app) {
    // tell swagger to pick up our jaxb annotations
    Json.mapper().registerModule(new JaxbAnnotationModule());
    Swagger s = super.scan(app);
    OAuth2Definition oauthTwoLegs = new OAuth2Definition();
    oauthTwoLegs.application(this.tokenEndPoint);
    oauthTwoLegs.scope(ScopePathType.READ_PUBLIC.value(), "Read Public record");
    s.securityDefinition("orcid_two_legs", oauthTwoLegs);
    return s;
}
Also used : JaxbAnnotationModule(com.fasterxml.jackson.module.jaxb.JaxbAnnotationModule) Swagger(io.swagger.models.Swagger) OAuth2Definition(io.swagger.models.auth.OAuth2Definition)

Example 84 with Swagger

use of io.swagger.models.Swagger in project cxf by apache.

the class Swagger2Feature method addSwaggerResource.

@Override
protected void addSwaggerResource(Server server, Bus bus) {
    JAXRSServiceFactoryBean sfb = (JAXRSServiceFactoryBean) server.getEndpoint().get(JAXRSServiceFactoryBean.class.getName());
    ServerProviderFactory factory = (ServerProviderFactory) server.getEndpoint().get(ServerProviderFactory.class.getName());
    ApplicationInfo appInfo = null;
    if (!isScan()) {
        appInfo = factory.getApplicationProvider();
        if (appInfo == null) {
            Set<Class<?>> serviceClasses = new HashSet<>();
            for (ClassResourceInfo cri : sfb.getClassResourceInfo()) {
                serviceClasses.add(cri.getServiceClass());
            }
            appInfo = new ApplicationInfo(new DefaultApplication(serviceClasses), bus);
            server.getEndpoint().put(Application.class.getName(), appInfo);
        }
    }
    List<Object> swaggerResources = new LinkedList<>();
    if (customizer == null) {
        customizer = new Swagger2Customizer();
    }
    ApiListingResource apiListingResource = new Swagger2ApiListingResource(customizer);
    swaggerResources.add(apiListingResource);
    List<Object> providers = new ArrayList<>();
    if (isRunAsFilter()) {
        providers.add(new SwaggerContainerRequestFilter(appInfo == null ? null : appInfo.getProvider(), customizer));
    }
    final Properties swaggerProps = getSwaggerProperties(bus);
    final Registration swaggerUiRegistration = getSwaggerUi(bus, swaggerProps, isRunAsFilter());
    if (!isRunAsFilter()) {
        swaggerResources.addAll(swaggerUiRegistration.getResources());
    }
    providers.addAll(swaggerUiRegistration.getProviders());
    sfb.setResourceClassesFromBeans(swaggerResources);
    List<ClassResourceInfo> cris = sfb.getClassResourceInfo();
    if (!isRunAsFilter()) {
        for (ClassResourceInfo cri : cris) {
            if (ApiListingResource.class.isAssignableFrom(cri.getResourceClass())) {
                InjectionUtils.injectContextProxies(cri, apiListingResource);
            }
        }
    }
    customizer.setClassResourceInfos(cris);
    customizer.setDynamicBasePath(dynamicBasePath);
    BeanConfig beanConfig = appInfo == null ? new BeanConfig() : new ApplicationBeanConfig(appInfo.getProvider());
    initBeanConfig(beanConfig, swaggerProps);
    Swagger swagger = beanConfig.getSwagger();
    if (swagger != null && securityDefinitions != null) {
        swagger.setSecurityDefinitions(securityDefinitions);
    }
    customizer.setBeanConfig(beanConfig);
    providers.add(new ReaderConfigFilter());
    if (beanConfig.isUsePathBasedConfig()) {
        providers.add(new ServletConfigProvider());
    }
    factory.setUserProviders(providers);
}
Also used : ServerProviderFactory(org.apache.cxf.jaxrs.provider.ServerProviderFactory) ApplicationInfo(org.apache.cxf.jaxrs.model.ApplicationInfo) ApiListingResource(io.swagger.jaxrs.listing.ApiListingResource) ArrayList(java.util.ArrayList) Properties(java.util.Properties) JAXRSServiceFactoryBean(org.apache.cxf.jaxrs.JAXRSServiceFactoryBean) Swagger(io.swagger.models.Swagger) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) BeanConfig(io.swagger.jaxrs.config.BeanConfig) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) LinkedList(java.util.LinkedList) Application(javax.ws.rs.core.Application)

Example 85 with Swagger

use of io.swagger.models.Swagger in project carbon-apimgt by wso2.

the class APIDefinitionFromSwagger20 method removeScopeFromSwaggerDefinition.

@Override
public String removeScopeFromSwaggerDefinition(String resourceConfigJSON, String name) {
    SwaggerParser swaggerParser = new SwaggerParser();
    Swagger swagger = swaggerParser.parse(resourceConfigJSON);
    Map<String, SecuritySchemeDefinition> securitySchemeDefinitionMap = swagger.getSecurityDefinitions();
    if (securitySchemeDefinitionMap != null && !securitySchemeDefinitionMap.isEmpty()) {
        OAuth2Definition oAuth2Definition = (OAuth2Definition) securitySchemeDefinitionMap.get(APIMgtConstants.OAUTH2SECURITY);
        if (oAuth2Definition != null) {
            // Removing Scope from Swagger SecurityDefinition
            oAuth2Definition.getScopes().remove(name);
            // Finding Security requirements at root level
            List<SecurityRequirement> securityRequirements = swagger.getSecurity();
            if (securityRequirements != null && !securityRequirements.isEmpty()) {
                // Get List of Security Requirements
                Iterator<SecurityRequirement> securityRequirementIterator = securityRequirements.iterator();
                while (securityRequirementIterator.hasNext()) {
                    SecurityRequirement securityRequirement = securityRequirementIterator.next();
                    Map<String, List<String>> secListMap = securityRequirement.getRequirements();
                    // get Oauth2Security scopes
                    List<String> scopesList = secListMap.get(APIMgtConstants.OAUTH2SECURITY);
                    if (scopesList != null) {
                        // Remove Scope from root level
                        scopesList.remove(name);
                    }
                    // Check root level security Requirements is empty
                    if (securityRequirement.getRequirements().isEmpty()) {
                        // Check root level security Requirements
                        securityRequirementIterator.remove();
                    }
                }
                if (securityRequirements.isEmpty()) {
                    // Remove root level security
                    swagger.setSecurity(null);
                }
            }
            Map<String, Path> pathMap = swagger.getPaths();
            if (pathMap != null && !pathMap.isEmpty()) {
                for (Map.Entry<String, Path> pathEntry : pathMap.entrySet()) {
                    Path path = pathEntry.getValue();
                    List<Operation> operationList = path.getOperations();
                    for (Operation operation : operationList) {
                        List<Map<String, List<String>>> operationSecurityList = operation.getSecurity();
                        if (operationSecurityList != null && !operationSecurityList.isEmpty()) {
                            Iterator<Map<String, List<String>>> securityMapIterator = operationSecurityList.iterator();
                            while (securityMapIterator.hasNext()) {
                                Map<String, List<String>> securityMap = securityMapIterator.next();
                                List<String> scopesList = securityMap.get(APIMgtConstants.OAUTH2SECURITY);
                                scopesList.remove(name);
                                if (scopesList.isEmpty()) {
                                    securityMapIterator.remove();
                                }
                            }
                            if (operationSecurityList.isEmpty()) {
                                operation.setSecurity(null);
                            }
                        }
                    }
                }
            }
        }
    }
    return Json.pretty(swagger);
}
Also used : Path(io.swagger.models.Path) OAuth2Definition(io.swagger.models.auth.OAuth2Definition) SecuritySchemeDefinition(io.swagger.models.auth.SecuritySchemeDefinition) Operation(io.swagger.models.Operation) SwaggerParser(io.swagger.parser.SwaggerParser) Swagger(io.swagger.models.Swagger) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) SecurityRequirement(io.swagger.models.SecurityRequirement)

Aggregations

Swagger (io.swagger.models.Swagger)184 Test (org.testng.annotations.Test)115 Operation (io.swagger.models.Operation)45 Parameter (io.swagger.models.parameters.Parameter)33 QueryParameter (io.swagger.models.parameters.QueryParameter)33 BodyParameter (io.swagger.models.parameters.BodyParameter)29 Path (io.swagger.models.Path)26 PathParameter (io.swagger.models.parameters.PathParameter)25 Reader (io.swagger.jaxrs.Reader)23 SpecFilter (io.swagger.core.filter.SpecFilter)21 Test (org.junit.Test)19 SwaggerParser (io.swagger.parser.SwaggerParser)16 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)16 Response (io.swagger.models.Response)14 SerializableParameter (io.swagger.models.parameters.SerializableParameter)14 HashMap (java.util.HashMap)14 Info (io.swagger.models.Info)12 Map (java.util.Map)12 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)10 Model (io.swagger.models.Model)10