|
| 1 | +/* |
| 2 | + * Copyright 2025 https://github.com/openapi-processor/openapi-parser |
| 3 | + * PDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.openapiparser.samples; |
| 7 | + |
| 8 | +import io.openapiparser.*; |
| 9 | +import io.openapiprocessor.interfaces.Converter; |
| 10 | +import io.openapiprocessor.interfaces.Reader; |
| 11 | +import io.openapiprocessor.interfaces.Writer; |
| 12 | +import io.openapiprocessor.jackson.JacksonJsonWriter; |
| 13 | +import io.openapiprocessor.jsonschema.reader.UriReader; |
| 14 | +import io.openapiprocessor.jsonschema.schema.DocumentLoader; |
| 15 | +import io.openapiprocessor.jsonschema.schema.DocumentStore; |
| 16 | +import io.openapiprocessor.jsonschema.schema.SchemaStore; |
| 17 | +import io.openapiprocessor.jsonschema.validator.Validator; |
| 18 | +import io.openapiprocessor.jsonschema.validator.ValidatorSettings; |
| 19 | +import io.openapiprocessor.snakeyaml.SnakeYamlConverter; |
| 20 | +import org.checkerframework.checker.nullness.qual.Nullable; |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | + |
| 23 | +import java.io.FileWriter; |
| 24 | +import java.io.IOException; |
| 25 | +import java.net.URI; |
| 26 | +import java.util.Collection; |
| 27 | +import java.util.Map; |
| 28 | + |
| 29 | +import static io.openapiprocessor.jsonschema.support.Null.nonNull; |
| 30 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 31 | + |
| 32 | +public class ParserSampleTest { |
| 33 | + |
| 34 | + /** |
| 35 | + * create a document loader. It loads a document by uri and converts it to a {@code Map<String, Object>} |
| 36 | + * object tree that represents the OpenAPI document. The parser operates on that Object tree which makes |
| 37 | + * it independent of the object mapper (i.e. Jackson or SnakeYAML). Both (Reader and Converter) have a |
| 38 | + * very simple interface which makes it easy to implement your own. |
| 39 | + * |
| 40 | + * @return a document loader |
| 41 | + */ |
| 42 | + DocumentLoader createLoader() { |
| 43 | + Reader reader = new UriReader(); |
| 44 | + Converter converter = new SnakeYamlConverter(); |
| 45 | + // Converter converter = new JacksonConverter (); |
| 46 | + return new DocumentLoader (reader, converter); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + void parse() { |
| 51 | + // create the OpenAPI parser. |
| 52 | + OpenApiParser parser = new OpenApiParser(new DocumentStore(), createLoader()); |
| 53 | + |
| 54 | + // parse the OpenAPI from resource or url. Here it loads the OpenAPI document from a resource file. |
| 55 | + OpenApiResult result = parser.parse ("/samples/openapi32.yaml"); |
| 56 | + |
| 57 | + // get the API model from the result to navigate the OpenAPI document. Each OpenAPI version has its |
| 58 | + // own model. |
| 59 | + // To work with multiple versions it is recommended to hide them behind a common interface that |
| 60 | + // provides the properties required by the application. The openapi-parser does currently not provide |
| 61 | + // such an interface. |
| 62 | + OpenApiVersion version = result.getVersion(); |
| 63 | + if (version == OpenApiVersion.V31) { |
| 64 | + io.openapiparser.model.v31.OpenApi model = result.getModel (io.openapiparser.model.v31.OpenApi.class); |
| 65 | + // .... |
| 66 | + |
| 67 | + } else if (version == OpenApiVersion.V32) { |
| 68 | + // get the version specific model |
| 69 | + io.openapiparser.model.v32.OpenApi model = result.getModel (io.openapiparser.model.v32.OpenApi.class); |
| 70 | + |
| 71 | + // navigate the model |
| 72 | + io.openapiparser.model.v32.Paths paths = model.getPaths (); |
| 73 | + if (paths == null) { |
| 74 | + throw new RuntimeException("missing paths"); |
| 75 | + } |
| 76 | + |
| 77 | + io.openapiparser.model.v32.PathItem pathItem = paths.getPathItem ("/foo"); |
| 78 | + if (pathItem == null) { |
| 79 | + throw new RuntimeException("missing path item"); |
| 80 | + } |
| 81 | + |
| 82 | + Map<String, io.openapiparser.model.v32.Operation> operations = pathItem.getOperations(); |
| 83 | + // ... |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + void validate() { |
| 89 | + DocumentLoader loader = createLoader(); |
| 90 | + |
| 91 | + // create the OpenAPI parser. |
| 92 | + OpenApiParser parser = new OpenApiParser(new DocumentStore(), loader); |
| 93 | + |
| 94 | + // create a JSON schema validator. |
| 95 | + SchemaStore store = new SchemaStore(loader); |
| 96 | + Validator validator = new Validator(new ValidatorSettings()); |
| 97 | + |
| 98 | + // parse the OpenAPI document. |
| 99 | + OpenApiResult result = parser.parse ("/samples/openapi32.yaml"); |
| 100 | + |
| 101 | + |
| 102 | + // validate the OpenAPI schema. |
| 103 | + boolean valid = result.validate (validator, store); |
| 104 | + |
| 105 | + // print validation errors. |
| 106 | + Collection<ValidationError> errors = result.getValidationErrors(); |
| 107 | + ValidationErrorTextBuilder builder = new ValidationErrorTextBuilder(); |
| 108 | + |
| 109 | + for (ValidationError error : errors) { |
| 110 | + System.out.println(builder.getText(error)); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + void bundle() throws IOException { |
| 116 | + // create the OpenAPI parser. |
| 117 | + OpenApiParser parser = new OpenApiParser(new DocumentStore(), createLoader()); |
| 118 | + |
| 119 | + // parse the OpenAPI document. |
| 120 | + OpenApiResult result = parser.parse("/samples/openapi32.yaml"); |
| 121 | + |
| 122 | + // bundle it, i.e. inline external $refs. |
| 123 | + Map<String, @Nullable Object> bundled = result.bundle(); |
| 124 | + |
| 125 | + // save the bundled document. |
| 126 | + Writer writer = new JacksonJsonWriter(new FileWriter("./bundle32.yaml")); |
| 127 | + writer.write(bundled); |
| 128 | + |
| 129 | + // to navigate the bundle it has to be parsed. The bundled object tree has no uri (which is |
| 130 | + // used to cache the document) so we provide one. |
| 131 | + OpenApiResult resultBundled = parser.parse(URI.create("/samples/bundled32.yaml"), bundled); |
| 132 | + |
| 133 | + // get the version specific model. |
| 134 | + io.openapiparser.model.v32.OpenApi model = resultBundled.getModel (io.openapiparser.model.v32.OpenApi.class); |
| 135 | + // ... |
| 136 | + } |
| 137 | + |
| 138 | + @Test |
| 139 | + void overlay() { |
| 140 | + DocumentLoader loader = createLoader(); |
| 141 | + DocumentStore documents = new DocumentStore(); |
| 142 | + |
| 143 | + // create the OpenAPI parser. |
| 144 | + OpenApiParser openApiParser = new OpenApiParser(documents, loader); |
| 145 | + |
| 146 | + // create the Overlay parser. |
| 147 | + OverlayParser overlayParser = new OverlayParser(documents, loader); |
| 148 | + |
| 149 | + // create JSON schema validator. |
| 150 | + SchemaStore store = new SchemaStore(loader); |
| 151 | + Validator validator = new Validator(new ValidatorSettings()); |
| 152 | + |
| 153 | + // parse & validate the OpenAPI document. |
| 154 | + OpenApiResult result = openApiParser.parse("/samples/openapi32.yaml"); |
| 155 | + boolean valid = result.validate(validator, store); |
| 156 | + |
| 157 | + // bundle it, i.e. inline external $refs. |
| 158 | + Map<String, @Nullable Object> bundled = result.bundle(); |
| 159 | + |
| 160 | + // to navigate the bundle it has to be parsed. The bundled object tree has no uri (which is |
| 161 | + // used to cache the document) so we provide one. |
| 162 | + OpenApiResult resultBundled = openApiParser.parse(URI.create("/samples/bundled32.yaml"), bundled); |
| 163 | + |
| 164 | + // parse & validate the Overlay document. |
| 165 | + OverlayResult resultOverlay = overlayParser.parse("/samples/overlay32.yaml"); |
| 166 | + boolean validOverlay = resultOverlay.validate(validator, store); |
| 167 | + |
| 168 | + // apply Overlay. |
| 169 | + Map<String, @Nullable Object> appliedOverlay = resultBundled.apply(resultOverlay); |
| 170 | + |
| 171 | + // to navigate the overlay'ed document it has to be parsed. The object tree has no uri (which is |
| 172 | + // used to cache the document) so we provide one. |
| 173 | + OpenApiResult resultApplied = openApiParser.parse(URI.create("/samples/overlayed32.yaml"), appliedOverlay); |
| 174 | + |
| 175 | + // get the version specific model and check the modification |
| 176 | + io.openapiparser.model.v32.OpenApi model = resultApplied.getModel (io.openapiparser.model.v32.OpenApi.class); |
| 177 | + io.openapiparser.model.v32.Components components = nonNull(model.getComponents()); |
| 178 | + |
| 179 | + Map<String, io.openapiparser.model.v32.Schema> schemas = components.getSchemas(); |
| 180 | + schemas.keySet().forEach(key -> { |
| 181 | + io.openapiparser.model.v32.Schema bar = nonNull(schemas.get(key).getProperties().get("bar")); |
| 182 | + String description = bar.getDescription(); |
| 183 | + assertEquals("added by overlay!", description); |
| 184 | + }); |
| 185 | + } |
| 186 | +} |
0 commit comments