Search in sources :

Example 31 with Tags

use of io.swagger.v3.oas.annotations.tags.Tags in project swagger-core by swagger-api.

the class SpecFilter method filter.

public OpenAPI filter(OpenAPI openAPI, OpenAPISpecFilter filter, Map<String, List<String>> params, Map<String, String> cookies, Map<String, List<String>> headers) {
    OpenAPI filteredOpenAPI = filterOpenAPI(filter, openAPI, params, cookies, headers);
    if (filteredOpenAPI == null) {
        return filteredOpenAPI;
    }
    OpenAPI clone = new OpenAPI();
    clone.info(filteredOpenAPI.getInfo());
    clone.openapi(filteredOpenAPI.getOpenapi());
    clone.setExtensions(filteredOpenAPI.getExtensions());
    clone.setExternalDocs(filteredOpenAPI.getExternalDocs());
    clone.setSecurity(filteredOpenAPI.getSecurity());
    clone.setServers(filteredOpenAPI.getServers());
    clone.tags(filteredOpenAPI.getTags() == null ? null : new ArrayList<>(openAPI.getTags()));
    final Set<String> allowedTags = new HashSet<>();
    final Set<String> filteredTags = new HashSet<>();
    Paths clonedPaths = new Paths();
    if (filteredOpenAPI.getPaths() != null) {
        for (String resourcePath : filteredOpenAPI.getPaths().keySet()) {
            PathItem pathItem = filteredOpenAPI.getPaths().get(resourcePath);
            PathItem filteredPathItem = filterPathItem(filter, pathItem, resourcePath, params, cookies, headers);
            if (filteredPathItem != null) {
                PathItem clonedPathItem = new PathItem();
                clonedPathItem.set$ref(filteredPathItem.get$ref());
                clonedPathItem.setDescription(filteredPathItem.getDescription());
                clonedPathItem.setSummary(filteredPathItem.getSummary());
                clonedPathItem.setExtensions(filteredPathItem.getExtensions());
                clonedPathItem.setParameters(filteredPathItem.getParameters());
                clonedPathItem.setServers(filteredPathItem.getServers());
                Map<PathItem.HttpMethod, Operation> ops = filteredPathItem.readOperationsMap();
                for (Map.Entry<PathItem.HttpMethod, Operation> entry : ops.entrySet()) {
                    PathItem.HttpMethod key = entry.getKey();
                    Operation op = entry.getValue();
                    List<String> opTagsBeforeFilter = null;
                    if (op.getTags() != null) {
                        opTagsBeforeFilter = new ArrayList<>(op.getTags());
                    } else {
                        opTagsBeforeFilter = new ArrayList<>();
                    }
                    op = filterOperation(filter, op, resourcePath, key.toString(), params, cookies, headers);
                    clonedPathItem.operation(key, op);
                    if (op == null) {
                        filteredTags.addAll(opTagsBeforeFilter);
                    } else {
                        if (op.getTags() != null) {
                            opTagsBeforeFilter.removeAll(op.getTags());
                            allowedTags.addAll(op.getTags());
                        }
                        filteredTags.addAll(opTagsBeforeFilter);
                    }
                }
                if (!clonedPathItem.readOperations().isEmpty()) {
                    clonedPaths.addPathItem(resourcePath, clonedPathItem);
                }
            }
        }
        clone.paths(clonedPaths);
    }
    filteredTags.removeAll(allowedTags);
    final List<Tag> tags = clone.getTags();
    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);
        }
    }
    if (filteredOpenAPI.getComponents() != null) {
        clone.components(new Components());
        clone.getComponents().setSchemas(filterComponentsSchema(filter, filteredOpenAPI.getComponents().getSchemas(), params, cookies, headers));
        clone.getComponents().setSecuritySchemes(filteredOpenAPI.getComponents().getSecuritySchemes());
        clone.getComponents().setCallbacks(filteredOpenAPI.getComponents().getCallbacks());
        clone.getComponents().setExamples(filteredOpenAPI.getComponents().getExamples());
        clone.getComponents().setExtensions(filteredOpenAPI.getComponents().getExtensions());
        clone.getComponents().setHeaders(filteredOpenAPI.getComponents().getHeaders());
        clone.getComponents().setLinks(filteredOpenAPI.getComponents().getLinks());
        clone.getComponents().setParameters(filteredOpenAPI.getComponents().getParameters());
        clone.getComponents().setRequestBodies(filteredOpenAPI.getComponents().getRequestBodies());
        clone.getComponents().setResponses(filteredOpenAPI.getComponents().getResponses());
    }
    if (filter.isRemovingUnreferencedDefinitions()) {
        clone = removeBrokenReferenceDefinitions(clone);
    }
    return clone;
}
Also used : ArrayList(java.util.ArrayList) Operation(io.swagger.v3.oas.models.Operation) Components(io.swagger.v3.oas.models.Components) PathItem(io.swagger.v3.oas.models.PathItem) Paths(io.swagger.v3.oas.models.Paths) Tag(io.swagger.v3.oas.models.tags.Tag) OpenAPI(io.swagger.v3.oas.models.OpenAPI) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 32 with Tags

use of io.swagger.v3.oas.annotations.tags.Tags in project swagger-core by swagger-api.

the class PetData method findPetByTags.

public List<Pet> findPetByTags(String tags) {
    String[] tagList = tags.split(",");
    List<Pet> result = new ArrayList<Pet>();
    for (Pet pet : pets) {
        if (null != pet.getTags()) {
            for (Tag tag : pet.getTags()) {
                for (String tagListString : tagList) {
                    if (tagListString.equals(tag.getName())) {
                        result.add(pet);
                    }
                }
            }
        }
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) Tag(io.swagger.v3.plugins.gradle.resources.model.Tag) Pet(io.swagger.v3.plugins.gradle.resources.model.Pet)

Example 33 with Tags

use of io.swagger.v3.oas.annotations.tags.Tags in project swagger-core by swagger-api.

the class PetData method findPetByTags.

public List<Pet> findPetByTags(String tags) {
    String[] tagList = tags.split(",");
    List<Pet> result = new ArrayList<Pet>();
    for (Pet pet : pets) {
        if (null != pet.getTags()) {
            for (Tag tag : pet.getTags()) {
                for (String tagListString : tagList) {
                    if (tagListString.equals(tag.getName())) {
                        result.add(pet);
                    }
                }
            }
        }
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) Tag(io.swagger.v3.jaxrs2.resources.model.Tag) Pet(io.swagger.v3.jaxrs2.resources.model.Pet)

Example 34 with Tags

use of io.swagger.v3.oas.annotations.tags.Tags in project swagger-core by swagger-api.

the class PetData method createPet.

static Pet createPet(long id, Category cat, String name, String[] urls, String[] tags, String status) {
    Pet pet = new Pet();
    pet.setId(id);
    pet.setCategory(cat);
    pet.setName(name);
    if (null != urls) {
        List<String> urlObjs = new ArrayList<String>();
        for (String urlString : urls) {
            urlObjs.add(urlString);
        }
        pet.setPhotoUrls(urlObjs);
    }
    List<Tag> tagObjs = new ArrayList<Tag>();
    int i = 0;
    if (null != tags) {
        for (String tagString : tags) {
            i = i + 1;
            Tag tag = new Tag();
            tag.setId(i);
            tag.setName(tagString);
            tagObjs.add(tag);
        }
    }
    pet.setTags(tagObjs);
    pet.setStatus(status);
    return pet;
}
Also used : ArrayList(java.util.ArrayList) Tag(io.swagger.v3.jaxrs2.resources.model.Tag) Pet(io.swagger.v3.jaxrs2.resources.model.Pet)

Example 35 with Tags

use of io.swagger.v3.oas.annotations.tags.Tags in project gravitee-management-rest-api by gravitee-io.

the class OAIToAPIConverter method visit.

@Override
public SwaggerApiEntity visit(OpenAPI oai) {
    final SwaggerApiEntity apiEntity = new SwaggerApiEntity();
    // Name
    apiEntity.setName(oai.getInfo().getTitle());
    // Description
    apiEntity.setDescription(oai.getInfo().getDescription() == null ? "Description of " + apiEntity.getName() : oai.getInfo().getDescription());
    // Version
    apiEntity.setVersion(oai.getInfo().getVersion());
    fill(apiEntity, oai);
    // Use X-Gravitee to add information in API
    XGraviteeIODefinition xGraviteeIODefinition = null;
    if (oai.getExtensions() != null && oai.getExtensions().get(X_GRAVITEEIO_DEFINITION_VENDOR_EXTENSION) != null) {
        xGraviteeIODefinition = new ObjectMapper().convertValue(oai.getExtensions().get(X_GRAVITEEIO_DEFINITION_VENDOR_EXTENSION), XGraviteeIODefinition.class);
    }
    // Proxy
    Proxy proxy = new Proxy();
    String defaultEndpoint = null;
    // Proxy - Endpoints
    if (!oai.getServers().isEmpty()) {
        List<String> evaluatedServerUrl = mapServersToEndpoint(oai.getServers());
        EndpointGroup defaultGroup = new EndpointGroup();
        defaultGroup.setName("default-group");
        if (evaluatedServerUrl == null) {
            defaultGroup.setEndpoints(singleton(new HttpEndpoint("default", defaultEndpoint)));
        } else if (evaluatedServerUrl.size() == 1) {
            defaultEndpoint = evaluatedServerUrl.get(0);
            defaultGroup.setEndpoints(singleton(new HttpEndpoint("default", defaultEndpoint)));
        } else {
            defaultEndpoint = evaluatedServerUrl.get(0);
            defaultGroup.setEndpoints(new HashSet<>());
            for (int i = 0; i < evaluatedServerUrl.size(); i++) {
                defaultGroup.getEndpoints().add(new HttpEndpoint("server" + (i + 1), evaluatedServerUrl.get(i)));
            }
        }
        proxy.setGroups(singleton(defaultGroup));
        apiEntity.setProxy(proxy);
    }
    // Proxy - Context Path / Virtual Host
    if (xGraviteeIODefinition != null && xGraviteeIODefinition.getVirtualHosts() != null) {
        proxy.setVirtualHosts(xGraviteeIODefinition.getVirtualHosts().stream().map(vHost -> new VirtualHost(vHost.getHost(), vHost.getPath(), (vHost.getOverrideEntrypoint() != null ? vHost.getOverrideEntrypoint() : false))).collect(Collectors.toList()));
    } else {
        String contextPath = null;
        if (defaultEndpoint != null) {
            contextPath = URI.create(defaultEndpoint).getPath();
        }
        if (contextPath == null || contextPath.equals("/")) {
            contextPath = apiEntity.getName().replaceAll("\\s+", "").toLowerCase();
        }
        proxy.setVirtualHosts(singletonList(new VirtualHost(contextPath)));
    }
    apiEntity.setProxy(proxy);
    // Add xGraviteeIODefinition config
    if (xGraviteeIODefinition != null) {
        // Categories
        if (xGraviteeIODefinition.getCategories() != null && !xGraviteeIODefinition.getCategories().isEmpty()) {
            apiEntity.setCategories(new HashSet<>(xGraviteeIODefinition.getCategories()));
        }
        // Groups
        if (xGraviteeIODefinition.getGroups() != null && !xGraviteeIODefinition.getGroups().isEmpty()) {
            // Groups in schema are group name. Replace them by id
            Set<String> groupIdsToImport = xGraviteeIODefinition.getGroups().stream().flatMap(group -> groupService.findByName(group).stream()).map(GroupEntity::getId).collect(Collectors.toSet());
            apiEntity.setGroups(groupIdsToImport);
        }
        // Labels
        if (xGraviteeIODefinition.getLabels() != null && !xGraviteeIODefinition.getLabels().isEmpty()) {
            apiEntity.setLabels(xGraviteeIODefinition.getLabels());
        }
        // Metadata
        if (xGraviteeIODefinition.getMetadata() != null && !xGraviteeIODefinition.getMetadata().isEmpty()) {
            final List<ApiMetadataEntity> apiMetadataEntities = xGraviteeIODefinition.getMetadata().stream().map(metadata -> {
                ApiMetadataEntity apiMetadata = new ApiMetadataEntity();
                apiMetadata.setKey(IdGenerator.generate(metadata.getName()));
                apiMetadata.setName(metadata.getName());
                apiMetadata.setValue(metadata.getValue());
                apiMetadata.setFormat(metadata.getFormat() != null ? MetadataFormat.valueOf(metadata.getFormat().name()) : MetadataFormat.STRING);
                return apiMetadata;
            }).collect(Collectors.toList());
            apiEntity.setMetadata(apiMetadataEntities);
        }
        // Picture
        if (xGraviteeIODefinition.getPicture() != null && !StringUtils.isEmpty(xGraviteeIODefinition.getPicture())) {
            if (xGraviteeIODefinition.getPicture().matches(PICTURE_REGEX)) {
                apiEntity.setPicture(xGraviteeIODefinition.getPicture());
            }
        }
        // Properties
        if (xGraviteeIODefinition.getProperties() != null && !xGraviteeIODefinition.getProperties().isEmpty()) {
            Properties properties = new Properties();
            properties.setProperties(xGraviteeIODefinition.getProperties().stream().map(prop -> new Property(prop.getKey(), prop.getValue())).collect(Collectors.toList()));
            apiEntity.setProperties(properties);
        }
        // Tags
        if (xGraviteeIODefinition.getTags() != null && !xGraviteeIODefinition.getTags().isEmpty()) {
            final Map<String, String> tagMap = tagService.findByReference(GraviteeContext.getCurrentOrganization(), TagReferenceType.ORGANIZATION).stream().collect(toMap(TagEntity::getId, TagEntity::getName));
            final Set<String> tagIdToAdd = xGraviteeIODefinition.getTags().stream().map(tag -> findTagIdByName(tagMap, tag)).filter(Objects::nonNull).collect(Collectors.toSet());
            if (tagIdToAdd != null && !tagIdToAdd.isEmpty()) {
                apiEntity.setTags(tagIdToAdd);
            }
        }
        // Visibility
        if (xGraviteeIODefinition.getVisibility() != null) {
            apiEntity.setVisibility(Visibility.valueOf(xGraviteeIODefinition.getVisibility().name()));
        }
    }
    return apiEntity;
}
Also used : java.util(java.util) Properties(io.gravitee.definition.model.Properties) GraviteeContext(io.gravitee.rest.api.service.common.GraviteeContext) Operation(io.swagger.v3.oas.models.Operation) PolicyOperationVisitorManager(io.gravitee.rest.api.service.impl.swagger.policy.PolicyOperationVisitorManager) StringUtils(org.apache.commons.lang3.StringUtils) Collections.singletonList(java.util.Collections.singletonList) OAIOperationVisitor(io.gravitee.rest.api.service.impl.swagger.visitor.v3.OAIOperationVisitor) PolicyHelper.clearNullValues(io.gravitee.rest.api.service.validator.PolicyHelper.clearNullValues) IdGenerator(io.gravitee.common.utils.IdGenerator) Matcher(java.util.regex.Matcher) Collections.singleton(java.util.Collections.singleton) Collectors.toMap(java.util.stream.Collectors.toMap) OpenAPI(io.swagger.v3.oas.models.OpenAPI) io.gravitee.rest.api.model(io.gravitee.rest.api.model) HttpEndpoint(io.gravitee.definition.model.endpoint.HttpEndpoint) ServerVariables(io.swagger.v3.oas.models.servers.ServerVariables) URI(java.net.URI) GroupService(io.gravitee.rest.api.service.GroupService) XGraviteeIODefinition(io.gravitee.rest.api.service.swagger.converter.extension.XGraviteeIODefinition) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) PathItem(io.swagger.v3.oas.models.PathItem) OAIDescriptor(io.gravitee.rest.api.service.swagger.OAIDescriptor) io.gravitee.definition.model(io.gravitee.definition.model) Collectors(java.util.stream.Collectors) OAIDescriptorVisitor(io.gravitee.rest.api.service.impl.swagger.visitor.v3.OAIDescriptorVisitor) Consumer(java.util.function.Consumer) Server(io.swagger.v3.oas.models.servers.Server) HttpMethod(io.gravitee.common.http.HttpMethod) CollectionUtils(org.springframework.util.CollectionUtils) SwaggerApiEntity(io.gravitee.rest.api.model.api.SwaggerApiEntity) ServerVariable(io.swagger.v3.oas.models.servers.ServerVariable) Pattern(java.util.regex.Pattern) Policy(io.gravitee.policy.api.swagger.Policy) TagService(io.gravitee.rest.api.service.TagService) Properties(io.gravitee.definition.model.Properties) HttpEndpoint(io.gravitee.definition.model.endpoint.HttpEndpoint) XGraviteeIODefinition(io.gravitee.rest.api.service.swagger.converter.extension.XGraviteeIODefinition) SwaggerApiEntity(io.gravitee.rest.api.model.api.SwaggerApiEntity) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

OpenAPI (io.swagger.v3.oas.models.OpenAPI)20 Test (org.testng.annotations.Test)15 ArrayList (java.util.ArrayList)11 Operation (io.swagger.v3.oas.models.Operation)9 PathItem (io.swagger.v3.oas.models.PathItem)8 Schema (io.swagger.v3.oas.models.media.Schema)8 Tag (io.swagger.v3.oas.models.tags.Tag)8 Paths (io.swagger.v3.oas.models.Paths)7 MediaType (io.swagger.v3.oas.models.media.MediaType)5 Parameter (io.swagger.v3.oas.models.parameters.Parameter)5 ApiResponse (io.swagger.v3.oas.models.responses.ApiResponse)5 ApiResponses (io.swagger.v3.oas.models.responses.ApiResponses)5 Callback (io.swagger.v3.oas.models.callbacks.Callback)4 Content (io.swagger.v3.oas.models.media.Content)4 RequestBody (io.swagger.v3.oas.models.parameters.RequestBody)4 SecurityScheme (io.swagger.v3.oas.models.security.SecurityScheme)4 List (java.util.List)4 Map (java.util.Map)4 AnnotatedType (io.swagger.v3.core.converter.AnnotatedType)3 IntegerSchema (io.swagger.v3.oas.models.media.IntegerSchema)3