Search in sources :

Example 41 with Paths

use of io.swagger.v3.oas.models.Paths in project carbon-apimgt by wso2.

the class RestApiCommonUtil method generateOpenAPIForAsync.

/**
 * Generate a basic OpenAPI definition with given details.
 *
 * @param name             name of the API.
 * @param version          version of the API.
 * @param context          context of the API.
 * @param callbackEndpoint callback URL of the async API.
 * @return OpenAPI definition as String.
 * @throws JsonProcessingException Error occurred while generating the OpenAPI.
 */
public static String generateOpenAPIForAsync(String name, String version, String context, String callbackEndpoint) throws JsonProcessingException {
    OpenAPI openAPI = new OpenAPI();
    Info info = new Info();
    info.setTitle(name);
    info.setDescription("API Definition of " + name);
    info.setVersion(version);
    openAPI.setInfo(info);
    ArrayList<Server> servers = new ArrayList<>();
    Server server = new Server();
    server.setUrl("/");
    servers.add(server);
    openAPI.setServers(Arrays.asList(server));
    Paths paths = new Paths();
    PathItem pathItem = new PathItem();
    Operation operation = new Operation();
    ApiResponses apiResponses = new ApiResponses();
    ApiResponse apiResponse = new ApiResponse();
    apiResponse.setDescription("Default response");
    apiResponses.addApiResponse("default", apiResponse);
    operation.setResponses(apiResponses);
    pathItem.setPost(operation);
    paths.addPathItem("/*", pathItem);
    openAPI.paths(paths);
    List<String> urls = new ArrayList<>();
    urls.add(callbackEndpoint);
    Map<String, Object> tempMap = new HashMap();
    tempMap.put("type", "http");
    tempMap.put("urls", urls);
    openAPI.addExtension(X_WSO2_PRODUCTION_ENDPOINTS, tempMap);
    openAPI.addExtension(X_WSO2_SANDBOX_ENDPOINTS, tempMap);
    openAPI.addExtension(X_WSO2_AUTH_HEADER, "Authorization");
    openAPI.addExtension(X_WSO2_BASEPATH, context + "/" + version);
    openAPI.addExtension(X_WSO2_DISABLE_SECURITY, true);
    return Json.mapper().writeValueAsString(openAPI);
}
Also used : Server(io.swagger.v3.oas.models.servers.Server) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Operation(io.swagger.v3.oas.models.Operation) Info(io.swagger.v3.oas.models.info.Info) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse) PathItem(io.swagger.v3.oas.models.PathItem) Paths(io.swagger.v3.oas.models.Paths) OpenAPI(io.swagger.v3.oas.models.OpenAPI) ApiResponses(io.swagger.v3.oas.models.responses.ApiResponses)

Example 42 with Paths

use of io.swagger.v3.oas.models.Paths in project carbon-apimgt by wso2.

the class OAS3Parser method addOrUpdatePathToSwagger.

/**
 * Add a new path based on the provided URI template to swagger if it does not exists. If it exists,
 * adds the respective operation to the existing path
 *
 * @param openAPI  swagger object
 * @param resource API resource data
 */
private void addOrUpdatePathToSwagger(OpenAPI openAPI, SwaggerData.Resource resource) {
    PathItem path;
    if (openAPI.getPaths() == null) {
        openAPI.setPaths(new Paths());
    }
    if (openAPI.getPaths().get(resource.getPath()) != null) {
        path = openAPI.getPaths().get(resource.getPath());
    } else {
        path = new PathItem();
    }
    Operation operation = createOperation(resource);
    PathItem.HttpMethod httpMethod = PathItem.HttpMethod.valueOf(resource.getVerb());
    path.operation(httpMethod, operation);
    openAPI.getPaths().addPathItem(resource.getPath(), path);
}
Also used : PathItem(io.swagger.v3.oas.models.PathItem) Paths(io.swagger.v3.oas.models.Paths) Operation(io.swagger.v3.oas.models.Operation)

Example 43 with Paths

use of io.swagger.v3.oas.models.Paths in project carbon-apimgt by wso2.

the class OAS3Parser method modifyGraphQLSwagger.

/**
 * Construct openAPI definition for graphQL. Add get and post operations
 *
 * @param openAPI OpenAPI
 * @return modified openAPI for GraphQL
 */
private void modifyGraphQLSwagger(OpenAPI openAPI) {
    SwaggerData.Resource resource = new SwaggerData.Resource();
    resource.setAuthType(APIConstants.AUTH_APPLICATION_OR_USER_LEVEL_TOKEN);
    resource.setPolicy(APIConstants.DEFAULT_SUB_POLICY_UNLIMITED);
    resource.setPath("/");
    resource.setVerb(APIConstants.HTTP_POST);
    Operation postOperation = createOperation(resource);
    // post operation
    RequestBody requestBody = new RequestBody();
    requestBody.setDescription("Query or mutation to be passed to graphQL API");
    requestBody.setRequired(true);
    JSONObject typeOfPayload = new JSONObject();
    JSONObject payload = new JSONObject();
    typeOfPayload.put(APIConstants.TYPE, APIConstants.STRING);
    payload.put(APIConstants.OperationParameter.PAYLOAD_PARAM_NAME, typeOfPayload);
    Schema postSchema = new Schema();
    postSchema.setType(APIConstants.OBJECT);
    postSchema.setProperties(payload);
    MediaType mediaType = new MediaType();
    mediaType.setSchema(postSchema);
    Content content = new Content();
    content.addMediaType(APPLICATION_JSON_MEDIA_TYPE, mediaType);
    requestBody.setContent(content);
    postOperation.setRequestBody(requestBody);
    // add post and get operations to path /*
    PathItem pathItem = new PathItem();
    pathItem.setPost(postOperation);
    Paths paths = new Paths();
    paths.put("/", pathItem);
    openAPI.setPaths(paths);
}
Also used : PathItem(io.swagger.v3.oas.models.PathItem) JSONObject(org.json.simple.JSONObject) SwaggerData(org.wso2.carbon.apimgt.api.model.SwaggerData) Content(io.swagger.v3.oas.models.media.Content) Schema(io.swagger.v3.oas.models.media.Schema) MediaType(io.swagger.v3.oas.models.media.MediaType) Operation(io.swagger.v3.oas.models.Operation) Paths(io.swagger.v3.oas.models.Paths) RequestBody(io.swagger.v3.oas.models.parameters.RequestBody)

Example 44 with Paths

use of io.swagger.v3.oas.models.Paths in project carbon-apimgt by wso2.

the class OAS3Parser method removeExamplesFromOpenAPI.

/**
 * Remove x-examples from all the paths from the OpenAPI definition.
 *
 * @param apiDefinition OpenAPI definition as String
 */
public static String removeExamplesFromOpenAPI(String apiDefinition) throws APIManagementException {
    try {
        OpenAPIV3Parser openAPIV3Parser = new OpenAPIV3Parser();
        SwaggerParseResult parseAttemptForV3 = openAPIV3Parser.readContents(apiDefinition, null, null);
        if (CollectionUtils.isNotEmpty(parseAttemptForV3.getMessages())) {
            log.debug("Errors found when parsing OAS definition");
        }
        OpenAPI openAPI = parseAttemptForV3.getOpenAPI();
        for (Map.Entry<String, PathItem> entry : openAPI.getPaths().entrySet()) {
            String path = entry.getKey();
            List<Operation> operations = openAPI.getPaths().get(path).readOperations();
            for (Operation operation : operations) {
                if (operation.getExtensions() != null && operation.getExtensions().keySet().contains(APIConstants.SWAGGER_X_EXAMPLES)) {
                    operation.getExtensions().remove(APIConstants.SWAGGER_X_EXAMPLES);
                }
            }
        }
        return Yaml.pretty().writeValueAsString(openAPI);
    } catch (JsonProcessingException e) {
        throw new APIManagementException("Error while removing examples from OpenAPI definition", e, ExceptionCodes.ERROR_REMOVING_EXAMPLES);
    }
}
Also used : PathItem(io.swagger.v3.oas.models.PathItem) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) Operation(io.swagger.v3.oas.models.Operation) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 45 with Paths

use of io.swagger.v3.oas.models.Paths in project carbon-apimgt by wso2.

the class OAS3Parser method processDisableSecurityExtension.

/**
 * This method will extractX-WSO2-disable-security extension provided in API level
 * by mgw and inject that extension to all resources in OAS file
 *
 * @param swaggerContent String
 * @return String
 * @throws APIManagementException
 */
@Override
public String processDisableSecurityExtension(String swaggerContent) throws APIManagementException {
    OpenAPI openAPI = getOpenAPI(swaggerContent);
    Map<String, Object> apiExtensions = openAPI.getExtensions();
    if (apiExtensions == null) {
        return swaggerContent;
    }
    // Check Disable Security is enabled in API level
    boolean apiLevelDisableSecurity = OASParserUtil.getDisableSecurity(apiExtensions);
    Paths paths = openAPI.getPaths();
    for (String pathKey : paths.keySet()) {
        Map<PathItem.HttpMethod, Operation> operationsMap = paths.get(pathKey).readOperationsMap();
        for (Map.Entry<PathItem.HttpMethod, Operation> entry : operationsMap.entrySet()) {
            Operation operation = entry.getValue();
            Map<String, Object> resourceExtensions = operation.getExtensions();
            boolean extensionsAreEmpty = false;
            if (apiLevelDisableSecurity) {
                if (resourceExtensions == null) {
                    resourceExtensions = new HashMap<>();
                    extensionsAreEmpty = true;
                }
                resourceExtensions.put(APIConstants.SWAGGER_X_AUTH_TYPE, "None");
                if (extensionsAreEmpty) {
                    operation.setExtensions(resourceExtensions);
                }
            } else if (resourceExtensions != null && resourceExtensions.containsKey(APIConstants.X_WSO2_DISABLE_SECURITY)) {
                // Check Disable Security is enabled in resource level
                boolean resourceLevelDisableSecurity = Boolean.parseBoolean(String.valueOf(resourceExtensions.get(APIConstants.X_WSO2_DISABLE_SECURITY)));
                if (resourceLevelDisableSecurity) {
                    resourceExtensions.put(APIConstants.SWAGGER_X_AUTH_TYPE, "None");
                }
            }
        }
    }
    return Json.pretty(openAPI);
}
Also used : JSONObject(org.json.simple.JSONObject) Paths(io.swagger.v3.oas.models.Paths) Operation(io.swagger.v3.oas.models.Operation) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) HttpMethod(io.swagger.models.HttpMethod)

Aggregations

Test (org.testng.annotations.Test)154 OpenAPI (io.swagger.v3.oas.models.OpenAPI)145 SwaggerParseResult (io.swagger.v3.parser.core.models.SwaggerParseResult)75 OpenAPIV3Parser (io.swagger.v3.parser.OpenAPIV3Parser)70 PathItem (io.swagger.v3.oas.models.PathItem)61 Operation (io.swagger.v3.oas.models.Operation)46 Paths (io.swagger.v3.oas.models.Paths)45 Schema (io.swagger.v3.oas.models.media.Schema)40 ArraySchema (io.swagger.v3.oas.models.media.ArraySchema)36 IntegerSchema (io.swagger.v3.oas.models.media.IntegerSchema)33 Components (io.swagger.v3.oas.models.Components)32 ComposedSchema (io.swagger.v3.oas.models.media.ComposedSchema)27 ObjectSchema (io.swagger.v3.oas.models.media.ObjectSchema)27 StringSchema (io.swagger.v3.oas.models.media.StringSchema)25 ByteArraySchema (io.swagger.v3.oas.models.media.ByteArraySchema)23 Parameter (io.swagger.v3.oas.models.parameters.Parameter)23 Info (io.swagger.v3.oas.models.info.Info)21 MapSchema (io.swagger.v3.oas.models.media.MapSchema)21 ParseOptions (io.swagger.v3.parser.core.models.ParseOptions)21 DateSchema (io.swagger.v3.oas.models.media.DateSchema)19