@Test public void testVendorExtension() throws JSONException { String jsonString = FileLoader.readString("parser/testVendorExtension.json"); Swagger swagger = OpenAPIParser.parse(jsonString); printSwagger(swagger); assertThat(swagger, is(notNullValue())); assertThat(swagger.getSwagger(), is("2.0")); assertThat(swagger.getBasePath(), is("/gotapi/testProfile")); assertThat(swagger.getPaths(), is(notNullValue())); assertThat(swagger.getProduces(), hasItems("application/json", "text/html")); assertThat(swagger.getConsumes(), hasItems("application/json")); Info info = swagger.getInfo(); assertThat(info.getTitle(), is("Test Profile")); assertThat(info.getVersion(), is("1.0")); assertThat(info.getDescription(), is("Test Description")); Path a0 = swagger.getPaths().getPath("/a0"); assertThat(a0, is(notNullValue())); assertThat(a0.getGet().getSummary(), is("test path a0")); assertThat(a0.getGet().getDescription(), is("test path a0")); assertThat(a0.getGet().getOperationId(), is("a0Get")); List<Parameter> parameters = a0.getGet().getParameters(); assertThat(parameters, is(notNullValue())); assertThat(parameters.get(0).getName(), is("serviceId")); assertThat(parameters.get(0).getDescription(), is("serviceId")); assertThat(parameters.get(0).getIn().getName(), is("query")); XType xType = a0.getPut().getXType(); assertThat(xType, is(notNullValue())); assertThat(xType, is(XType.EVENT)); XEvent xEvent = a0.getPut().getXEvent(); assertThat(xEvent, is(notNullValue())); assertThat(xEvent.getSchema(), is(notNullValue())); assertThat(xEvent.getSchema().getReference(), is(notNullValue())); assertThat(xEvent.getSchema().getReference(), is("#/definitions/CommonEvent")); assertThat(xEvent.getExamples(), is(notNullValue())); Example example = xEvent.getExamples().get("application/json"); assertThat(example, is(notNullValue())); assertThat(example.getExample(), is(notNullValue())); assertThat(example.getExample().getString("serviceId"), is("Test.exampleId.localhost.deviceconnect.org")); assertThat(example.getExample().getString("profile"), is("testProfile")); assertThat(example.getExample().getString("attribute"), is("a0")); Path a1 = swagger.getPaths().getPath("/a1"); assertThat(a1, is(notNullValue())); assertThat(a1.getGet().getSummary(), is("test path a1")); assertThat(a1.getGet().getDescription(), is("test path a1")); assertThat(a1.getGet().getOperationId(), is(nullValue())); }
public static Swagger parse(String jsonString) throws JSONException { JSONObject jsonObject = new JSONObject(jsonString); Swagger swagger = new Swagger(); for (Iterator<String> it = jsonObject.keys(); it.hasNext();) { String key = it.next(); Object object = jsonObject.get(key); if (object != null) { if ("swagger".equalsIgnoreCase(key)) { swagger.setSwagger((String) object); } else if ("basePath".equalsIgnoreCase(key)) { swagger.setBasePath((String) object); } else if ("host".equalsIgnoreCase(key)) { swagger.setHost((String) object); } else if ("info".equalsIgnoreCase(key)) { swagger.setInfo(parseInfo((JSONObject) object)); } else if ("schemes".equalsIgnoreCase(key)) { swagger.setSchemes(parseStringArray((JSONArray) object)); } else if ("consumes".equalsIgnoreCase(key)) { swagger.setConsumes(parseStringArray((JSONArray) object)); } else if ("produces".equalsIgnoreCase(key)) { swagger.setProduces(parseStringArray((JSONArray) object)); } else if ("paths".equalsIgnoreCase(key)) { swagger.setPaths(parsePaths((JSONObject) object)); } else if ("definitions".equalsIgnoreCase(key)) { swagger.setDefinitions(parseDefinitions((JSONObject) object)); } else if ("parameters".equalsIgnoreCase(key)) { swagger.setParameters(parseParameters((JSONObject) object)); } else if ("responses".equalsIgnoreCase(key)) { swagger.setResponses(parseResponsesWithRoot((JSONObject) object)); } else if ("securityDefinitions".equalsIgnoreCase(key)) { swagger.setSecurityDefinitions(parseSecurityDefinitions((JSONObject) object)); } else if ("security".equalsIgnoreCase(key)) { swagger.setSecurityRequirement(parseSecurity((JSONObject) object)); } else if ("tags".equalsIgnoreCase(key)) { swagger.setTags(parseTags((JSONArray) object)); } else if ("externalDocs".equalsIgnoreCase(key)) { swagger.setExternalDocs(parseExternalDocs((JSONObject) object)); } else if (key.startsWith("x-")) { swagger.addVendorExtension(key, parseVendorExtension(object)); } } } if (swagger.getSwagger() == null) { throw new IllegalArgumentException("swagger does not exist."); } if (!swagger.getSwagger().equalsIgnoreCase("2.0")) { throw new IllegalArgumentException("swagger is invalid."); } if (swagger.getInfo() == null) { throw new IllegalArgumentException("info does not exist."); } if (swagger.getPaths() == null) { throw new IllegalArgumentException("paths does not exist."); } return swagger; }
OpenAPIParser { public static Swagger parse(String jsonString) throws JSONException { JSONObject jsonObject = new JSONObject(jsonString); Swagger swagger = new Swagger(); for (Iterator<String> it = jsonObject.keys(); it.hasNext();) { String key = it.next(); Object object = jsonObject.get(key); if (object != null) { if ("swagger".equalsIgnoreCase(key)) { swagger.setSwagger((String) object); } else if ("basePath".equalsIgnoreCase(key)) { swagger.setBasePath((String) object); } else if ("host".equalsIgnoreCase(key)) { swagger.setHost((String) object); } else if ("info".equalsIgnoreCase(key)) { swagger.setInfo(parseInfo((JSONObject) object)); } else if ("schemes".equalsIgnoreCase(key)) { swagger.setSchemes(parseStringArray((JSONArray) object)); } else if ("consumes".equalsIgnoreCase(key)) { swagger.setConsumes(parseStringArray((JSONArray) object)); } else if ("produces".equalsIgnoreCase(key)) { swagger.setProduces(parseStringArray((JSONArray) object)); } else if ("paths".equalsIgnoreCase(key)) { swagger.setPaths(parsePaths((JSONObject) object)); } else if ("definitions".equalsIgnoreCase(key)) { swagger.setDefinitions(parseDefinitions((JSONObject) object)); } else if ("parameters".equalsIgnoreCase(key)) { swagger.setParameters(parseParameters((JSONObject) object)); } else if ("responses".equalsIgnoreCase(key)) { swagger.setResponses(parseResponsesWithRoot((JSONObject) object)); } else if ("securityDefinitions".equalsIgnoreCase(key)) { swagger.setSecurityDefinitions(parseSecurityDefinitions((JSONObject) object)); } else if ("security".equalsIgnoreCase(key)) { swagger.setSecurityRequirement(parseSecurity((JSONObject) object)); } else if ("tags".equalsIgnoreCase(key)) { swagger.setTags(parseTags((JSONArray) object)); } else if ("externalDocs".equalsIgnoreCase(key)) { swagger.setExternalDocs(parseExternalDocs((JSONObject) object)); } else if (key.startsWith("x-")) { swagger.addVendorExtension(key, parseVendorExtension(object)); } } } if (swagger.getSwagger() == null) { throw new IllegalArgumentException("swagger does not exist."); } if (!swagger.getSwagger().equalsIgnoreCase("2.0")) { throw new IllegalArgumentException("swagger is invalid."); } if (swagger.getInfo() == null) { throw new IllegalArgumentException("info does not exist."); } if (swagger.getPaths() == null) { throw new IllegalArgumentException("paths does not exist."); } return swagger; } }
OpenAPIParser { public static Swagger parse(String jsonString) throws JSONException { JSONObject jsonObject = new JSONObject(jsonString); Swagger swagger = new Swagger(); for (Iterator<String> it = jsonObject.keys(); it.hasNext();) { String key = it.next(); Object object = jsonObject.get(key); if (object != null) { if ("swagger".equalsIgnoreCase(key)) { swagger.setSwagger((String) object); } else if ("basePath".equalsIgnoreCase(key)) { swagger.setBasePath((String) object); } else if ("host".equalsIgnoreCase(key)) { swagger.setHost((String) object); } else if ("info".equalsIgnoreCase(key)) { swagger.setInfo(parseInfo((JSONObject) object)); } else if ("schemes".equalsIgnoreCase(key)) { swagger.setSchemes(parseStringArray((JSONArray) object)); } else if ("consumes".equalsIgnoreCase(key)) { swagger.setConsumes(parseStringArray((JSONArray) object)); } else if ("produces".equalsIgnoreCase(key)) { swagger.setProduces(parseStringArray((JSONArray) object)); } else if ("paths".equalsIgnoreCase(key)) { swagger.setPaths(parsePaths((JSONObject) object)); } else if ("definitions".equalsIgnoreCase(key)) { swagger.setDefinitions(parseDefinitions((JSONObject) object)); } else if ("parameters".equalsIgnoreCase(key)) { swagger.setParameters(parseParameters((JSONObject) object)); } else if ("responses".equalsIgnoreCase(key)) { swagger.setResponses(parseResponsesWithRoot((JSONObject) object)); } else if ("securityDefinitions".equalsIgnoreCase(key)) { swagger.setSecurityDefinitions(parseSecurityDefinitions((JSONObject) object)); } else if ("security".equalsIgnoreCase(key)) { swagger.setSecurityRequirement(parseSecurity((JSONObject) object)); } else if ("tags".equalsIgnoreCase(key)) { swagger.setTags(parseTags((JSONArray) object)); } else if ("externalDocs".equalsIgnoreCase(key)) { swagger.setExternalDocs(parseExternalDocs((JSONObject) object)); } else if (key.startsWith("x-")) { swagger.addVendorExtension(key, parseVendorExtension(object)); } } } if (swagger.getSwagger() == null) { throw new IllegalArgumentException("swagger does not exist."); } if (!swagger.getSwagger().equalsIgnoreCase("2.0")) { throw new IllegalArgumentException("swagger is invalid."); } if (swagger.getInfo() == null) { throw new IllegalArgumentException("info does not exist."); } if (swagger.getPaths() == null) { throw new IllegalArgumentException("paths does not exist."); } return swagger; } private  OpenAPIParser(); }
OpenAPIParser { public static Swagger parse(String jsonString) throws JSONException { JSONObject jsonObject = new JSONObject(jsonString); Swagger swagger = new Swagger(); for (Iterator<String> it = jsonObject.keys(); it.hasNext();) { String key = it.next(); Object object = jsonObject.get(key); if (object != null) { if ("swagger".equalsIgnoreCase(key)) { swagger.setSwagger((String) object); } else if ("basePath".equalsIgnoreCase(key)) { swagger.setBasePath((String) object); } else if ("host".equalsIgnoreCase(key)) { swagger.setHost((String) object); } else if ("info".equalsIgnoreCase(key)) { swagger.setInfo(parseInfo((JSONObject) object)); } else if ("schemes".equalsIgnoreCase(key)) { swagger.setSchemes(parseStringArray((JSONArray) object)); } else if ("consumes".equalsIgnoreCase(key)) { swagger.setConsumes(parseStringArray((JSONArray) object)); } else if ("produces".equalsIgnoreCase(key)) { swagger.setProduces(parseStringArray((JSONArray) object)); } else if ("paths".equalsIgnoreCase(key)) { swagger.setPaths(parsePaths((JSONObject) object)); } else if ("definitions".equalsIgnoreCase(key)) { swagger.setDefinitions(parseDefinitions((JSONObject) object)); } else if ("parameters".equalsIgnoreCase(key)) { swagger.setParameters(parseParameters((JSONObject) object)); } else if ("responses".equalsIgnoreCase(key)) { swagger.setResponses(parseResponsesWithRoot((JSONObject) object)); } else if ("securityDefinitions".equalsIgnoreCase(key)) { swagger.setSecurityDefinitions(parseSecurityDefinitions((JSONObject) object)); } else if ("security".equalsIgnoreCase(key)) { swagger.setSecurityRequirement(parseSecurity((JSONObject) object)); } else if ("tags".equalsIgnoreCase(key)) { swagger.setTags(parseTags((JSONArray) object)); } else if ("externalDocs".equalsIgnoreCase(key)) { swagger.setExternalDocs(parseExternalDocs((JSONObject) object)); } else if (key.startsWith("x-")) { swagger.addVendorExtension(key, parseVendorExtension(object)); } } } if (swagger.getSwagger() == null) { throw new IllegalArgumentException("swagger does not exist."); } if (!swagger.getSwagger().equalsIgnoreCase("2.0")) { throw new IllegalArgumentException("swagger is invalid."); } if (swagger.getInfo() == null) { throw new IllegalArgumentException("info does not exist."); } if (swagger.getPaths() == null) { throw new IllegalArgumentException("paths does not exist."); } return swagger; } private  OpenAPIParser(); static Swagger parse(String jsonString); @SuppressWarnings("unchecked") static Bundle parseJSONObject(JSONObject jsonObject); @SuppressWarnings("unchecked") static ArrayList parseJSONArray(JSONArray jsonArray); }
OpenAPIParser { public static Swagger parse(String jsonString) throws JSONException { JSONObject jsonObject = new JSONObject(jsonString); Swagger swagger = new Swagger(); for (Iterator<String> it = jsonObject.keys(); it.hasNext();) { String key = it.next(); Object object = jsonObject.get(key); if (object != null) { if ("swagger".equalsIgnoreCase(key)) { swagger.setSwagger((String) object); } else if ("basePath".equalsIgnoreCase(key)) { swagger.setBasePath((String) object); } else if ("host".equalsIgnoreCase(key)) { swagger.setHost((String) object); } else if ("info".equalsIgnoreCase(key)) { swagger.setInfo(parseInfo((JSONObject) object)); } else if ("schemes".equalsIgnoreCase(key)) { swagger.setSchemes(parseStringArray((JSONArray) object)); } else if ("consumes".equalsIgnoreCase(key)) { swagger.setConsumes(parseStringArray((JSONArray) object)); } else if ("produces".equalsIgnoreCase(key)) { swagger.setProduces(parseStringArray((JSONArray) object)); } else if ("paths".equalsIgnoreCase(key)) { swagger.setPaths(parsePaths((JSONObject) object)); } else if ("definitions".equalsIgnoreCase(key)) { swagger.setDefinitions(parseDefinitions((JSONObject) object)); } else if ("parameters".equalsIgnoreCase(key)) { swagger.setParameters(parseParameters((JSONObject) object)); } else if ("responses".equalsIgnoreCase(key)) { swagger.setResponses(parseResponsesWithRoot((JSONObject) object)); } else if ("securityDefinitions".equalsIgnoreCase(key)) { swagger.setSecurityDefinitions(parseSecurityDefinitions((JSONObject) object)); } else if ("security".equalsIgnoreCase(key)) { swagger.setSecurityRequirement(parseSecurity((JSONObject) object)); } else if ("tags".equalsIgnoreCase(key)) { swagger.setTags(parseTags((JSONArray) object)); } else if ("externalDocs".equalsIgnoreCase(key)) { swagger.setExternalDocs(parseExternalDocs((JSONObject) object)); } else if (key.startsWith("x-")) { swagger.addVendorExtension(key, parseVendorExtension(object)); } } } if (swagger.getSwagger() == null) { throw new IllegalArgumentException("swagger does not exist."); } if (!swagger.getSwagger().equalsIgnoreCase("2.0")) { throw new IllegalArgumentException("swagger is invalid."); } if (swagger.getInfo() == null) { throw new IllegalArgumentException("info does not exist."); } if (swagger.getPaths() == null) { throw new IllegalArgumentException("paths does not exist."); } return swagger; } private  OpenAPIParser(); static Swagger parse(String jsonString); @SuppressWarnings("unchecked") static Bundle parseJSONObject(JSONObject jsonObject); @SuppressWarnings("unchecked") static ArrayList parseJSONArray(JSONArray jsonArray);  }