Search in sources :

Example 6 with OpenAPI

use of io.swagger.v3.oas.models.OpenAPI in project vertx-web by vert-x3.

the class OpenApi3Utils method replaceRef.

private static void replaceRef(ObjectNode n, ObjectNode root, OpenAPI oas) {
    /**
     * If a ref is found, the structure of the schema is circular. The oas parser don't solve circular refs.
     * So I bundle the schema:
     * 1. I update the ref field with a #/definitions/schema_name uri
     * 2. If #/definitions/schema_name is empty, I solve it
     */
    String oldRef = n.get("$ref").asText();
    Matcher m = COMPONENTS_REFS_MATCHER.matcher(oldRef);
    if (m.lookingAt()) {
        String schemaName = m.group(1);
        String newRef = m.replaceAll(COMPONENTS_REFS_SUBSTITUTION);
        n.remove("$ref");
        n.put("$ref", newRef);
        if (!root.has("definitions") || !root.get("definitions").has(schemaName)) {
            Schema s = oas.getComponents().getSchemas().get(schemaName);
            ObjectNode schema = ObjectMapperFactory.createJson().convertValue(s, ObjectNode.class);
            // We need to search inside for other refs
            if (!root.has("definitions")) {
                ObjectNode definitions = JsonNodeFactory.instance.objectNode();
                definitions.set(schemaName, schema);
                root.putObject("definitions");
            } else {
                ((ObjectNode) root.get("definitions")).set(schemaName, schema);
            }
            walkAndSolve(schema, root, oas);
        }
    } else
        throw new RuntimeException("Wrong ref! " + oldRef);
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Matcher(java.util.regex.Matcher) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) Schema(io.swagger.v3.oas.models.media.Schema) JsonSchema(com.networknt.schema.JsonSchema)

Aggregations

Operation (io.swagger.v3.oas.models.Operation)2 OpenAPIV3Parser (io.swagger.v3.parser.OpenAPIV3Parser)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 JsonSchema (com.networknt.schema.JsonSchema)1 OpenApiConfigurationException (io.swagger.v3.oas.integration.OpenApiConfigurationException)1 SwaggerConfiguration (io.swagger.v3.oas.integration.SwaggerConfiguration)1 OpenApiContext (io.swagger.v3.oas.integration.api.OpenApiContext)1 OpenAPI (io.swagger.v3.oas.models.OpenAPI)1 PathItem (io.swagger.v3.oas.models.PathItem)1 HttpMethod (io.swagger.v3.oas.models.PathItem.HttpMethod)1 ComposedSchema (io.swagger.v3.oas.models.media.ComposedSchema)1 Schema (io.swagger.v3.oas.models.media.Schema)1 Parameter (io.swagger.v3.oas.models.parameters.Parameter)1 ApiResponse (io.swagger.v3.oas.models.responses.ApiResponse)1 Tag (io.swagger.v3.oas.models.tags.Tag)1 ParseOptions (io.swagger.v3.parser.core.models.ParseOptions)1 SwaggerParseResult (io.swagger.v3.parser.core.models.SwaggerParseResult)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 ArrayList (java.util.ArrayList)1