diff --git a/src/main/java/com/kenshoo/swagger/validator/ResourceValidator.java b/src/main/java/com/kenshoo/swagger/validator/ResourceValidator.java index 1623aea..403ec18 100644 --- a/src/main/java/com/kenshoo/swagger/validator/ResourceValidator.java +++ b/src/main/java/com/kenshoo/swagger/validator/ResourceValidator.java @@ -29,7 +29,7 @@ public void validate() { if (cls == null) { handleError("{0} is not defined.", SwaggerValidator.JAVA_CLASS_TAG); } - validatePathAnnotation(cls, path); + for (Map.Entry entry : resource.entrySet()) { String key = entry.getKey(); if (key.startsWith("x-")) { @@ -44,7 +44,7 @@ public void validate() { handleWarning("Operation {0} should not be defined. It's provided by the container.", key); continue; } - if (!isOperationAnnotatedMethodExists(cls, key)) { + if (!isOperationAnnotatedMethodExists(cls, path, key)) { handleError("Method annotated with {0} operation not found in class {1}", key, cls); } else { // operation exists, check that it has tags @@ -59,44 +59,37 @@ public void validate() { } } - private void validatePathAnnotation(Class cls, String path) { - Path pathAnnotation = cls.getAnnotation(Path.class); - if (pathAnnotation == null) { - handleError("Path annotation not found on {0}", cls); - } - - if (path.contains(pathAnnotation.value())) { - String methodPath = path.replace(pathAnnotation.value(), ""); - if (methodPath.length() > 0) { - for (Method m : cls.getMethods()) { - pathAnnotation = m.getAnnotation(Path.class); - if (pathAnnotation != null && pathAnnotation.value().equals(methodPath)) { - return; - } - } - handleError("No path annotation matches {0}", path); - } - } else { - handleError("Path {0} on annotation does not match {1}", pathAnnotation.value(), path); - } - } - /** * returns true if at least one public method with the relevant annotation exists * @param cls + * @param path * @param operation * @return */ - private boolean isOperationAnnotatedMethodExists(Class cls, String operation) { + private boolean isOperationAnnotatedMethodExists(Class cls, String path, String operation) { + Path pathAnnotation = cls.getAnnotation(Path.class); + + if (pathAnnotation == null) { + handleError("Path annotation not found on {0}", cls); + } else if (!path.contains(pathAnnotation.value())) { + handleError("Path {0} on annotation does not match {1}", pathAnnotation.value(), path); + } + + String methodPath = path.replace(pathAnnotation.value(), ""); + for (Method m : cls.getMethods()) { - Annotation[] annotations = m.getDeclaredAnnotations(); - for (Annotation ann : annotations) { - HttpMethod httpMethod = ann.annotationType().getAnnotation(HttpMethod.class); - if (httpMethod != null && httpMethod.value().equalsIgnoreCase(operation)) { - return true; + Path methodPathAnnotation = m.getAnnotation(Path.class); + if ((methodPath.length() == 0 && methodPathAnnotation == null) || (methodPathAnnotation != null && methodPath.equals(methodPathAnnotation.value()))) { + Annotation[] annotations = m.getDeclaredAnnotations(); + for (Annotation ann : annotations) { + HttpMethod httpMethod = ann.annotationType().getAnnotation(HttpMethod.class); + if (httpMethod != null && httpMethod.value().equalsIgnoreCase(operation)) { + return true; + } } } } + return false; } diff --git a/src/test/java/com/kenshoo/swagger/validator/SimpleResource.java b/src/test/java/com/kenshoo/swagger/validator/SimpleResource.java index 50ce48f..6c29e64 100644 --- a/src/test/java/com/kenshoo/swagger/validator/SimpleResource.java +++ b/src/test/java/com/kenshoo/swagger/validator/SimpleResource.java @@ -3,12 +3,21 @@ import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import java.util.Collections; +import java.util.List; @Path("/test") public class SimpleResource { @GET - public SimpleModel getSomething() { + public List getAllSomethings() { + return Collections.singletonList(new SimpleModel()); + } + + @GET + @Path("/{id}") + public SimpleModel getSomething(@PathParam("id") String id) { return new SimpleModel(); } @@ -18,5 +27,4 @@ public SimpleModel postSomething(SimpleModel post) { } - } diff --git a/src/test/java/com/kenshoo/swagger/validator/SwaggerValidatorTest.java b/src/test/java/com/kenshoo/swagger/validator/SwaggerValidatorTest.java index 7186a45..1a62316 100644 --- a/src/test/java/com/kenshoo/swagger/validator/SwaggerValidatorTest.java +++ b/src/test/java/com/kenshoo/swagger/validator/SwaggerValidatorTest.java @@ -29,6 +29,12 @@ public void testMissingOperationYaml() throws Exception { swaggerValidator.validateResources(); } + @Test(expected = ValidationException.class) + public void testPathOperationMismatchYaml() throws Exception { + SwaggerValidator swaggerValidator = new SwaggerValidator(getClass().getResourceAsStream("/path_operation_mismatch.yaml")); + swaggerValidator.validateResources(); + } + @Test(expected = ValidationException.class) public void testForbiddenTypeYaml() throws Exception { SwaggerValidator swaggerValidator = new SwaggerValidator(getClass().getResourceAsStream("/forbidden_type.yaml")); diff --git a/src/test/resources/forbidden_type.yaml b/src/test/resources/forbidden_type.yaml index 5e8cb68..4bfef7e 100644 --- a/src/test/resources/forbidden_type.yaml +++ b/src/test/resources/forbidden_type.yaml @@ -19,9 +19,11 @@ paths: - application/json responses: '200': - description: Returns something + description: Returns somethings schema: - $ref: '#/definitions/sm' + type: array + items: + $ref: "#/definitions/sm" default: description: unexpected error schema: diff --git a/src/test/resources/missing_operation.yaml b/src/test/resources/missing_operation.yaml index 171387f..3e0ac9d 100644 --- a/src/test/resources/missing_operation.yaml +++ b/src/test/resources/missing_operation.yaml @@ -19,9 +19,11 @@ paths: - application/json responses: '200': - description: Returns something + description: Returns somethings schema: - $ref: '#/definitions/sm' + type: array + items: + $ref: "#/definitions/sm" default: description: unexpected error schema: diff --git a/src/test/resources/no_xjava_definition.yaml b/src/test/resources/no_xjava_definition.yaml index b01c9ef..4600602 100644 --- a/src/test/resources/no_xjava_definition.yaml +++ b/src/test/resources/no_xjava_definition.yaml @@ -24,9 +24,11 @@ paths: - application/json responses: '200': - description: Returns something + description: Returns somethings schema: - $ref: '#/definitions/sm' + type: array + items: + $ref: "#/definitions/sm" default: description: unexpected error schema: diff --git a/src/test/resources/path_operation_mismatch.yaml b/src/test/resources/path_operation_mismatch.yaml new file mode 100644 index 0000000..e745fe3 --- /dev/null +++ b/src/test/resources/path_operation_mismatch.yaml @@ -0,0 +1,59 @@ +swagger: '2.0' +info: + version: '2' + title: Test + description: Test. +basePath: '/v2' +schemes: + - https +consumes: + - application/json +produces: + - application/json +paths: + + /test/{id}: + post: + description: Test + tags: + - mytag + parameters: + - in: body + name: body + description: Something to add + required: false + schema: + $ref: "#/definitions/sm" + responses: + '201': + description: Creates something + schema: + $ref: '#/definitions/sm' + default: + description: unexpected error + schema: + $ref: '#/definitions/errorModel' + x-javaClass: com.kenshoo.swagger.validator.SimpleResource + + +definitions: + sm: + properties: + a: + type: string + b: + type: string + c: + type: string + x-javaClass: com.kenshoo.swagger.validator.SimpleModel + + errorModel: + required: + - errorCode + - errorMessage + properties: + errorCode: + type: string + errorMessage: + type: string + x-javaClass: com.kenshoo.platform.rest.ErrorInfo diff --git a/src/test/resources/valid.yaml b/src/test/resources/valid.yaml index 7ffa73f..857c0b1 100644 --- a/src/test/resources/valid.yaml +++ b/src/test/resources/valid.yaml @@ -16,6 +16,26 @@ tags: paths: /test: + get: + description: Test + tags: + - mytag + produces: + - application/json + responses: + '200': + description: Returns somethings + schema: + type: array + items: + $ref: "#/definitions/sm" + default: + description: unexpected error + schema: + $ref: '#/definitions/errorModel' + x-javaClass: com.kenshoo.swagger.validator.SimpleResource + + /test/{id}: get: description: Test tags: @@ -32,7 +52,6 @@ paths: schema: $ref: '#/definitions/errorModel' x-javaClass: com.kenshoo.swagger.validator.SimpleResource - definitions: