Search in sources :

Example 6 with JsonMapObjectReaderWriter

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

the class SwaggerToOpenApiConversionTest method testOpenApiJSON.

@Test
public void testOpenApiJSON() throws Exception {
    final WebClient client = createWebClient("/openapi.json");
    String openApiResponse = client.get(String.class);
    JsonMapObject openApiJson = new JsonMapObjectReaderWriter().fromJsonToJsonObject(openApiResponse);
    assertEquals(6, openApiJson.size());
    // version
    assertEquals("3.0.0", openApiJson.getProperty("openapi"));
    // servers
    List<Map<String, Object>> servers = openApiJson.getListMapProperty("servers");
    assertEquals(1, servers.size());
    assertEquals(1, servers.get(0).size());
    assertEquals("http://localhost:" + PORT, servers.get(0).get("url"));
    // info
    JsonMapObject info = openApiJson.getJsonMapProperty("info");
    assertEquals(4, info.size());
    assertEquals(DESCRIPTION, info.getProperty("description"));
    assertEquals(TITLE, info.getProperty("title"));
    JsonMapObject contact = info.getJsonMapProperty("contact");
    assertEquals(1, contact.size());
    assertEquals(CONTACT, contact.getProperty("name"));
    JsonMapObject license = info.getJsonMapProperty("license");
    assertEquals(2, license.size());
    assertEquals(LICENSE, license.getProperty("name"));
    assertEquals(LICENSE_URL, license.getProperty("url"));
    // tags
    List<Map<String, Object>> tags = openApiJson.getListMapProperty("tags");
    assertEquals(1, tags.size());
    assertEquals(1, tags.get(0).size());
    assertEquals("bookstore", tags.get(0).get("name"));
    // paths
    JsonMapObject paths = openApiJson.getJsonMapProperty("paths");
    assertEquals(2, paths.size());
    // 1: bookstore
    JsonMapObject bookstore = paths.getJsonMapProperty("/bookstore");
    assertEquals(1, bookstore.size());
    // get
    verifyBookStoreGet(bookstore);
    // 2: bookstore/{id}
    JsonMapObject bookstoreId = paths.getJsonMapProperty("/bookstore/{id}");
    assertEquals(2, bookstoreId.size());
    // get
    verifyBookStoreIdGet(bookstoreId);
    // delete
    verifyBookStoreIdDelete(bookstoreId);
    // components
    JsonMapObject comps = openApiJson.getJsonMapProperty("components");
    assertEquals(3, comps.size());
    JsonMapObject requestBodies = comps.getJsonMapProperty("requestBodies");
    assertEquals(0, requestBodies.size());
    JsonMapObject schemas = comps.getJsonMapProperty("schemas");
    assertEquals(1, schemas.size());
    JsonMapObject secSchemes = comps.getJsonMapProperty("securitySchemes");
    assertEquals(1, secSchemes.size());
    // Finally check swagger.json can still be generated.
    doTestSwagger2JSON();
}
Also used : JsonMapObjectReaderWriter(org.apache.cxf.jaxrs.json.basic.JsonMapObjectReaderWriter) WebClient(org.apache.cxf.jaxrs.client.WebClient) Map(java.util.Map) JsonMapObject(org.apache.cxf.jaxrs.json.basic.JsonMapObject) Test(org.junit.Test)

Example 7 with JsonMapObjectReaderWriter

use of org.apache.cxf.jaxrs.json.basic.JsonMapObjectReaderWriter 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 JsonMapObjectReaderWriter

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

the class SwaggerToOpenApiConversionUtilsTest method testConvertFromSwaggerToOpenApiWithRequestBodies.

@Test
public void testConvertFromSwaggerToOpenApiWithRequestBodies() {
    OpenApiConfiguration cfg = new OpenApiConfiguration();
    cfg.setCreateRequestBodies(true);
    String s = SwaggerToOpenApiConversionUtils.getOpenApiFromSwaggerLoc("/swagger2petShop.json", cfg);
    JsonMapObjectReaderWriter readerWriter = new JsonMapObjectReaderWriter();
    JsonMapObject sw3 = readerWriter.fromJsonToJsonObject(s);
    assertEquals("3.0.0", sw3.getStringProperty("openapi"));
    verifyServersProperty(sw3);
    verifyInfoProperty(sw3);
    verifyTagsProperty(sw3);
    verifyPathsProperty(sw3, cfg);
    verifyComponentsProperty(sw3, cfg);
}
Also used : JsonMapObjectReaderWriter(org.apache.cxf.jaxrs.json.basic.JsonMapObjectReaderWriter) JsonMapObject(org.apache.cxf.jaxrs.json.basic.JsonMapObject) Test(org.junit.Test)

Example 9 with JsonMapObjectReaderWriter

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

the class OpenApiParseUtils method getUserApplicationFromJson.

public static UserApplication getUserApplicationFromJson(String json, ParseConfiguration cfg) {
    JsonMapObjectReaderWriter reader = new JsonMapObjectReaderWriter();
    Map<String, Object> map = reader.fromJson(json);
    UserApplication app = new UserApplication();
    app.setBasePath("/");
    List<Map<String, Object>> servers = CastUtils.cast((List<?>) map.get("servers"));
    if (servers != null && !servers.isEmpty()) {
        final String url = (String) servers.get(0).get("url");
        if (url != null) {
            app.setBasePath(url);
        }
    }
    Map<String, List<UserOperation>> userOpsMap = new LinkedHashMap<String, List<UserOperation>>();
    Set<String> tags = new HashSet<>();
    List<Map<String, Object>> tagsProp = CastUtils.cast((List<?>) map.get("tags"));
    if (tagsProp != null) {
        for (Map<String, Object> tagProp : tagsProp) {
            tags.add((String) tagProp.get("name"));
        }
    } else {
        tags.add("");
    }
    for (String tag : tags) {
        userOpsMap.put(tag, new LinkedList<UserOperation>());
    }
    Map<String, Map<String, Object>> paths = CastUtils.cast((Map<?, ?>) map.get("paths"));
    for (Map.Entry<String, Map<String, Object>> pathEntry : paths.entrySet()) {
        String operPath = pathEntry.getKey();
        Map<String, Object> operations = pathEntry.getValue();
        for (Map.Entry<String, Object> operEntry : operations.entrySet()) {
            UserOperation userOp = new UserOperation();
            userOp.setVerb(operEntry.getKey().toUpperCase());
            Map<String, Object> oper = CastUtils.cast((Map<?, ?>) operEntry.getValue());
            userOp.setPath(operPath);
            userOp.setName((String) oper.get("operationId"));
            Map<String, Object> responses = CastUtils.cast((Map<?, ?>) oper.get("responses"));
            if (responses != null) {
                userOp.setProduces(listToString(responses.entrySet().stream().map(entry -> CastUtils.cast((Map<?, ?>) entry.getValue())).map(value -> CastUtils.cast((Map<?, ?>) value.get("content"))).filter(Objects::nonNull).flatMap(content -> content.keySet().stream().map(type -> (String) type)).collect(Collectors.toList())));
            }
            Map<String, Object> payloads = CastUtils.cast((Map<?, ?>) oper.get("requestBody"));
            if (payloads != null) {
                userOp.setConsumes(listToString(payloads.entrySet().stream().map(entry -> CastUtils.cast((Map<?, ?>) entry.getValue())).map(value -> CastUtils.cast((Map<?, ?>) value.get("content"))).filter(Objects::nonNull).flatMap(content -> content.keySet().stream().map(type -> (String) type)).collect(Collectors.toList())));
            }
            List<Parameter> userOpParams = new LinkedList<Parameter>();
            List<Map<String, Object>> params = CastUtils.cast((List<?>) oper.get("parameters"));
            if (params != null) {
                for (Map<String, Object> param : params) {
                    String name = (String) param.get("name");
                    // "query", "header", "path" or "cookie".
                    String paramType = (String) param.get("in");
                    ParameterType pType = null;
                    if ("query".equals(paramType)) {
                        pType = ParameterType.QUERY;
                    } else if ("header".equals(paramType)) {
                        pType = ParameterType.HEADER;
                    } else if ("path".equals(paramType)) {
                        pType = ParameterType.PATH;
                    } else if ("cookie".equals(paramType)) {
                        pType = ParameterType.COOKIE;
                    } else {
                        pType = ParameterType.REQUEST_BODY;
                    }
                    Parameter userParam = new Parameter(pType, name);
                    setJavaType(userParam, (String) param.get("type"));
                    userOpParams.add(userParam);
                }
            }
            if (!userOpParams.isEmpty()) {
                userOp.setParameters(userOpParams);
            }
            List<String> opTags = CastUtils.cast((List<?>) oper.get("tags"));
            if (opTags == null) {
                opTags = Collections.singletonList("");
            }
            for (String opTag : opTags) {
                userOpsMap.get(opTag).add(userOp);
            }
        }
    }
    List<UserResource> resources = new LinkedList<UserResource>();
    for (Map.Entry<String, List<UserOperation>> entry : userOpsMap.entrySet()) {
        UserResource ur = new UserResource();
        ur.setPath("/");
        ur.setOperations(entry.getValue());
        ur.setName(entry.getKey());
        resources.add(ur);
    }
    app.setResources(resources);
    return app;
}
Also used : Bus(org.apache.cxf.Bus) UserApplication(org.apache.cxf.jaxrs.model.UserApplication) HashMap(java.util.HashMap) UserResource(org.apache.cxf.jaxrs.model.UserResource) Parameter(org.apache.cxf.jaxrs.model.Parameter) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) ParameterType(org.apache.cxf.jaxrs.model.ParameterType) LogUtils(org.apache.cxf.common.logging.LogUtils) Map(java.util.Map) ResourceUtils(org.apache.cxf.jaxrs.utils.ResourceUtils) LinkedList(java.util.LinkedList) UserOperation(org.apache.cxf.jaxrs.model.UserOperation) CastUtils(org.apache.cxf.helpers.CastUtils) IOUtils(org.apache.cxf.helpers.IOUtils) Set(java.util.Set) IOException(java.io.IOException) Logger(java.util.logging.Logger) ClassLoaderUtils(org.apache.cxf.common.classloader.ClassLoaderUtils) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) List(java.util.List) BusFactory(org.apache.cxf.BusFactory) Collections(java.util.Collections) InputStream(java.io.InputStream) JsonMapObjectReaderWriter(org.apache.cxf.jaxrs.json.basic.JsonMapObjectReaderWriter) LinkedHashMap(java.util.LinkedHashMap) LinkedList(java.util.LinkedList) List(java.util.List) HashSet(java.util.HashSet) ParameterType(org.apache.cxf.jaxrs.model.ParameterType) JsonMapObjectReaderWriter(org.apache.cxf.jaxrs.json.basic.JsonMapObjectReaderWriter) UserResource(org.apache.cxf.jaxrs.model.UserResource) LinkedList(java.util.LinkedList) UserApplication(org.apache.cxf.jaxrs.model.UserApplication) UserOperation(org.apache.cxf.jaxrs.model.UserOperation) Objects(java.util.Objects) Parameter(org.apache.cxf.jaxrs.model.Parameter) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 10 with JsonMapObjectReaderWriter

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

the class SwaggerParseUtils method getUserApplicationFromJson.

public static UserApplication getUserApplicationFromJson(String json, ParseConfiguration cfg) {
    JsonMapObjectReaderWriter reader = new JsonMapObjectReaderWriter();
    Map<String, Object> map = reader.fromJson(json);
    UserApplication app = new UserApplication();
    String relativePath = (String) map.get("basePath");
    app.setBasePath(StringUtils.isEmpty(relativePath) ? "/" : relativePath);
    Map<String, List<UserOperation>> userOpsMap = new LinkedHashMap<String, List<UserOperation>>();
    Set<String> tags = new HashSet<>();
    List<Map<String, Object>> tagsProp = CastUtils.cast((List<?>) map.get("tags"));
    if (tagsProp != null) {
        for (Map<String, Object> tagProp : tagsProp) {
            tags.add((String) tagProp.get("name"));
        }
    } else {
        tags.add("");
    }
    for (String tag : tags) {
        userOpsMap.put(tag, new LinkedList<UserOperation>());
    }
    Map<String, Map<String, Object>> paths = CastUtils.cast((Map<?, ?>) map.get("paths"));
    for (Map.Entry<String, Map<String, Object>> pathEntry : paths.entrySet()) {
        String operPath = pathEntry.getKey();
        Map<String, Object> operations = pathEntry.getValue();
        for (Map.Entry<String, Object> operEntry : operations.entrySet()) {
            UserOperation userOp = new UserOperation();
            userOp.setVerb(operEntry.getKey().toUpperCase());
            Map<String, Object> oper = CastUtils.cast((Map<?, ?>) operEntry.getValue());
            userOp.setPath(operPath);
            userOp.setName((String) oper.get("operationId"));
            List<String> opProduces = CastUtils.cast((List<?>) oper.get("produces"));
            userOp.setProduces(listToString(opProduces));
            List<String> opConsumes = CastUtils.cast((List<?>) oper.get("consumes"));
            userOp.setConsumes(listToString(opConsumes));
            List<Parameter> userOpParams = new LinkedList<Parameter>();
            List<Map<String, Object>> params = CastUtils.cast((List<?>) oper.get("parameters"));
            for (Map<String, Object> param : params) {
                String name = (String) param.get("name");
                // "query", "header", "path", "formData" or "body"
                String paramType = (String) param.get("in");
                ParameterType pType = "body".equals(paramType) ? ParameterType.REQUEST_BODY : "formData".equals(paramType) ? ParameterType.FORM : ParameterType.valueOf(paramType.toUpperCase());
                Parameter userParam = new Parameter(pType, name);
                setJavaType(userParam, (String) param.get("type"));
                userOpParams.add(userParam);
            }
            if (!userOpParams.isEmpty()) {
                userOp.setParameters(userOpParams);
            }
            List<String> opTags = CastUtils.cast((List<?>) oper.get("tags"));
            if (opTags == null) {
                opTags = Collections.singletonList("");
            }
            for (String opTag : opTags) {
                userOpsMap.get(opTag).add(userOp);
            }
        }
    }
    List<UserResource> resources = new LinkedList<UserResource>();
    for (Map.Entry<String, List<UserOperation>> entry : userOpsMap.entrySet()) {
        UserResource ur = new UserResource();
        ur.setPath("/");
        ur.setOperations(entry.getValue());
        ur.setName(entry.getKey());
        resources.add(ur);
    }
    app.setResources(resources);
    return app;
}
Also used : LinkedHashMap(java.util.LinkedHashMap) LinkedList(java.util.LinkedList) List(java.util.List) HashSet(java.util.HashSet) ParameterType(org.apache.cxf.jaxrs.model.ParameterType) JsonMapObjectReaderWriter(org.apache.cxf.jaxrs.json.basic.JsonMapObjectReaderWriter) UserResource(org.apache.cxf.jaxrs.model.UserResource) LinkedList(java.util.LinkedList) UserApplication(org.apache.cxf.jaxrs.model.UserApplication) UserOperation(org.apache.cxf.jaxrs.model.UserOperation) Parameter(org.apache.cxf.jaxrs.model.Parameter) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

JsonMapObjectReaderWriter (org.apache.cxf.jaxrs.json.basic.JsonMapObjectReaderWriter)20 Test (org.junit.Test)9 JwsHeaders (org.apache.cxf.rs.security.jose.jws.JwsHeaders)7 JsonWebKey (org.apache.cxf.rs.security.jose.jwk.JsonWebKey)6 JsonWebKeys (org.apache.cxf.rs.security.jose.jwk.JsonWebKeys)6 LinkedHashMap (java.util.LinkedHashMap)5 Map (java.util.Map)5 JsonMapObject (org.apache.cxf.jaxrs.json.basic.JsonMapObject)5 JwsCompactConsumer (org.apache.cxf.rs.security.jose.jws.JwsCompactConsumer)5 JwsCompactProducer (org.apache.cxf.rs.security.jose.jws.JwsCompactProducer)5 JwsJsonConsumer (org.apache.cxf.rs.security.jose.jws.JwsJsonConsumer)5 JwsJsonProducer (org.apache.cxf.rs.security.jose.jws.JwsJsonProducer)5 List (java.util.List)3 BouncyCastleProvider (org.bouncycastle.jce.provider.BouncyCastleProvider)3 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 LinkedList (java.util.LinkedList)2 Parameter (org.apache.cxf.jaxrs.model.Parameter)2