Search in sources :

Example 6 with JsonMapObject

use of org.apache.cxf.jaxrs.json.basic.JsonMapObject in project cxf by apache.

the class SwaggerToOpenApiConversionUtils method prepareRequestBody.

private static void prepareRequestBody(JsonMapObject sw2PathVerbProps, Map<String, JsonMapObject> requestBodies) {
    List<String> sw2PathVerbConsumes = CastUtils.cast((List<?>) sw2PathVerbProps.removeProperty("consumes"));
    JsonMapObject sw3RequestBody = null;
    List<JsonMapObject> sw3formBody = null;
    List<Map<String, Object>> sw2PathVerbParamsList = sw2PathVerbProps.getListMapProperty("parameters");
    if (sw2PathVerbParamsList != null) {
        for (Iterator<Map<String, Object>> it = sw2PathVerbParamsList.iterator(); it.hasNext(); ) {
            JsonMapObject sw2PathVerbParamMap = new JsonMapObject(it.next());
            if ("body".equals(sw2PathVerbParamMap.getStringProperty("in"))) {
                it.remove();
                sw3RequestBody = new JsonMapObject();
                String description = sw2PathVerbParamMap.getStringProperty("description");
                if (description != null) {
                    sw3RequestBody.setProperty("description", description);
                }
                Boolean required = sw2PathVerbParamMap.getBooleanProperty("required");
                if (required != null) {
                    sw3RequestBody.setProperty("required", required);
                }
                JsonMapObject schema = sw2PathVerbParamMap.getJsonMapProperty("schema");
                if (schema != null) {
                    JsonMapObject content = prepareContentFromSchema(schema, sw2PathVerbConsumes, requestBodies != null);
                    if (content != null) {
                        sw3RequestBody.setProperty("content", content);
                    }
                }
            } else if ("formData".equals(sw2PathVerbParamMap.getStringProperty("in"))) {
                it.remove();
                if (sw3formBody == null) {
                    sw3formBody = new LinkedList<>();
                    sw3RequestBody = new JsonMapObject();
                }
                sw2PathVerbParamMap.removeProperty("in");
                sw2PathVerbParamMap.removeProperty("required");
                sw3formBody.add(sw2PathVerbParamMap);
            } else if ("array".equals(sw2PathVerbParamMap.getStringProperty("type"))) {
                sw2PathVerbParamMap.removeProperty("type");
                sw2PathVerbParamMap.removeProperty("collectionFormat");
                sw2PathVerbParamMap.setProperty("explode", true);
                JsonMapObject items = sw2PathVerbParamMap.getJsonMapProperty("items");
                sw2PathVerbParamMap.removeProperty("items");
                JsonMapObject schema = new JsonMapObject();
                schema.setProperty("type", "array");
                schema.setProperty("items", items);
                sw2PathVerbParamMap.setProperty("schema", schema);
            } else {
                if ("matrix".equals(sw2PathVerbParamMap.getStringProperty("in"))) {
                    sw2PathVerbParamMap.removeProperty("in");
                    sw2PathVerbParamMap.setProperty("in", "path");
                    sw2PathVerbParamMap.setProperty("style", "matrix");
                }
                String type = (String) sw2PathVerbParamMap.removeProperty("type");
                Object enumK = sw2PathVerbParamMap.removeProperty("enum");
                if (type != null) {
                    JsonMapObject schema = new JsonMapObject();
                    schema.setProperty("type", type);
                    if (enumK != null) {
                        schema.setProperty("enum", enumK);
                    }
                    for (String prop : SIMPLE_TYPE_RELATED_PROPS) {
                        Object value = sw2PathVerbParamMap.removeProperty(prop);
                        if (value != null) {
                            schema.setProperty(prop, value);
                        }
                    }
                    if ("password".equals(sw2PathVerbParamMap.getProperty("name"))) {
                        schema.setProperty("format", "password");
                    }
                    sw2PathVerbParamMap.setProperty("schema", schema);
                }
            }
        }
    }
    if (sw2PathVerbParamsList.isEmpty()) {
        sw2PathVerbProps.removeProperty("parameters");
    }
    if (sw3formBody != null) {
        sw3RequestBody.setProperty("content", prepareFormContent(sw3formBody, sw2PathVerbConsumes));
    }
    if (sw3RequestBody != null) {
        if (requestBodies == null || sw3formBody != null) {
            sw2PathVerbProps.setProperty("requestBody", sw3RequestBody);
        } else {
            JsonMapObject content = sw3RequestBody.getJsonMapProperty("content");
            if (content != null) {
                String requestBodyName = (String) content.removeProperty("requestBodyName");
                if (requestBodyName != null) {
                    requestBodies.put(requestBodyName, sw3RequestBody);
                    String ref = "#components/requestBodies/" + requestBodyName;
                    sw2PathVerbProps.setProperty("requestBody", Collections.singletonMap("$ref", ref));
                }
            }
        }
    }
}
Also used : JsonMapObject(org.apache.cxf.jaxrs.json.basic.JsonMapObject) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) LinkedList(java.util.LinkedList) JsonMapObject(org.apache.cxf.jaxrs.json.basic.JsonMapObject)

Example 7 with JsonMapObject

use of org.apache.cxf.jaxrs.json.basic.JsonMapObject in project cxf by apache.

the class SwaggerToOpenApiConversionUtils method getOpenApiFromSwaggerJson.

public static String getOpenApiFromSwaggerJson(MessageContext ctx, String json, OpenApiConfiguration cfg) throws IOException {
    JsonMapObjectReaderWriter readerWriter = new JsonMapObjectReaderWriter();
    JsonMapObject sw2 = readerWriter.fromJsonToJsonObject(json);
    JsonMapObject sw3 = new JsonMapObject();
    // "openapi"
    sw3.setProperty("openapi", "3.0.0");
    // "servers"
    setServersProperty(ctx, sw2, sw3);
    // "info"
    JsonMapObject infoObject = sw2.getJsonMapProperty("info");
    if (infoObject != null) {
        sw3.setProperty("info", infoObject);
    }
    // "tags"
    List<Map<String, Object>> tagsObject = sw2.getListMapProperty("tags");
    if (tagsObject != null) {
        sw3.setProperty("tags", tagsObject);
    }
    // paths
    Map<String, JsonMapObject> requestBodies = cfg != null && cfg.isCreateRequestBodies() ? new LinkedHashMap<>() : null;
    setPathsProperty(sw2, sw3, requestBodies);
    // components
    setComponentsProperty(sw2, sw3, requestBodies);
    // externalDocs
    Object externalDocsObject = sw2.getProperty("externalDocs");
    if (externalDocsObject != null) {
        sw3.setProperty("externalDocs", externalDocsObject);
    }
    return readerWriter.toJson(sw3).replace("#/definitions/", "#/components/schemas/");
}
Also used : JsonMapObjectReaderWriter(org.apache.cxf.jaxrs.json.basic.JsonMapObjectReaderWriter) JsonMapObject(org.apache.cxf.jaxrs.json.basic.JsonMapObject) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) JsonMapObject(org.apache.cxf.jaxrs.json.basic.JsonMapObject)

Example 8 with JsonMapObject

use of org.apache.cxf.jaxrs.json.basic.JsonMapObject in project cxf by apache.

the class SwaggerToOpenApiConversionUtils method prepareResponses.

private static void prepareResponses(JsonMapObject sw2PathVerbProps) {
    List<String> sw2PathVerbProduces = CastUtils.cast((List<?>) sw2PathVerbProps.removeProperty("produces"));
    JsonMapObject sw3PathVerbResps = null;
    JsonMapObject sw2PathVerbResps = sw2PathVerbProps.getJsonMapProperty("responses");
    if (sw2PathVerbResps != null) {
        sw3PathVerbResps = new JsonMapObject();
        JsonMapObject okResp = null;
        if (sw2PathVerbResps.containsProperty("200")) {
            okResp = new JsonMapObject(CastUtils.cast((Map<?, ?>) sw2PathVerbResps.removeProperty("200")));
            JsonMapObject newOkResp = new JsonMapObject();
            String description = okResp.getStringProperty("description");
            if (description != null) {
                newOkResp.setProperty("description", description);
            }
            JsonMapObject schema = okResp.getJsonMapProperty("schema");
            if (schema != null) {
                JsonMapObject content = prepareContentFromSchema(schema, sw2PathVerbProduces, false);
                if (content != null) {
                    newOkResp.setProperty("content", content);
                }
            }
            JsonMapObject headers = okResp.getJsonMapProperty("headers");
            if (headers != null) {
                newOkResp.setProperty("headers", headers);
            }
            sw3PathVerbResps.setProperty("200", newOkResp);
        }
        for (Map.Entry<String, Object> entry : sw2PathVerbResps.asMap().entrySet()) {
            sw3PathVerbResps.setProperty(entry.getKey(), entry.getValue());
        }
        sw2PathVerbProps.setProperty("responses", sw3PathVerbResps);
    }
}
Also used : JsonMapObject(org.apache.cxf.jaxrs.json.basic.JsonMapObject) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) JsonMapObject(org.apache.cxf.jaxrs.json.basic.JsonMapObject)

Example 9 with JsonMapObject

use of org.apache.cxf.jaxrs.json.basic.JsonMapObject in project cxf by apache.

the class SwaggerToOpenApiConversionUtils method setComponentsProperty.

private static void setComponentsProperty(JsonMapObject sw2, JsonMapObject sw3, Map<String, JsonMapObject> requestBodies) {
    JsonMapObject comps = new JsonMapObject();
    JsonMapObject requestBodiesObj = new JsonMapObject();
    if (requestBodies != null) {
        for (Map.Entry<String, JsonMapObject> entry : requestBodies.entrySet()) {
            requestBodiesObj.setProperty(entry.getKey(), entry.getValue());
        }
    }
    comps.setProperty("requestBodies", requestBodiesObj);
    Object s2Defs = sw2.getProperty("definitions");
    if (s2Defs != null) {
        comps.setProperty("schemas", s2Defs);
    }
    JsonMapObject s2SecurityDefs = sw2.getJsonMapProperty("securityDefinitions");
    if (s2SecurityDefs != null) {
        comps.setProperty("securitySchemes", s2SecurityDefs);
        for (String property : s2SecurityDefs.asMap().keySet()) {
            JsonMapObject securityScheme = s2SecurityDefs.getJsonMapProperty(property);
            if ("basic".equals(securityScheme.getStringProperty("type"))) {
                securityScheme.setProperty("type", "http");
                securityScheme.setProperty("scheme", "basic");
            }
        }
    }
    sw3.setProperty("components", comps);
}
Also used : JsonMapObject(org.apache.cxf.jaxrs.json.basic.JsonMapObject) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) JsonMapObject(org.apache.cxf.jaxrs.json.basic.JsonMapObject)

Example 10 with JsonMapObject

use of org.apache.cxf.jaxrs.json.basic.JsonMapObject in project cxf by apache.

the class SwaggerToOpenApiConversionUtils method prepareContentFromSchema.

private static JsonMapObject prepareContentFromSchema(JsonMapObject schema, List<String> mediaTypes, boolean storeModelName) {
    String type = schema.getStringProperty("type");
    String modelName = null;
    boolean isArray = false;
    if (!"object".equals(type) || !"string".equals(type)) {
        String ref = null;
        JsonMapObject items = null;
        if ("array".equals(type)) {
            isArray = true;
            items = schema.getJsonMapProperty("items");
            ref = (String) items.getProperty("$ref");
        } else {
            ref = schema.getStringProperty("$ref");
        }
        if (ref != null) {
            int index = ref.lastIndexOf("/");
            modelName = ref.substring(index + 1);
            if (items == null) {
                schema.setProperty("$ref", "#components/schemas/" + modelName);
            } else {
                items.setProperty("$ref", "#components/schemas/" + modelName);
            }
        }
    }
    JsonMapObject content = new JsonMapObject();
    List<String> mediaTypesList = mediaTypes == null ? Collections.singletonList("application/json") : mediaTypes;
    for (String mediaType : mediaTypesList) {
        content.setProperty(mediaType, Collections.singletonMap("schema", schema));
    }
    if (modelName != null && storeModelName) {
        content.setProperty("requestBodyName", isArray ? modelName + "Array" : modelName);
    }
    // pass the model name via the content object
    return content;
}
Also used : JsonMapObject(org.apache.cxf.jaxrs.json.basic.JsonMapObject)

Aggregations

JsonMapObject (org.apache.cxf.jaxrs.json.basic.JsonMapObject)63 Map (java.util.Map)23 LinkedHashMap (java.util.LinkedHashMap)5 JsonMapObjectReaderWriter (org.apache.cxf.jaxrs.json.basic.JsonMapObjectReaderWriter)5 Test (org.junit.Test)3 LinkedList (java.util.LinkedList)2 IOException (java.io.IOException)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 WebClient (org.apache.cxf.jaxrs.client.WebClient)1