|
6 | 6 | package io.openapiparser; |
7 | 7 |
|
8 | 8 | import io.openapiparser.model.v30.OpenApi; |
| 9 | +import io.openapiparser.model.v30.PathItem; |
9 | 10 | import io.openapiprocessor.interfaces.Converter; |
10 | 11 | import io.openapiprocessor.interfaces.Reader; |
11 | 12 | import io.openapiprocessor.jsonschema.reader.UriReader; |
12 | 13 | import io.openapiprocessor.jsonschema.schema.*; |
13 | 14 | import io.openapiprocessor.jsonschema.validator.Validator; |
14 | 15 | import io.openapiprocessor.jsonschema.validator.ValidatorSettings; |
15 | | -import io.openapiprocessor.jsonschema.validator.result.MessageTextBuilder; |
16 | 16 | import io.openapiprocessor.snakeyaml.SnakeYamlConverter; |
| 17 | +import org.junit.jupiter.api.Test; |
17 | 18 |
|
18 | 19 | import java.util.Collection; |
19 | 20 |
|
20 | | -public class SetupExample { |
| 21 | +public class SetupExampleTest { |
21 | 22 |
|
| 23 | + @Test |
22 | 24 | void parseAndValidate () { |
| 25 | + // 1. create a document loader. |
| 26 | + // It loads a document by uri and converts it to a Map<String, Object> |
| 27 | + // object tree that represents the OpenAPI document. The parser |
| 28 | + // operates on that Object tree which makes it independent of the |
| 29 | + // object mapper (e.g. jackson, snakeyaml etc.). |
| 30 | + // Both (Reader and Converter) have a very simple interface which makes |
| 31 | + // it easy to implement your own. |
23 | 32 | Reader reader = new UriReader (); |
24 | 33 | Converter converter = new SnakeYamlConverter (); |
25 | 34 | // Converter converter = new JacksonConverter (); |
26 | 35 | DocumentLoader loader = new DocumentLoader (reader, converter); |
27 | 36 |
|
| 37 | + // 2. create a resolver. |
| 38 | + // it is responsible for resolving the $ref'erences in the OpenAPI document. |
| 39 | + // The Settings object is initialized with the JSON schema version used by |
| 40 | + // OpenAPI (here Draft 4 for OpenAPI 3.0.x). |
28 | 41 | DocumentStore documents = new DocumentStore (); |
29 | 42 | Resolver.Settings resolverSettings = new Resolver.Settings (SchemaVersion.Draft4); |
30 | 43 | Resolver resolver = new Resolver (documents, loader, resolverSettings); |
31 | 44 |
|
32 | | - // parser OpenAPI file or url |
| 45 | + // 3. parse the OpenAPI from resource or url. |
| 46 | + // here it loads an OpenAPI document from a resource file, but URI works too. |
33 | 47 | OpenApiParser parser = new OpenApiParser (resolver); |
34 | 48 | OpenApiResult result = parser.parse ("openapi.yaml"); |
| 49 | + |
| 50 | + // 4. get the API model from the result to navigate the OpenAPI document. |
35 | 51 | // OpenAPI 3.1.x with model.v31.OpenAPI import |
36 | 52 | OpenApi model = result.getModel (OpenApi.class); |
37 | 53 |
|
38 | | - // validate OpenAPI |
| 54 | + // 5. navigate the model |
| 55 | + PathItem pathItem = model.getPaths ().getPathItem ("/foo"); |
| 56 | + |
| 57 | + // 6. create Validator to validate the OpenAPI schema. |
39 | 58 | SchemaStore store = new SchemaStore (loader); |
40 | 59 | ValidatorSettings settings = new ValidatorSettings (); |
41 | 60 | Validator validator = new Validator (settings); |
| 61 | + |
| 62 | + // 7. validate the OpenAPI schema. |
42 | 63 | boolean valid = result.validate (validator, store); |
43 | 64 |
|
44 | | - // print validation errors |
| 65 | + // 8. print validation errors |
45 | 66 | Collection<ValidationError> errors = result.getValidationErrors (); |
46 | 67 | ValidationErrorTextBuilder builder = new ValidationErrorTextBuilder (); |
47 | 68 |
|
|
0 commit comments