Skip to content

Commit 1b85c31

Browse files
authored
Merge pull request #1 from eirnym/annotation_ext
Add ability to use the annotation on parameter, add classpath usage
2 parents a9b02fe + 28841e5 commit 1b85c31

File tree

4 files changed

+84
-61
lines changed

4 files changed

+84
-61
lines changed

README.md

Lines changed: 66 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,66 @@
1-
# Json-Schema-Validation-Starter
2-
3-
This provides a Spring-Boot-Starter to include JsonSchemaValidation with the help of the [https://github.com/networknt/json-schema-validator](https://github.com/networknt/json-schema-validator) -library.
4-
5-
<a href="https://www.buymeacoffee.com/JanLoebel" rel="Buy me a coffee!">![Foo](https://cdn.buymeacoffee.com/buttons/default-orange.png)</a>
6-
7-
## Usage
8-
9-
Include the starter into you're project.
10-
11-
You need to add jitpack to your `pom.xml` because this project is not available in the official maven repository.
12-
```
13-
<repositories>
14-
<repository>
15-
<id>jitpack.io</id>
16-
<url>https://jitpack.io</url>
17-
</repository>
18-
</repositories>
19-
```
20-
21-
Add the `json-schema-validation-starter`-dependency to your `pom.xml`
22-
```
23-
<dependency>
24-
<groupId>com.github.JanLoebel</groupId>
25-
<artifactId>json-schema-validation-starter</artifactId>
26-
<version>2.0.0</version>
27-
</dependency>
28-
```
29-
30-
After that simply create a json-schema and put it into e.g.: `resources/jsonschema/book.json`.
31-
The last step is now to let your entity know that it should be validated and which schema it should use.
32-
33-
```
34-
@JsonSchemaValidation("classpath:jsonschema/book.json")
35-
public class Book {
36-
private String title;
37-
private String author;
38-
}
39-
```
40-
41-
## Example project
42-
Head over to [http://github.com/JanLoebel/json-schema-validation-starter-example](http://github.com/JanLoebel/json-schema-validation-starter-example) to checkout the sample project.
43-
44-
## Contribution
45-
Please feel free to improve or modify the code and open a Pull-Request! Any contribution is welcome :)
46-
47-
## License
48-
MIT License
49-
50-
Copyright (c) 2019 Jan Löbel
51-
52-
See LICENSE file for details.
1+
# Json-Schema-Validation-Starter
2+
3+
This provides a Spring-Boot-Starter to include JsonSchemaValidation with the help of the [https://github.com/networknt/json-schema-validator](https://github.com/networknt/json-schema-validator) -library.
4+
5+
<a href="https://www.buymeacoffee.com/JanLoebel" rel="Buy me a coffee!">![Foo](https://cdn.buymeacoffee.com/buttons/default-orange.png)</a>
6+
7+
## Usage
8+
9+
Include the starter into you're project.
10+
11+
You need to add jitpack to your `pom.xml` because this project is not available in the official maven repository.
12+
```
13+
<repositories>
14+
<repository>
15+
<id>jitpack.io</id>
16+
<url>https://jitpack.io</url>
17+
</repository>
18+
</repositories>
19+
```
20+
21+
Add the `json-schema-validation-starter`-dependency to your `pom.xml`
22+
```
23+
<dependency>
24+
<groupId>com.github.JanLoebel</groupId>
25+
<artifactId>json-schema-validation-starter</artifactId>
26+
<version>2.0.0</version>
27+
</dependency>
28+
```
29+
30+
After that simply create a json-schema and put it into e.g.: `resources/jsonschema/book.json`.
31+
The last step is now to let your entity know that it should be validated and which schema it should use.
32+
33+
```
34+
@JsonSchemaValidation("classpath:jsonschema/book.json")
35+
public class Book {
36+
private String title;
37+
private String author;
38+
}
39+
```
40+
41+
Alternatively, you need to add this annotation in your controller like below. This is helpful when you generate your classes from a schema or can't edit them.
42+
43+
```
44+
@RestController
45+
@RequestMapping("/books")
46+
public BooksConroller {
47+
@PostMapping
48+
public ResponseEntity<Book> createBook(@RequestBody @JsonSchemaValidation("classpath:jsonschema/book.json") Book bookDto) {
49+
//...
50+
return bookDto;
51+
}
52+
}
53+
```
54+
55+
## Example project
56+
Head over to [http://github.com/JanLoebel/json-schema-validation-starter-example](http://github.com/JanLoebel/json-schema-validation-starter-example) to checkout the sample project.
57+
58+
## Contribution
59+
Please feel free to improve or modify the code and open a Pull-Request! Any contribution is welcome :)
60+
61+
## License
62+
MIT License
63+
64+
Copyright (c) 2019 Jan Löbel
65+
66+
See LICENSE file for details.

src/main/java/com/github/JanLoebel/jsonschemavalidation/JsonSchemaValidation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import java.lang.annotation.RetentionPolicy;
66
import java.lang.annotation.Target;
77

8-
@Target(ElementType.TYPE)
8+
@Target({ElementType.TYPE, ElementType.PARAMETER})
99
@Retention(RetentionPolicy.RUNTIME)
1010
public @interface JsonSchemaValidation {
1111

src/main/java/com/github/JanLoebel/jsonschemavalidation/advice/JsonValidationRequestBodyControllerAdvice.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ public JsonValidationRequestBodyControllerAdvice(ObjectMapper objectMapper, Json
3535
@Override
3636
public boolean supports(MethodParameter parameter,
3737
Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {
38-
return parameter.getNestedParameterType().isAnnotationPresent(JsonSchemaValidation.class);
38+
return parameter.hasParameterAnnotation(JsonSchemaValidation.class) ||
39+
parameter.getNestedParameterType().isAnnotationPresent(JsonSchemaValidation.class);
3940
}
4041

4142
@Override
@@ -53,8 +54,14 @@ public HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, MethodPara
5354
}
5455

5556
private String extractSchemaUri(MethodParameter parameter) {
56-
final JsonSchemaValidation annotation =
57-
parameter.getNestedParameterType().getAnnotation(JsonSchemaValidation.class);
57+
final JsonSchemaValidation annotation;
58+
if (parameter.hasParameterAnnotation(JsonSchemaValidation.class)) {
59+
annotation = parameter.getParameterAnnotation(JsonSchemaValidation.class);
60+
} else if (parameter.getNestedParameterType().isAnnotationPresent(JsonSchemaValidation.class)) {
61+
annotation = parameter.getNestedParameterType().getAnnotation(JsonSchemaValidation.class);
62+
} else {
63+
throw new IllegalArgumentException(String.valueOf(parameter));
64+
}
5865
return annotation.value();
5966
}
6067

src/main/java/com/github/JanLoebel/jsonschemavalidation/provider/DefaultJsonSchemaProvider.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22

33
import com.networknt.schema.*;
44
import com.github.JanLoebel.jsonschemavalidation.JsonSchemaValidationException;
5+
import org.springframework.core.io.ClassPathResource;
56
import org.springframework.util.ResourceUtils;
67

7-
import java.io.File;
8-
import java.io.FileInputStream;
9-
import java.io.FileNotFoundException;
10-
import java.io.InputStream;
8+
import java.io.*;
119
import java.util.Collection;
1210

1311
public class DefaultJsonSchemaProvider implements JsonSchemaProvider {
@@ -44,8 +42,12 @@ private InputStream createInputStream(String url) {
4442
// TODO validate this
4543
return new FileInputStream(new File(url));
4644
}
45+
if (url.toLowerCase().startsWith("classpath:")) {
46+
return new ClassPathResource(url.substring("classpath:".length())).getInputStream();
47+
}
48+
4749
return new FileInputStream(ResourceUtils.getFile(url));
48-
} catch (FileNotFoundException e) {
50+
} catch (IOException e) {
4951
throw new IllegalStateException("Could not load url: " + url, e);
5052
}
5153
}

0 commit comments

Comments
 (0)