@Test public void testPaths() { String json = "{\n" + " \"openapi\": \"3.0.0\",\n" + " \"paths\": {\n" + " \"/pet\": {\n" + " \"foo\": \"bar\",\n" + " \"get\": {\n" + " \"security\": [\n" + " {\n" + " \"petstore_auth\": [\n" + " \"write:pets\",\n" + " \"read:pets\"\n" + " ]\n" + " }\n" + " ]\n" + " }\n" + " }\n" + " }\n" + "}"; OpenAPIV3Parser parser = new OpenAPIV3Parser(); SwaggerParseResult result = parser.readContents(json, null, null); List<String> messageList = result.getMessages(); Set<String> messages = new HashSet<>(messageList); assertTrue(messages.contains("attribute paths.'/pet'.foo is unexpected")); OpenAPI openAPI = result.getOpenAPI(); PathItem path = openAPI.getPaths().get("/pet"); assertNotNull(path); Operation operation = path.getGet(); assertNotNull(operation); List<SecurityRequirement> security = operation.getSecurity(); assertTrue(security.size() == 1); Map<String, List<String>> requirement = security.get(0); assertTrue(requirement.containsKey("petstore_auth")); List<String> scopesList = requirement.get("petstore_auth"); Set<String> scopes = new HashSet<>(scopesList); assertTrue(scopes.contains("read:pets")); assertTrue(scopes.contains("write:pets")); }
public Paths getPaths(ObjectNode obj, String location, ParseResult result) { final Paths paths = new Paths(); if (obj == null) { return null; } Set<String> pathKeys = getKeys(obj); for (String pathName : pathKeys) { JsonNode pathValue = obj.get(pathName); if(pathName.startsWith("x-")) { Map <String,Object> extensions = getExtensions(obj); if(extensions != null && extensions.size() > 0) { paths.setExtensions(extensions); } } else { if (!pathValue.getNodeType().equals(JsonNodeType.OBJECT)) { result.invalidType(location, pathName, "object", pathValue); } else { if(!pathName.startsWith("/")){ result.warning(location," Resource "+pathName+ " should start with /"); } ObjectNode path = (ObjectNode) pathValue; PathItem pathObj = getPathItem(path,String.format("%s.'%s'", location,pathName), result); String[] eachPart = pathName.split("[-/.]+"); Arrays.stream(eachPart) .filter(part -> part.startsWith("{") && part.endsWith("}") && part.length() > 2) .forEach(part -> { String pathParam = part.substring(1, part.length() - 1); boolean definedInPathLevel = isPathParamDefined(pathParam, pathObj.getParameters()); if (!definedInPathLevel) { List<Operation> operationsInAPath = getAllOperationsInAPath(pathObj); operationsInAPath.forEach(operation -> { List<Parameter> operationParameters = operation.getParameters(); if (operationParameters == null) { operationParameters = Collections.<Parameter>emptyList(); } operationParameters.forEach(parameter -> { if(PATH_PARAMETER.equalsIgnoreCase(parameter.getIn()) && Boolean.FALSE.equals(parameter.getRequired())){ result.warning(location, "For path parameter "+ parameter.getName() + " the required value should be true"); } }); if (!isPathParamDefined(pathParam, operationParameters)) { result.warning(location + ".'" + pathName + "'"," Declared path parameter " + pathParam + " needs to be defined as a path parameter in path or operation level"); return; } }); } }); paths.put(pathName, pathObj); } } } return paths; }
OpenAPIDeserializer { public Paths getPaths(ObjectNode obj, String location, ParseResult result) { final Paths paths = new Paths(); if (obj == null) { return null; } Set<String> pathKeys = getKeys(obj); for (String pathName : pathKeys) { JsonNode pathValue = obj.get(pathName); if(pathName.startsWith("x-")) { Map <String,Object> extensions = getExtensions(obj); if(extensions != null && extensions.size() > 0) { paths.setExtensions(extensions); } } else { if (!pathValue.getNodeType().equals(JsonNodeType.OBJECT)) { result.invalidType(location, pathName, "object", pathValue); } else { if(!pathName.startsWith("/")){ result.warning(location," Resource "+pathName+ " should start with /"); } ObjectNode path = (ObjectNode) pathValue; PathItem pathObj = getPathItem(path,String.format("%s.'%s'", location,pathName), result); String[] eachPart = pathName.split("[-/.]+"); Arrays.stream(eachPart) .filter(part -> part.startsWith("{") && part.endsWith("}") && part.length() > 2) .forEach(part -> { String pathParam = part.substring(1, part.length() - 1); boolean definedInPathLevel = isPathParamDefined(pathParam, pathObj.getParameters()); if (!definedInPathLevel) { List<Operation> operationsInAPath = getAllOperationsInAPath(pathObj); operationsInAPath.forEach(operation -> { List<Parameter> operationParameters = operation.getParameters(); if (operationParameters == null) { operationParameters = Collections.<Parameter>emptyList(); } operationParameters.forEach(parameter -> { if(PATH_PARAMETER.equalsIgnoreCase(parameter.getIn()) && Boolean.FALSE.equals(parameter.getRequired())){ result.warning(location, "For path parameter "+ parameter.getName() + " the required value should be true"); } }); if (!isPathParamDefined(pathParam, operationParameters)) { result.warning(location + ".'" + pathName + "'"," Declared path parameter " + pathParam + " needs to be defined as a path parameter in path or operation level"); return; } }); } }); paths.put(pathName, pathObj); } } } return paths; } }
OpenAPIDeserializer { public Paths getPaths(ObjectNode obj, String location, ParseResult result) { final Paths paths = new Paths(); if (obj == null) { return null; } Set<String> pathKeys = getKeys(obj); for (String pathName : pathKeys) { JsonNode pathValue = obj.get(pathName); if(pathName.startsWith("x-")) { Map <String,Object> extensions = getExtensions(obj); if(extensions != null && extensions.size() > 0) { paths.setExtensions(extensions); } } else { if (!pathValue.getNodeType().equals(JsonNodeType.OBJECT)) { result.invalidType(location, pathName, "object", pathValue); } else { if(!pathName.startsWith("/")){ result.warning(location," Resource "+pathName+ " should start with /"); } ObjectNode path = (ObjectNode) pathValue; PathItem pathObj = getPathItem(path,String.format("%s.'%s'", location,pathName), result); String[] eachPart = pathName.split("[-/.]+"); Arrays.stream(eachPart) .filter(part -> part.startsWith("{") && part.endsWith("}") && part.length() > 2) .forEach(part -> { String pathParam = part.substring(1, part.length() - 1); boolean definedInPathLevel = isPathParamDefined(pathParam, pathObj.getParameters()); if (!definedInPathLevel) { List<Operation> operationsInAPath = getAllOperationsInAPath(pathObj); operationsInAPath.forEach(operation -> { List<Parameter> operationParameters = operation.getParameters(); if (operationParameters == null) { operationParameters = Collections.<Parameter>emptyList(); } operationParameters.forEach(parameter -> { if(PATH_PARAMETER.equalsIgnoreCase(parameter.getIn()) && Boolean.FALSE.equals(parameter.getRequired())){ result.warning(location, "For path parameter "+ parameter.getName() + " the required value should be true"); } }); if (!isPathParamDefined(pathParam, operationParameters)) { result.warning(location + ".'" + pathName + "'"," Declared path parameter " + pathParam + " needs to be defined as a path parameter in path or operation level"); return; } }); } }); paths.put(pathName, pathObj); } } } return paths; }  }
OpenAPIDeserializer { public Paths getPaths(ObjectNode obj, String location, ParseResult result) { final Paths paths = new Paths(); if (obj == null) { return null; } Set<String> pathKeys = getKeys(obj); for (String pathName : pathKeys) { JsonNode pathValue = obj.get(pathName); if(pathName.startsWith("x-")) { Map <String,Object> extensions = getExtensions(obj); if(extensions != null && extensions.size() > 0) { paths.setExtensions(extensions); } } else { if (!pathValue.getNodeType().equals(JsonNodeType.OBJECT)) { result.invalidType(location, pathName, "object", pathValue); } else { if(!pathName.startsWith("/")){ result.warning(location," Resource "+pathName+ " should start with /"); } ObjectNode path = (ObjectNode) pathValue; PathItem pathObj = getPathItem(path,String.format("%s.'%s'", location,pathName), result); String[] eachPart = pathName.split("[-/.]+"); Arrays.stream(eachPart) .filter(part -> part.startsWith("{") && part.endsWith("}") && part.length() > 2) .forEach(part -> { String pathParam = part.substring(1, part.length() - 1); boolean definedInPathLevel = isPathParamDefined(pathParam, pathObj.getParameters()); if (!definedInPathLevel) { List<Operation> operationsInAPath = getAllOperationsInAPath(pathObj); operationsInAPath.forEach(operation -> { List<Parameter> operationParameters = operation.getParameters(); if (operationParameters == null) { operationParameters = Collections.<Parameter>emptyList(); } operationParameters.forEach(parameter -> { if(PATH_PARAMETER.equalsIgnoreCase(parameter.getIn()) && Boolean.FALSE.equals(parameter.getRequired())){ result.warning(location, "For path parameter "+ parameter.getName() + " the required value should be true"); } }); if (!isPathParamDefined(pathParam, operationParameters)) { result.warning(location + ".'" + pathName + "'"," Declared path parameter " + pathParam + " needs to be defined as a path parameter in path or operation level"); return; } }); } }); paths.put(pathName, pathObj); } } } return paths; }  SwaggerParseResult deserialize(JsonNode rootNode); SwaggerParseResult deserialize(JsonNode rootNode, String path); OpenAPI parseRoot(JsonNode node, ParseResult result, String path); String mungedRef(String refString); Map<String,Object> getExtensions(ObjectNode node); Components getComponents(ObjectNode obj, String location, ParseResult result); List<Tag> getTagList(ArrayNode obj, String location, ParseResult result); Tag getTag(ObjectNode obj, String location, ParseResult result); List<Server> getServersList(ArrayNode obj, String location, ParseResult result, String path); List<Server> getServersList(ArrayNode obj, String location, ParseResult result); Server getServer(ObjectNode obj, String location, ParseResult result); Server getServer(ObjectNode obj, String location, ParseResult result, String path); ServerVariables getServerVariables(ObjectNode obj, String location, ParseResult result); ServerVariable getServerVariable(ObjectNode obj, String location, ParseResult result); Paths getPaths(ObjectNode obj, String location, ParseResult result); List<Operation> getAllOperationsInAPath(PathItem pathObj); PathItem getPathItem(ObjectNode obj, String location, ParseResult result); ExternalDocumentation getExternalDocs(ObjectNode node, String location, ParseResult result); String getString(String key, ObjectNode node, boolean required, String location, ParseResult result, Set<String> uniqueValues); String getString(String key, ObjectNode node, boolean required, String location, ParseResult result); Set<String> getKeys(ObjectNode node); ObjectNode getObject(String key, ObjectNode node, boolean required, String location, ParseResult result); Info getInfo(ObjectNode node, String location, ParseResult result); License getLicense(ObjectNode node, String location, ParseResult result); Contact getContact(ObjectNode node, String location, ParseResult result); Content getContent(ObjectNode node, String location, ParseResult result); MediaType getMediaType(ObjectNode contentNode, String location, ParseResult result); Map<String,Encoding> getEncodingMap(ObjectNode node, String location, ParseResult result); Encoding getEncoding(ObjectNode node, String location, ParseResult result); Map<String, Link> getLinks(ObjectNode obj, String location, ParseResult result, boolean underComponents); Link getLink(ObjectNode linkNode, String location, ParseResult result); Map <String,Callback> getCallbacks(ObjectNode node, String location, ParseResult result, boolean underComponents); Callback getCallback(ObjectNode node,String location, ParseResult result); XML getXml(ObjectNode node, String location, ParseResult result); ArrayNode getArray(String key, ObjectNode node, boolean required, String location, ParseResult result); Boolean getBoolean(String key, ObjectNode node, boolean required, String location, ParseResult result); BigDecimal getBigDecimal(String key, ObjectNode node, boolean required, String location, ParseResult result); Integer getInteger(String key, ObjectNode node, boolean required, String location, ParseResult result); Map<String, Parameter> getParameters(ObjectNode obj, String location, ParseResult result, boolean underComponents); List<Parameter> getParameterList(ArrayNode obj, String location, ParseResult result); Parameter getParameter(ObjectNode obj, String location, ParseResult result); Map<String, Header> getHeaders(ObjectNode obj, String location, ParseResult result, boolean underComponents); Header getHeader(ObjectNode headerNode, String location, ParseResult result); Object getAnyExample(String nodeKey,ObjectNode node, String location, ParseResult result ); Map<String, SecurityScheme> getSecuritySchemes(ObjectNode obj, String location, ParseResult result, boolean underComponents); SecurityScheme getSecurityScheme(ObjectNode node, String location, ParseResult result); OAuthFlows getOAuthFlows(ObjectNode node, String location, ParseResult result); OAuthFlow getOAuthFlow(String oAuthFlowType, ObjectNode node, String location, ParseResult result); Map<String, Schema> getSchemas(ObjectNode obj, String location, ParseResult result, boolean underComponents); Discriminator getDiscriminator(ObjectNode node, String location, ParseResult result); Schema getSchema(ObjectNode node, String location, ParseResult result); Map<String, Example> getExamples(ObjectNode obj, String location, ParseResult result, boolean  underComponents); List<Example> getExampleList(ArrayNode obj, String location, ParseResult result); Example getExample(ObjectNode node, String location, ParseResult result); void setStyle(String value, Parameter parameter, String location, ObjectNode obj, ParseResult result); ApiResponses getResponses(ObjectNode node, String location, ParseResult result, boolean underComponents); ApiResponse getResponse(ObjectNode node, String location, ParseResult result); List<String> getTagsStrings(ArrayNode nodes, String location, ParseResult result); Operation getOperation(ObjectNode obj, String location, ParseResult result); List<SecurityRequirement> getSecurityRequirementsList(ArrayNode nodes, String location, ParseResult result); Map<String, RequestBody> getRequestBodies(ObjectNode obj, String location, ParseResult result, boolean underComponents); String inferTypeFromArray(ArrayNode an); }
OpenAPIDeserializer { public Paths getPaths(ObjectNode obj, String location, ParseResult result) { final Paths paths = new Paths(); if (obj == null) { return null; } Set<String> pathKeys = getKeys(obj); for (String pathName : pathKeys) { JsonNode pathValue = obj.get(pathName); if(pathName.startsWith("x-")) { Map <String,Object> extensions = getExtensions(obj); if(extensions != null && extensions.size() > 0) { paths.setExtensions(extensions); } } else { if (!pathValue.getNodeType().equals(JsonNodeType.OBJECT)) { result.invalidType(location, pathName, "object", pathValue); } else { if(!pathName.startsWith("/")){ result.warning(location," Resource "+pathName+ " should start with /"); } ObjectNode path = (ObjectNode) pathValue; PathItem pathObj = getPathItem(path,String.format("%s.'%s'", location,pathName), result); String[] eachPart = pathName.split("[-/.]+"); Arrays.stream(eachPart) .filter(part -> part.startsWith("{") && part.endsWith("}") && part.length() > 2) .forEach(part -> { String pathParam = part.substring(1, part.length() - 1); boolean definedInPathLevel = isPathParamDefined(pathParam, pathObj.getParameters()); if (!definedInPathLevel) { List<Operation> operationsInAPath = getAllOperationsInAPath(pathObj); operationsInAPath.forEach(operation -> { List<Parameter> operationParameters = operation.getParameters(); if (operationParameters == null) { operationParameters = Collections.<Parameter>emptyList(); } operationParameters.forEach(parameter -> { if(PATH_PARAMETER.equalsIgnoreCase(parameter.getIn()) && Boolean.FALSE.equals(parameter.getRequired())){ result.warning(location, "For path parameter "+ parameter.getName() + " the required value should be true"); } }); if (!isPathParamDefined(pathParam, operationParameters)) { result.warning(location + ".'" + pathName + "'"," Declared path parameter " + pathParam + " needs to be defined as a path parameter in path or operation level"); return; } }); } }); paths.put(pathName, pathObj); } } } return paths; }  SwaggerParseResult deserialize(JsonNode rootNode); SwaggerParseResult deserialize(JsonNode rootNode, String path); OpenAPI parseRoot(JsonNode node, ParseResult result, String path); String mungedRef(String refString); Map<String,Object> getExtensions(ObjectNode node); Components getComponents(ObjectNode obj, String location, ParseResult result); List<Tag> getTagList(ArrayNode obj, String location, ParseResult result); Tag getTag(ObjectNode obj, String location, ParseResult result); List<Server> getServersList(ArrayNode obj, String location, ParseResult result, String path); List<Server> getServersList(ArrayNode obj, String location, ParseResult result); Server getServer(ObjectNode obj, String location, ParseResult result); Server getServer(ObjectNode obj, String location, ParseResult result, String path); ServerVariables getServerVariables(ObjectNode obj, String location, ParseResult result); ServerVariable getServerVariable(ObjectNode obj, String location, ParseResult result); Paths getPaths(ObjectNode obj, String location, ParseResult result); List<Operation> getAllOperationsInAPath(PathItem pathObj); PathItem getPathItem(ObjectNode obj, String location, ParseResult result); ExternalDocumentation getExternalDocs(ObjectNode node, String location, ParseResult result); String getString(String key, ObjectNode node, boolean required, String location, ParseResult result, Set<String> uniqueValues); String getString(String key, ObjectNode node, boolean required, String location, ParseResult result); Set<String> getKeys(ObjectNode node); ObjectNode getObject(String key, ObjectNode node, boolean required, String location, ParseResult result); Info getInfo(ObjectNode node, String location, ParseResult result); License getLicense(ObjectNode node, String location, ParseResult result); Contact getContact(ObjectNode node, String location, ParseResult result); Content getContent(ObjectNode node, String location, ParseResult result); MediaType getMediaType(ObjectNode contentNode, String location, ParseResult result); Map<String,Encoding> getEncodingMap(ObjectNode node, String location, ParseResult result); Encoding getEncoding(ObjectNode node, String location, ParseResult result); Map<String, Link> getLinks(ObjectNode obj, String location, ParseResult result, boolean underComponents); Link getLink(ObjectNode linkNode, String location, ParseResult result); Map <String,Callback> getCallbacks(ObjectNode node, String location, ParseResult result, boolean underComponents); Callback getCallback(ObjectNode node,String location, ParseResult result); XML getXml(ObjectNode node, String location, ParseResult result); ArrayNode getArray(String key, ObjectNode node, boolean required, String location, ParseResult result); Boolean getBoolean(String key, ObjectNode node, boolean required, String location, ParseResult result); BigDecimal getBigDecimal(String key, ObjectNode node, boolean required, String location, ParseResult result); Integer getInteger(String key, ObjectNode node, boolean required, String location, ParseResult result); Map<String, Parameter> getParameters(ObjectNode obj, String location, ParseResult result, boolean underComponents); List<Parameter> getParameterList(ArrayNode obj, String location, ParseResult result); Parameter getParameter(ObjectNode obj, String location, ParseResult result); Map<String, Header> getHeaders(ObjectNode obj, String location, ParseResult result, boolean underComponents); Header getHeader(ObjectNode headerNode, String location, ParseResult result); Object getAnyExample(String nodeKey,ObjectNode node, String location, ParseResult result ); Map<String, SecurityScheme> getSecuritySchemes(ObjectNode obj, String location, ParseResult result, boolean underComponents); SecurityScheme getSecurityScheme(ObjectNode node, String location, ParseResult result); OAuthFlows getOAuthFlows(ObjectNode node, String location, ParseResult result); OAuthFlow getOAuthFlow(String oAuthFlowType, ObjectNode node, String location, ParseResult result); Map<String, Schema> getSchemas(ObjectNode obj, String location, ParseResult result, boolean underComponents); Discriminator getDiscriminator(ObjectNode node, String location, ParseResult result); Schema getSchema(ObjectNode node, String location, ParseResult result); Map<String, Example> getExamples(ObjectNode obj, String location, ParseResult result, boolean  underComponents); List<Example> getExampleList(ArrayNode obj, String location, ParseResult result); Example getExample(ObjectNode node, String location, ParseResult result); void setStyle(String value, Parameter parameter, String location, ObjectNode obj, ParseResult result); ApiResponses getResponses(ObjectNode node, String location, ParseResult result, boolean underComponents); ApiResponse getResponse(ObjectNode node, String location, ParseResult result); List<String> getTagsStrings(ArrayNode nodes, String location, ParseResult result); Operation getOperation(ObjectNode obj, String location, ParseResult result); List<SecurityRequirement> getSecurityRequirementsList(ArrayNode nodes, String location, ParseResult result); Map<String, RequestBody> getRequestBodies(ObjectNode obj, String location, ParseResult result, boolean underComponents); String inferTypeFromArray(ArrayNode an);  }