Search in sources :

Example 1 with Tag

use of io.swagger.models.Tag in project camel by apache.

the class RestSwaggerReader method parse.

private void parse(Swagger swagger, RestDefinition rest, String camelContextId, ClassResolver classResolver) {
    List<VerbDefinition> verbs = new ArrayList<>(rest.getVerbs());
    // must sort the verbs by uri so we group them together when an uri has multiple operations
    Collections.sort(verbs, new VerbOrdering());
    // we need to group the operations within the same tag, so use the path as default if not configured
    String pathAsTag = rest.getTag() != null ? rest.getTag() : FileUtil.stripLeadingSeparator(rest.getPath());
    String summary = rest.getDescriptionText();
    if (ObjectHelper.isNotEmpty(pathAsTag)) {
        // add rest as tag
        Tag tag = new Tag();
        tag.description(summary);
        tag.name(pathAsTag);
        swagger.addTag(tag);
    }
    // gather all types in use
    Set<String> types = new LinkedHashSet<>();
    for (VerbDefinition verb : verbs) {
        // check if the Verb Definition must be excluded from documentation
        Boolean apiDocs;
        if (verb.getApiDocs() != null) {
            apiDocs = verb.getApiDocs();
        } else {
            // fallback to option on rest
            apiDocs = rest.getApiDocs();
        }
        if (apiDocs != null && !apiDocs) {
            continue;
        }
        String type = verb.getType();
        if (ObjectHelper.isNotEmpty(type)) {
            if (type.endsWith("[]")) {
                type = type.substring(0, type.length() - 2);
            }
            types.add(type);
        }
        type = verb.getOutType();
        if (ObjectHelper.isNotEmpty(type)) {
            if (type.endsWith("[]")) {
                type = type.substring(0, type.length() - 2);
            }
            types.add(type);
        }
        // there can also be types in response messages
        if (verb.getResponseMsgs() != null) {
            for (RestOperationResponseMsgDefinition def : verb.getResponseMsgs()) {
                type = def.getResponseModel();
                if (ObjectHelper.isNotEmpty(type)) {
                    if (type.endsWith("[]")) {
                        type = type.substring(0, type.length() - 2);
                    }
                    types.add(type);
                }
            }
        }
    }
    // use annotation scanner to find models (annotated classes)
    for (String type : types) {
        Class<?> clazz = classResolver.resolveClass(type);
        appendModels(clazz, swagger);
    }
    doParseVerbs(swagger, rest, camelContextId, verbs, pathAsTag);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) VerbDefinition(org.apache.camel.model.rest.VerbDefinition) RestOperationResponseMsgDefinition(org.apache.camel.model.rest.RestOperationResponseMsgDefinition) ArrayList(java.util.ArrayList) Tag(io.swagger.models.Tag)

Example 2 with Tag

use of io.swagger.models.Tag in project swagger-core by swagger-api.

the class ReaderTest method scanApiAnnotationWhichAreOnlyPresentInInterfaceAndNotInImplementation.

@Test(description = "scan resource (impl) which has the Api annotations only declared in its interface")
public void scanApiAnnotationWhichAreOnlyPresentInInterfaceAndNotInImplementation() {
    Swagger swagger = getSwagger(ResourceWithAnnotationsOnlyInInterfaceImpl.class);
    assertNotNull(swagger);
    final List<Tag> tags = swagger.getTags();
    assertEquals(tags.size(), 1);
    assertEquals(tags.get(0).getName(), "someTag");
}
Also used : Swagger(io.swagger.models.Swagger) Tag(io.swagger.models.Tag) Test(org.testng.annotations.Test)

Example 3 with Tag

use of io.swagger.models.Tag in project swagger-core by swagger-api.

the class SimpleReaderTest method scanResourceWithResponseStatusReturnType.

@Test(description = "scan a resource with Response.Status return type per 877")
public void scanResourceWithResponseStatusReturnType() {
    Swagger swagger = getSwagger(Resource877.class);
    assertNotNull(swagger.getTags());
    assertEquals(swagger.getTags().size(), 1);
    Tag tag = swagger.getTags().get(0);
    assertEquals(tag.getName(), "externalinfo");
    assertNull(tag.getDescription());
    assertNull(tag.getExternalDocs());
}
Also used : Swagger(io.swagger.models.Swagger) Tag(io.swagger.models.Tag) Test(org.testng.annotations.Test)

Example 4 with Tag

use of io.swagger.models.Tag in project swagger-core by swagger-api.

the class SpecFilter method filter.

public Swagger filter(Swagger swagger, SwaggerSpecFilter filter, Map<String, List<String>> params, Map<String, String> cookies, Map<String, List<String>> headers) {
    Swagger clone = new Swagger();
    clone.info(swagger.getInfo()).tags(swagger.getTags() == null ? null : new ArrayList<Tag>(swagger.getTags())).host(swagger.getHost()).basePath(swagger.getBasePath()).schemes(swagger.getSchemes()).consumes(swagger.getConsumes()).produces(swagger.getProduces()).externalDocs(swagger.getExternalDocs()).vendorExtensions(swagger.getVendorExtensions());
    final Set<String> filteredTags = new HashSet<String>();
    final Set<String> allowedTags = new HashSet<String>();
    for (String resourcePath : swagger.getPaths().keySet()) {
        Path path = swagger.getPaths().get(resourcePath);
        Map<String, Operation> ops = new HashMap<String, Operation>();
        ops.put("get", path.getGet());
        ops.put("head", path.getHead());
        ops.put("put", path.getPut());
        ops.put("post", path.getPost());
        ops.put("delete", path.getDelete());
        ops.put("patch", path.getPatch());
        ops.put("options", path.getOptions());
        Path clonedPath = new Path();
        for (String key : ops.keySet()) {
            Operation op = ops.get(key);
            if (op != null) {
                ApiDescription desc = new ApiDescription(resourcePath, key);
                final Set<String> tags;
                if (filter.isOperationAllowed(op, desc, params, cookies, headers)) {
                    clonedPath.set(key, filterOperation(filter, op, desc, params, cookies, headers));
                    tags = allowedTags;
                } else {
                    tags = filteredTags;
                }
                if (op.getTags() != null) {
                    tags.addAll(op.getTags());
                }
            }
        }
        if (!clonedPath.isEmpty()) {
            clone.path(resourcePath, clonedPath);
        }
    }
    final List<Tag> tags = clone.getTags();
    filteredTags.removeAll(allowedTags);
    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);
        }
    }
    Map<String, Model> definitions = filterDefinitions(filter, swagger.getDefinitions(), params, cookies, headers);
    clone.setSecurityDefinitions(swagger.getSecurityDefinitions());
    clone.setSecurity(swagger.getSecurity());
    clone.setDefinitions(definitions);
    // existing client filters directly implementing SwaggerSpecFilter.
    if (filter instanceof AbstractSpecFilter) {
        if (((AbstractSpecFilter) filter).isRemovingUnreferencedDefinitions()) {
            clone = removeBrokenReferenceDefinitions(clone);
        }
    }
    return clone;
}
Also used : Path(io.swagger.models.Path) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Operation(io.swagger.models.Operation) 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) Tag(io.swagger.models.Tag) ApiDescription(io.swagger.model.ApiDescription) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 5 with Tag

use of io.swagger.models.Tag in project swagger-core by swagger-api.

the class Reader method readSwaggerConfig.

protected void readSwaggerConfig(Class<?> cls, SwaggerDefinition config) {
    if (!config.basePath().isEmpty()) {
        swagger.setBasePath(config.basePath());
    }
    if (!config.host().isEmpty()) {
        swagger.setHost(config.host());
    }
    readInfoConfig(config);
    for (String consume : config.consumes()) {
        if (StringUtils.isNotEmpty(consume)) {
            swagger.addConsumes(consume);
        }
    }
    for (String produce : config.produces()) {
        if (StringUtils.isNotEmpty(produce)) {
            swagger.addProduces(produce);
        }
    }
    for (OAuth2Definition oAuth2Config : config.securityDefinition().oAuth2Definitions()) {
        io.swagger.models.auth.OAuth2Definition oAuth2Definition = new io.swagger.models.auth.OAuth2Definition();
        OAuth2Definition.Flow flow = oAuth2Config.flow();
        if (flow.equals(OAuth2Definition.Flow.ACCESS_CODE)) {
            oAuth2Definition = oAuth2Definition.accessCode(oAuth2Config.authorizationUrl(), oAuth2Config.tokenUrl());
        } else if (flow.equals(OAuth2Definition.Flow.APPLICATION)) {
            oAuth2Definition = oAuth2Definition.application(oAuth2Config.tokenUrl());
        } else if (flow.equals(OAuth2Definition.Flow.IMPLICIT)) {
            oAuth2Definition = oAuth2Definition.implicit(oAuth2Config.authorizationUrl());
        } else {
            oAuth2Definition = oAuth2Definition.password(oAuth2Config.tokenUrl());
        }
        for (Scope scope : oAuth2Config.scopes()) {
            oAuth2Definition.addScope(scope.name(), scope.description());
        }
        oAuth2Definition.setDescription(oAuth2Config.description());
        swagger.addSecurityDefinition(oAuth2Config.key(), oAuth2Definition);
    }
    for (ApiKeyAuthDefinition[] apiKeyAuthConfigs : new ApiKeyAuthDefinition[][] { config.securityDefinition().apiKeyAuthDefintions(), config.securityDefinition().apiKeyAuthDefinitions() }) {
        for (ApiKeyAuthDefinition apiKeyAuthConfig : apiKeyAuthConfigs) {
            io.swagger.models.auth.ApiKeyAuthDefinition apiKeyAuthDefinition = new io.swagger.models.auth.ApiKeyAuthDefinition();
            apiKeyAuthDefinition.setName(apiKeyAuthConfig.name());
            apiKeyAuthDefinition.setIn(In.forValue(apiKeyAuthConfig.in().toValue()));
            apiKeyAuthDefinition.setDescription(apiKeyAuthConfig.description());
            swagger.addSecurityDefinition(apiKeyAuthConfig.key(), apiKeyAuthDefinition);
        }
    }
    for (BasicAuthDefinition[] basicAuthConfigs : new BasicAuthDefinition[][] { config.securityDefinition().basicAuthDefinions(), config.securityDefinition().basicAuthDefinitions() }) {
        for (BasicAuthDefinition basicAuthConfig : basicAuthConfigs) {
            io.swagger.models.auth.BasicAuthDefinition basicAuthDefinition = new io.swagger.models.auth.BasicAuthDefinition();
            basicAuthDefinition.setDescription(basicAuthConfig.description());
            swagger.addSecurityDefinition(basicAuthConfig.key(), basicAuthDefinition);
        }
    }
    if (!config.externalDocs().value().isEmpty()) {
        ExternalDocs externalDocs = swagger.getExternalDocs();
        if (externalDocs == null) {
            externalDocs = new ExternalDocs();
            swagger.setExternalDocs(externalDocs);
        }
        externalDocs.setDescription(config.externalDocs().value());
        if (!config.externalDocs().url().isEmpty()) {
            externalDocs.setUrl(config.externalDocs().url());
        }
    }
    for (io.swagger.annotations.Tag tagConfig : config.tags()) {
        if (!tagConfig.name().isEmpty()) {
            Tag tag = new Tag();
            tag.setName(tagConfig.name());
            tag.setDescription(tagConfig.description());
            if (!tagConfig.externalDocs().value().isEmpty()) {
                tag.setExternalDocs(new ExternalDocs(tagConfig.externalDocs().value(), tagConfig.externalDocs().url()));
            }
            tag.getVendorExtensions().putAll(BaseReaderUtils.parseExtensions(tagConfig.extensions()));
            swagger.addTag(tag);
        }
    }
    for (SwaggerDefinition.Scheme scheme : config.schemes()) {
        if (scheme != SwaggerDefinition.Scheme.DEFAULT) {
            swagger.addScheme(Scheme.forValue(scheme.name()));
        }
    }
}
Also used : OAuth2Definition(io.swagger.annotations.OAuth2Definition) BasicAuthDefinition(io.swagger.annotations.BasicAuthDefinition) ExternalDocs(io.swagger.models.ExternalDocs) ApiKeyAuthDefinition(io.swagger.annotations.ApiKeyAuthDefinition) AuthorizationScope(io.swagger.annotations.AuthorizationScope) Scope(io.swagger.annotations.Scope) Tag(io.swagger.models.Tag) SwaggerDefinition(io.swagger.annotations.SwaggerDefinition)

Aggregations

Tag (io.swagger.models.Tag)10 Swagger (io.swagger.models.Swagger)4 SwaggerDefinition (io.swagger.annotations.SwaggerDefinition)3 LinkedHashMap (java.util.LinkedHashMap)3 LinkedHashSet (java.util.LinkedHashSet)3 AnnotatedParameter (com.fasterxml.jackson.databind.introspect.AnnotatedParameter)2 ApiKeyAuthDefinition (io.swagger.annotations.ApiKeyAuthDefinition)2 AuthorizationScope (io.swagger.annotations.AuthorizationScope)2 BasicAuthDefinition (io.swagger.annotations.BasicAuthDefinition)2 OAuth2Definition (io.swagger.annotations.OAuth2Definition)2 Scope (io.swagger.annotations.Scope)2 ExternalDocs (io.swagger.models.ExternalDocs)2 Operation (io.swagger.models.Operation)2 Path (io.swagger.models.Path)2 FormParameter (io.swagger.models.parameters.FormParameter)2 HeaderParameter (io.swagger.models.parameters.HeaderParameter)2 Parameter (io.swagger.models.parameters.Parameter)2 PathParameter (io.swagger.models.parameters.PathParameter)2 QueryParameter (io.swagger.models.parameters.QueryParameter)2 HashMap (java.util.HashMap)2