Skip to content

Commit

Permalink
Merge pull request #1 from eirnym/annotation_ext
Browse files Browse the repository at this point in the history
Add ability to use the annotation on parameter, add classpath usage
  • Loading branch information
JanLoebel authored Mar 27, 2020
2 parents a9b02fe + 28841e5 commit 1b85c31
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 61 deletions.
118 changes: 66 additions & 52 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,52 +1,66 @@
# Json-Schema-Validation-Starter

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.

<a href="https://www.buymeacoffee.com/JanLoebel" rel="Buy me a coffee!">![Foo](https://cdn.buymeacoffee.com/buttons/default-orange.png)</a>

## Usage

Include the starter into you're project.

You need to add jitpack to your `pom.xml` because this project is not available in the official maven repository.
```
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
```

Add the `json-schema-validation-starter`-dependency to your `pom.xml`
```
<dependency>
<groupId>com.github.JanLoebel</groupId>
<artifactId>json-schema-validation-starter</artifactId>
<version>2.0.0</version>
</dependency>
```

After that simply create a json-schema and put it into e.g.: `resources/jsonschema/book.json`.
The last step is now to let your entity know that it should be validated and which schema it should use.

```
@JsonSchemaValidation("classpath:jsonschema/book.json")
public class Book {
private String title;
private String author;
}
```

## Example project
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.

## Contribution
Please feel free to improve or modify the code and open a Pull-Request! Any contribution is welcome :)

## License
MIT License

Copyright (c) 2019 Jan Löbel

See LICENSE file for details.
# Json-Schema-Validation-Starter

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.

<a href="https://www.buymeacoffee.com/JanLoebel" rel="Buy me a coffee!">![Foo](https://cdn.buymeacoffee.com/buttons/default-orange.png)</a>

## Usage

Include the starter into you're project.

You need to add jitpack to your `pom.xml` because this project is not available in the official maven repository.
```
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
```

Add the `json-schema-validation-starter`-dependency to your `pom.xml`
```
<dependency>
<groupId>com.github.JanLoebel</groupId>
<artifactId>json-schema-validation-starter</artifactId>
<version>2.0.0</version>
</dependency>
```

After that simply create a json-schema and put it into e.g.: `resources/jsonschema/book.json`.
The last step is now to let your entity know that it should be validated and which schema it should use.

```
@JsonSchemaValidation("classpath:jsonschema/book.json")
public class Book {
private String title;
private String author;
}
```

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.

```
@RestController
@RequestMapping("/books")
public BooksConroller {
@PostMapping
public ResponseEntity<Book> createBook(@RequestBody @JsonSchemaValidation("classpath:jsonschema/book.json") Book bookDto) {
//...
return bookDto;
}
}
```

## Example project
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.

## Contribution
Please feel free to improve or modify the code and open a Pull-Request! Any contribution is welcome :)

## License
MIT License

Copyright (c) 2019 Jan Löbel

See LICENSE file for details.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.TYPE)
@Target({ElementType.TYPE, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface JsonSchemaValidation {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ public JsonValidationRequestBodyControllerAdvice(ObjectMapper objectMapper, Json
@Override
public boolean supports(MethodParameter parameter,
Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {
return parameter.getNestedParameterType().isAnnotationPresent(JsonSchemaValidation.class);
return parameter.hasParameterAnnotation(JsonSchemaValidation.class) ||
parameter.getNestedParameterType().isAnnotationPresent(JsonSchemaValidation.class);
}

@Override
Expand All @@ -53,8 +54,14 @@ public HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, MethodPara
}

private String extractSchemaUri(MethodParameter parameter) {
final JsonSchemaValidation annotation =
parameter.getNestedParameterType().getAnnotation(JsonSchemaValidation.class);
final JsonSchemaValidation annotation;
if (parameter.hasParameterAnnotation(JsonSchemaValidation.class)) {
annotation = parameter.getParameterAnnotation(JsonSchemaValidation.class);
} else if (parameter.getNestedParameterType().isAnnotationPresent(JsonSchemaValidation.class)) {
annotation = parameter.getNestedParameterType().getAnnotation(JsonSchemaValidation.class);
} else {
throw new IllegalArgumentException(String.valueOf(parameter));
}
return annotation.value();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@

import com.networknt.schema.*;
import com.github.JanLoebel.jsonschemavalidation.JsonSchemaValidationException;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.ResourceUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.*;
import java.util.Collection;

public class DefaultJsonSchemaProvider implements JsonSchemaProvider {
Expand Down Expand Up @@ -44,8 +42,12 @@ private InputStream createInputStream(String url) {
// TODO validate this
return new FileInputStream(new File(url));
}
if (url.toLowerCase().startsWith("classpath:")) {
return new ClassPathResource(url.substring("classpath:".length())).getInputStream();
}

return new FileInputStream(ResourceUtils.getFile(url));
} catch (FileNotFoundException e) {
} catch (IOException e) {
throw new IllegalStateException("Could not load url: " + url, e);
}
}
Expand Down

0 comments on commit 1b85c31

Please sign in to comment.