Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Verify if combination of (method) path and operation exists #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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