Skip to content

Commit

Permalink
Verify if combination of (method) path and operation exists
Browse files Browse the repository at this point in the history
  • Loading branch information
Roy Willemse committed Mar 16, 2016
1 parent 8c74143 commit a979dd8
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 39 deletions.
53 changes: 23 additions & 30 deletions src/main/java/com/kenshoo/swagger/validator/ResourceValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Object> entry : resource.entrySet()) {
String key = entry.getKey();
if (key.startsWith("x-")) {
Expand All @@ -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
Expand All @@ -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;
}

Expand Down
12 changes: 10 additions & 2 deletions src/test/java/com/kenshoo/swagger/validator/SimpleResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<SimpleModel> getAllSomethings() {
return Collections.singletonList(new SimpleModel());
}

@GET
@Path("/{id}")
public SimpleModel getSomething(@PathParam("id") String id) {
return new SimpleModel();
}

Expand All @@ -18,5 +27,4 @@ public SimpleModel postSomething(SimpleModel post) {
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down
6 changes: 4 additions & 2 deletions src/test/resources/forbidden_type.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
6 changes: 4 additions & 2 deletions src/test/resources/missing_operation.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
6 changes: 4 additions & 2 deletions src/test/resources/no_xjava_definition.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
59 changes: 59 additions & 0 deletions src/test/resources/path_operation_mismatch.yaml
Original file line number Diff line number Diff line change
@@ -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
21 changes: 20 additions & 1 deletion src/test/resources/valid.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -32,7 +52,6 @@ paths:
schema:
$ref: '#/definitions/errorModel'
x-javaClass: com.kenshoo.swagger.validator.SimpleResource


definitions:

Expand Down

0 comments on commit a979dd8

Please sign in to comment.