Search in sources :

Example 91 with Parameter

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

the class APIDefinitionFromSwagger20 method generateMergedResourceDefinition.

@Override
public String generateMergedResourceDefinition(String resourceConfigJson, API api) {
    SwaggerParser swaggerParser = new SwaggerParser();
    Swagger swagger = swaggerParser.parse(resourceConfigJson);
    addSecuritySchemeToSwaggerDefinition(swagger, api);
    String securityName = getOauthSecurityName(swagger);
    if (!StringUtils.isEmpty(securityName)) {
        List<SecurityRequirement> securityRequirements = swagger.getSecurity();
        if (securityRequirements != null) {
            for (SecurityRequirement securityRequirement : securityRequirements) {
                Map<String, List<String>> requirementMap = securityRequirement.getRequirements();
                if (requirementMap.containsKey(securityName)) {
                    requirementMap.replace(securityName, api.getScopes());
                } else {
                    if (!api.getScopes().isEmpty()) {
                        requirementMap.put(securityName, api.getScopes());
                    }
                }
            }
        } else {
            if (!api.getScopes().isEmpty()) {
                SecurityRequirement securityRequirement = new SecurityRequirement();
                securityRequirement.setRequirements(securityName, api.getScopes());
                swagger.addSecurity(securityRequirement);
            }
        }
        Map<String, UriTemplate> uriTemplateMap = new HashMap<>(api.getUriTemplates());
        swagger.getPaths().entrySet().removeIf(entry -> isPathNotExist(uriTemplateMap, securityName, entry));
        uriTemplateMap.forEach((k, v) -> {
            Path path = swagger.getPath(v.getUriTemplate());
            if (path == null) {
                path = new Path();
                swagger.path(v.getUriTemplate(), path);
            }
            Map<HttpMethod, Operation> operationMap = path.getOperationMap();
            Operation operation = operationMap.get(getHttpMethodForVerb(v.getHttpVerb().toUpperCase()));
            if (operation != null) {
                assignScopesToOperation(operation, securityName, v.getScopes());
            } else {
                Operation operationTocCreate = new Operation();
                operationTocCreate.addSecurity(securityName, v.getScopes());
                operationTocCreate.addResponse("200", getDefaultResponse());
                List<Parameter> parameterList = getParameters(v.getUriTemplate());
                if (!HttpMethod.GET.toString().equalsIgnoreCase(v.getHttpVerb()) && !HttpMethod.DELETE.toString().equalsIgnoreCase(v.getHttpVerb()) && !HttpMethod.OPTIONS.toString().equalsIgnoreCase(v.getHttpVerb()) && !HttpMethod.HEAD.toString().equalsIgnoreCase(v.getHttpVerb())) {
                    parameterList.add(getDefaultBodyParameter());
                }
                operationTocCreate.setParameters(parameterList);
                path.set(v.getHttpVerb().toLowerCase(), operationTocCreate);
            }
        });
    }
    return Json.pretty(swagger);
}
Also used : Path(io.swagger.models.Path) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Operation(io.swagger.models.Operation) UriTemplate(org.wso2.carbon.apimgt.core.models.UriTemplate) SwaggerParser(io.swagger.parser.SwaggerParser) Swagger(io.swagger.models.Swagger) FormParameter(io.swagger.models.parameters.FormParameter) PathParameter(io.swagger.models.parameters.PathParameter) Parameter(io.swagger.models.parameters.Parameter) QueryParameter(io.swagger.models.parameters.QueryParameter) BodyParameter(io.swagger.models.parameters.BodyParameter) List(java.util.List) ArrayList(java.util.ArrayList) HttpMethod(io.swagger.models.HttpMethod) SecurityRequirement(io.swagger.models.SecurityRequirement)

Example 92 with Parameter

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

the class APIDefinitionFromSwagger20 method generateSwaggerFromResources.

/**
 * generate the swagger from uri templates.
 *
 * @param api API Object
 * @return Generated swagger resources as string.
 */
@Override
public String generateSwaggerFromResources(API.APIBuilder api) {
    Swagger swagger = new Swagger();
    Info info = new Info();
    info.setTitle(api.getName());
    info.setDescription(api.getDescription());
    Contact contact = new Contact();
    if (api.getBusinessInformation() != null) {
        BusinessInformation businessInformation = api.getBusinessInformation();
        contact.setName(businessInformation.getBusinessOwner());
        contact.setEmail(businessInformation.getBusinessOwnerEmail());
    }
    info.setContact(contact);
    info.setVersion(api.getVersion());
    swagger.setInfo(info);
    addSecuritySchemeToSwaggerDefinition(swagger, api.build());
    Map<String, Path> stringPathMap = new HashMap();
    for (UriTemplate uriTemplate : api.getUriTemplates().values()) {
        String uriTemplateString = uriTemplate.getUriTemplate();
        List<Parameter> parameterList = getParameters(uriTemplateString);
        if (uriTemplate.getParameters() == null || uriTemplate.getParameters().isEmpty()) {
            if (!HttpMethod.GET.toString().equalsIgnoreCase(uriTemplate.getHttpVerb()) && !HttpMethod.DELETE.toString().equalsIgnoreCase(uriTemplate.getHttpVerb()) && !HttpMethod.OPTIONS.toString().equalsIgnoreCase(uriTemplate.getHttpVerb()) && !HttpMethod.HEAD.toString().equalsIgnoreCase(uriTemplate.getHttpVerb())) {
                parameterList.add(getDefaultBodyParameter());
            }
        } else {
            for (URITemplateParam uriTemplateParam : uriTemplate.getParameters()) {
                Parameter parameter = getParameterFromURITemplateParam(uriTemplateParam);
                parameterList.add(parameter);
            }
        }
        Operation operation = new Operation();
        operation.setParameters(parameterList);
        operation.setOperationId(uriTemplate.getTemplateId());
        // having content types like */* can break swagger definition
        if (!StringUtils.isEmpty(uriTemplate.getContentType()) && !uriTemplate.getContentType().contains("*")) {
            List<String> consumesList = new ArrayList<>();
            consumesList.add(uriTemplate.getContentType());
            operation.setConsumes(consumesList);
        }
        operation.addResponse("200", getDefaultResponse());
        if (!APIMgtConstants.AUTH_NO_AUTHENTICATION.equals(uriTemplate.getAuthType()) && ((api.getSecurityScheme() & 2) == 2)) {
            log.debug("API security scheme : API Key Scheme ---- Resource Auth Type : Not None");
            operation.addSecurity(APIMgtConstants.SWAGGER_APIKEY, null);
        }
        if (!APIMgtConstants.AUTH_NO_AUTHENTICATION.equals(uriTemplate.getAuthType()) && ((api.getSecurityScheme() & 1) == 1)) {
            log.debug("API security scheme : Oauth Scheme ---- Resource Auth Type : Not None");
            operation.addSecurity(APIMgtConstants.SWAGGER_OAUTH2, null);
        }
        if (stringPathMap.containsKey(uriTemplateString)) {
            Path path = stringPathMap.get(uriTemplateString);
            path.set(uriTemplate.getHttpVerb().toLowerCase(), operation);
        } else {
            Path path = new Path();
            path.set(uriTemplate.getHttpVerb().toLowerCase(), operation);
            stringPathMap.put(uriTemplateString, path);
        }
    }
    swagger.setPaths(stringPathMap);
    swagger.setPaths(stringPathMap);
    return Json.pretty(swagger);
}
Also used : Path(io.swagger.models.Path) BusinessInformation(org.wso2.carbon.apimgt.core.models.BusinessInformation) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Operation(io.swagger.models.Operation) Info(io.swagger.models.Info) ServiceMethodInfo(org.wso2.msf4j.ServiceMethodInfo) UriTemplate(org.wso2.carbon.apimgt.core.models.UriTemplate) Contact(io.swagger.models.Contact) Swagger(io.swagger.models.Swagger) FormParameter(io.swagger.models.parameters.FormParameter) PathParameter(io.swagger.models.parameters.PathParameter) Parameter(io.swagger.models.parameters.Parameter) QueryParameter(io.swagger.models.parameters.QueryParameter) BodyParameter(io.swagger.models.parameters.BodyParameter) URITemplateParam(org.wso2.carbon.apimgt.core.models.URITemplateParam)

Aggregations

Parameter (io.swagger.models.parameters.Parameter)92 BodyParameter (io.swagger.models.parameters.BodyParameter)54 QueryParameter (io.swagger.models.parameters.QueryParameter)53 PathParameter (io.swagger.models.parameters.PathParameter)51 Test (org.testng.annotations.Test)51 HeaderParameter (io.swagger.models.parameters.HeaderParameter)38 Swagger (io.swagger.models.Swagger)33 FormParameter (io.swagger.models.parameters.FormParameter)27 Operation (io.swagger.models.Operation)20 SerializableParameter (io.swagger.models.parameters.SerializableParameter)15 Type (java.lang.reflect.Type)14 ArrayList (java.util.ArrayList)14 Annotation (java.lang.annotation.Annotation)10 Path (io.swagger.models.Path)9 Method (java.lang.reflect.Method)9 JavaType (com.fasterxml.jackson.databind.JavaType)7 ContextParameter (io.servicecomb.swagger.extend.parameter.ContextParameter)6 Property (io.swagger.models.properties.Property)6 HashMap (java.util.HashMap)6 AnnotatedParameter (com.fasterxml.jackson.databind.introspect.AnnotatedParameter)5