### OpenAPI Parsing Setup (2023.1 API) Source: https://github.com/openapi-processor/openapi-parser/blob/master/openapi-parser/README.adoc Partial example for setting up document loading and reference resolution using the 2023.1 API of the openapi-parser library. This snippet shows the initial steps for parsing and validating an OpenAPI document. This API version is marked as obsolete. ```Java import io.openapiparser.jackson.JacksonConverter; import io.openapiparser.model.v30.OpenApi; import io.openapiparser.reader.UriReader; import io.openapiparser.schema.*; import io.openapiparser.snakeyaml.SnakeYamlConverter; import io.openapiparser.validator.Validator; import io.openapiparser.validator.ValidatorSettings; import io.openapiparser.validator.result.*; public class Example { void parseAndValidate () { // setup resolver (handles documents and $refs) Reader reader = new UriReader (); DocumentStore documents = new DocumentStore (); Converter converter = new SnakeYamlConverter (); ``` -------------------------------- ### Java JSON Schema Validator Setup Example Source: https://github.com/openapi-processor/openapi-parser/blob/master/json-schema-validator/README.adoc This Java code snippet demonstrates how to set up the `json-schema-validator`. It shows the initial steps for creating a document loader using `UriReader`, which is responsible for loading documents by URI and converting them into a generic object tree for validation. This setup highlights the validator's independence from specific object mappers. ```java package io.openapiprocessor.jsonschema.example; import io.openapiprocessor.interfaces.Converter; import io.openapiprocessor.interfaces.ConverterException; import io.openapiprocessor.jackson.JacksonConverter; import io.openapiprocessor.jsonschema.ouput.OutputConverter; import io.openapiprocessor.jsonschema.ouput.OutputUnit; import io.openapiprocessor.jsonschema.reader.UriReader; import io.openapiprocessor.jsonschema.schema.*; import io.openapiprocessor.jsonschema.validator.Validator; import io.openapiprocessor.jsonschema.validator.ValidatorSettings; import io.openapiprocessor.jsonschema.validator.steps.ValidationStep; import org.junit.jupiter.api.Test; import java.net.URI; import static io.openapiprocessor.jsonschema.schema.UriSupport.createUri; public class SetupExampleTest { @Test void setupExample() throws ConverterException { // 1. create a document loader. // It loads a document by uri and converts it to a Map // object tree that represents the json or yaml content. The Validator // operates on that Object tree which makes it independent of the // object mapper (e.g. jackson, snakeyaml etc.). // Both (Reader and Converter) have a very simple interface which makes // it easy to implement your own. UriReader reader = new UriReader (); ``` -------------------------------- ### OpenAPI Parsing and Validation (Basic Setup) Source: https://github.com/openapi-processor/openapi-parser/blob/master/openapi-parser/README.adoc Demonstrates a basic setup for parsing an OpenAPI document, navigating its model, and performing schema validation. This snippet shows the core steps without explicit versioning of the parser setup. ```Java OpenApiParser parser = new OpenApiParser (documents, loader); // 3. parse the OpenAPI from resource or url. // here it loads an OpenAPI document from a resource file, but URI works too. OpenApiResult result = parser.parse ("openapi.yaml"); // 4. get the API model from the result to navigate the OpenAPI document. // OpenAPI 3.1.x with model.v31.OpenAPI import OpenApi model = result.getModel (OpenApi.class); // 5. navigate the model PathItem pathItem = model.getPaths ().getPathItem ("/foo"); // 6. create Validator to validate the OpenAPI schema. SchemaStore store = new SchemaStore (loader); ValidatorSettings settings = new ValidatorSettings (); Validator validator = new Validator (settings); // 7. validate the OpenAPI schema. boolean valid = result.validate (validator, store); // 8. print validation errors Collection errors = result.getValidationErrors (); ValidationErrorTextBuilder builder = new ValidationErrorTextBuilder (); for (ValidationError error : errors) { System.out.println (builder.getText(error)); } } } ``` -------------------------------- ### Clone Git Repository with Submodules Source: https://github.com/openapi-processor/openapi-parser/blob/master/json-schema-validator/src/test/resources/README.adoc Clones a Git repository and initializes its submodules. This command is used for the initial setup of a project that includes submodules, ensuring all dependencies are fetched. ```Git git clone --recurse-submodules ``` -------------------------------- ### Gradle: Check SLF4J API Dependency Insight Source: https://github.com/openapi-processor/openapi-parser/blob/master/DEV.adoc This command uses Gradle's `dependencyInsight` task to inspect the dependency tree for `org.slf4j:slf4j-api`. It requires Gradle to be installed and the project to be a Gradle project. The output provides detailed information about how the specified dependency is resolved within the `testRuntimeClasspath` configuration. ```Shell ../gradlew dependencyInsight --dependency org.slf4j:slf4j-api --configuration testRuntimeClasspath ``` -------------------------------- ### OpenAPI Document Parsing and JSON Schema Validation (Java 2023.3 API) Source: https://github.com/openapi-processor/openapi-parser/blob/master/openapi-parser/README.adoc This Java code example demonstrates how to parse an OpenAPI YAML file and perform JSON Schema validation using the 2023.3 API. It illustrates the setup of a `DocumentLoader` with `UriReader` and `SnakeYamlConverter` to process OpenAPI documents, highlighting the independence from specific object mappers. ```Java package io.openapiparser; import io.openapiparser.model.v30.OpenApi; import io.openapiparser.model.v30.PathItem; import io.openapiprocessor.interfaces.Converter; import io.openapiprocessor.interfaces.Reader; import io.openapiprocessor.jsonschema.reader.UriReader; import io.openapiprocessor.jsonschema.schema.*; import io.openapiprocessor.jsonschema.validator.Validator; import io.openapiprocessor.jsonschema.validator.ValidatorSettings; import io.openapiprocessor.snakeyaml.SnakeYamlConverter; import org.junit.jupiter.api.Test; import java.util.Collection; public class SetupExampleTest { //@Test void parseAndValidate () { // 1. create a document loader. // It loads a document by uri and converts it to a Map // object tree that represents the OpenAPI document. The parser // operates on that Object tree which makes it independent of the // object mapper (e.g. jackson, snakeyaml etc.). // Both (Reader and Converter) have a very simple interface which makes // it easy to implement your own. Reader reader = new UriReader (); Converter converter = new SnakeYamlConverter (); // Converter converter = new JacksonConverter (); DocumentLoader loader = new DocumentLoader (reader, converter); // 2. create a parser. DocumentStore documents = new DocumentStore (); ``` -------------------------------- ### OpenAPI Parsing and Validation (2023.2 API) Source: https://github.com/openapi-processor/openapi-parser/blob/master/openapi-parser/README.adoc Complete example for parsing and validating an OpenAPI document using the 2023.2 API of the openapi-parser library. It covers document loading, reference resolution, parsing, model navigation, and schema validation, including error reporting. This API version is marked as obsolete. ```Java package io.openapiparser; import io.openapiparser.model.v30.OpenApi; import io.openapiparser.model.v30.PathItem; import io.openapiprocessor.interfaces.Converter; import io.openapiprocessor.interfaces.Reader; import io.openapiprocessor.jsonschema.reader.UriReader; import io.openapiprocessor.jsonschema.schema.*; import io.openapiprocessor.jsonschema.validator.Validator; import io.openapiprocessor.jsonschema.validator.ValidatorSettings; import io.openapiprocessor.snakeyaml.SnakeYamlConverter; import org.junit.jupiter.api.Test; import java.util.Collection; public class SetupExampleTest { @Test void parseAndValidate () { // 1. create a document loader. // It loads a document by uri and converts it to a Map // object tree that represents the OpenAPI document. The parser // operates on that Object tree which makes it independent of the // object mapper (e.g. jackson, snakeyaml etc.). // Both (Reader and Converter) have a very simple interface which makes // it easy to implement your own. Reader reader = new UriReader (); Converter converter = new SnakeYamlConverter (); // Converter converter = new JacksonConverter (); DocumentLoader loader = new DocumentLoader (reader, converter); // 2. create a resolver. // it is responsible for resolving the $ref'erences in the OpenAPI document. // The Settings object is initialized with the JSON schema version used by // OpenAPI (here Draft 4 for OpenAPI 3.0.x). DocumentStore documents = new DocumentStore (); Resolver.Settings resolverSettings = new Resolver.Settings (SchemaVersion.Draft4); Resolver resolver = new Resolver (documents, loader, resolverSettings); // 3. parse the OpenAPI from resource or url. // here it loads an OpenAPI document from a resource file, but URI works too. OpenApiParser parser = new OpenApiParser (resolver); OpenApiResult result = parser.parse ("openapi.yaml"); // 4. get the API model from the result to navigate the OpenAPI document. // OpenAPI 3.1.x with model.v31.OpenAPI import OpenApi model = result.getModel (OpenApi.class); // 5. navigate the model PathItem pathItem = model.getPaths ().getPathItem ("/foo"); // 6. create Validator to validate the OpenAPI schema. SchemaStore store = new SchemaStore (loader); ValidatorSettings settings = new ValidatorSettings (); Validator validator = new Validator (settings); // 7. validate the OpenAPI schema. boolean valid = result.validate (validator, store); // 8. print validation errors Collection errors = result.getValidationErrors (); ValidationErrorTextBuilder builder = new ValidationErrorTextBuilder (); for (ValidationError error : errors) { System.out.println (builder.getText(error)); } } } ``` -------------------------------- ### Bundle OpenAPI Specification with Redocly CLI Source: https://github.com/openapi-processor/openapi-parser/blob/master/openapi-parser/src/test/resources/README.adoc This command uses the Redocly CLI to bundle an OpenAPI specification file (openapi.yaml) into a single, bundled YAML file (openapi_bundled.yaml). This is useful for consolidating multiple OpenAPI definition files into one for deployment or distribution. ```Shell npx @redocly/cli bundle openapi.yaml > openapi_bundled.yaml ``` -------------------------------- ### OpenAPI Parser User-Friendly API Design: Info Retrieval Source: https://github.com/openapi-processor/openapi-parser/blob/master/openapi-parser/README.adoc This section discusses the design philosophy of the OpenAPI parser's user-friendly API, specifically focusing on how 'Info' objects are retrieved. It contrasts the default `getInfo()` method with potential 'raw' alternatives, `Optional` returns, and `@Nullable` annotations to handle property presence. ```APIDOC model.v30.OpenApi (or model.v31.OpenApi) getInfo(): Info Alternative raw API: getInfoRaw(): Info Alternative with Optional: getInfo(): Optional Alternative with Nullable: getInfo(): @Nullable Info ``` -------------------------------- ### Validate JSON Instance Against Schema in Java Source: https://github.com/openapi-processor/openapi-parser/blob/master/json-schema-validator/README.adoc This Java code snippet demonstrates how to use the `openapi-parser` library to validate a JSON instance against a JSON schema. It covers initializing the document loader and converter, registering a schema from a URI, creating a JSON instance from a string, configuring the validator, executing the validation, and iterating through the validation results to print any errors. ```Java Converter converter = new JacksonConverter (); DocumentLoader loader = new DocumentLoader (reader, converter); // 2. create a json schema store, register a schema and get the schema. // the store creates a JsonSchema object from the schema document. A // JsonSchema object is a required parameter of the Validator. // There are several register() methods and convenience functions to // register json schema draft versions (e.g. 2029-09 etc.). // Here the store will download the schema from the given uri. URI schemaUri = createUri ("https://openapiprocessor.io/schemas/mapping/mapping-v3.json"); SchemaStore store = new SchemaStore (loader); store.register(schemaUri); // get the json schema object JsonSchema schema = store.getSchema (schemaUri); // 3. create an instance. An instance is the document we want to validate // with the schema. Like the schema above it is a Map // object tree. DocumentLoader and converter can be used to create the // Map object tree. // Here we simply create it from a string using the Converter. JsonInstance instance = new JsonInstance (converter.convert ( "## simple mapping file\n" + "\n" + "openapi-processor-mapping: v3\n" + "options:\n" + " package-name: io.openapiprocessor.generated\n" + " bean-validation: jakarta\n" + " javadoc: true\n" + " model-name-suffix: Resource\n" + " bad: property" )); // 4. create the validator. The ValidatorSettings are used to enable or // disable specific formats or set a fallback schema draft version for // schemas that do not provide a meta schema. ValidatorSettings settings = new ValidatorSettings (); Validator validator = new Validator(settings); // 5. run validation. The result ValidationStep is an implementation // specific result. It contains all validation details and can be easily // converted to the official json schema output format. ValidationStep step = validator.validate(schema, instance); // boolean valid = step.isValid (); // 6. convert to official output format. It supports the formats, flag, // basic and verbose. OutputConverter output = new OutputConverter(Output.BASIC); OutputUnit result = output.convert(step); boolean valid = result.isValid (); // 7. print errors with error location if (!valid && result.getErrors () != null) { System.out.println ("validation failed!"); for (OutputUnit error : result.getErrors ()) { String schemaLocation = error.getAbsoluteKeywordLocation (); schemaLocation = schemaLocation.substring (schemaLocation.indexOf ('#')); String msg = String.format ("%s at instance %s (schema %s)", error.getError (), error.getInstanceLocation (), schemaLocation ); System.out.println (msg); } } } } ``` -------------------------------- ### Add Git Submodule Source: https://github.com/openapi-processor/openapi-parser/blob/master/json-schema-validator/src/test/resources/README.adoc Adds a new Git submodule to the current repository. This command registers the submodule's URL and path, making it part of the parent repository's version control. ```Git git submodule add https://github.com/json-schema-org/JSON-Schema-Test-Suite openapi-parser-validator/src/test/resources/suites/JSON-Schema-Test-Suite ``` -------------------------------- ### Clone and Update Git Submodules to Remote Source: https://github.com/openapi-processor/openapi-parser/blob/master/json-schema-validator/src/test/resources/README.adoc Clones a Git repository and initializes its submodules, then immediately updates them to their latest remote versions. Useful for ensuring the submodule is up-to-date upon initial clone. ```Git git clone --recurse-submodules --remote-submodules ``` -------------------------------- ### Parse and Validate OpenAPI Specification in Java Source: https://github.com/openapi-processor/openapi-parser/blob/master/openapi-parser/README.adoc This Java snippet illustrates the process of parsing an 'openapi.yaml' file using OpenApiParser, obtaining the OpenAPI model, validating it with a SchemaStore and Validator, and then collecting and printing any validation messages (errors or warnings) to the console. ```Java // Converter converter = new JacksonConverter (); Resolver resolver = new Resolver (reader, converter, documents); // parser OpenAPI file or url OpenApiParser parser = new OpenApiParser (resolver); OpenApiResult result = parser.parse ("openapi.yaml"); // OpenAPI 3.1.x with model.v31.OpenAPI import OpenApi model = result.getModel (OpenApi.class); // validate OpenAPI SchemaStore store = new SchemaStore (resolver); ValidatorSettings settings = new ValidatorSettings (); Validator validator = new Validator (settings); boolean valid = result.validate (validator, store); // print validation messages (i.e. errors) MessageCollector collector = new MessageCollector (result.getValidationMessages ()); LinkedList messages = collector.collect (); MessageTextBuilder builder = new MessageTextBuilder (); for (Message message : messages) { System.out.println (builder.getText(message)); } ``` -------------------------------- ### Move Git Submodule Source: https://github.com/openapi-processor/openapi-parser/blob/master/json-schema-validator/src/test/resources/README.adoc Moves a Git submodule from one path to another within the repository. This command updates the submodule's path in the .gitmodules file and the Git index. ```Git git mv old/submod new target directory ``` -------------------------------- ### Java: Basic JSON Schema Instance Reference Loop Source: https://github.com/openapi-processor/openapi-parser/blob/master/json-schema-validator/src/main/java/io/openapiprocessor/jsonschema/validator/README.adoc A Java code snippet showing a basic `while` loop that checks if an instance is a reference. The full logic for resolving references, including tracking visited references to prevent infinite loops, is present in the original source as a commented-out block. ```Java while (instance.isRef()) { } ``` -------------------------------- ### Writer Interface for OpenAPI Documents Source: https://github.com/openapi-processor/openapi-parser/blob/master/io-interfaces/README.adoc Defines the `Writer` interface used to write OpenAPI JSON/YAML documents, accepting a Java `Object` tree. This interface allows for custom document writing implementations, enabling the output of processed OpenAPI documents to various destinations or formats. ```Java Writer interface: void write(Object document) throws IOException; Parameters: document: Object - The document object tree to write. Throws: IOException: If an I/O error occurs during writing. ``` -------------------------------- ### Update Git Submodule to Remote Source: https://github.com/openapi-processor/openapi-parser/blob/master/json-schema-validator/src/test/resources/README.adoc Updates a specific Git submodule to its latest commit from the remote repository. This command is used to pull changes from the submodule's upstream. ```Git git submodule update --remote ``` -------------------------------- ### Reader Interface for OpenAPI Documents Source: https://github.com/openapi-processor/openapi-parser/blob/master/io-interfaces/README.adoc Defines the `Reader` interface used to read OpenAPI JSON/YAML documents from a given URI, returning an `InputStream`. This interface is crucial for plugging in custom document reading mechanisms, allowing the parser to consume documents from various sources. ```Java Reader interface: InputStream read(URI uri) throws IOException; Parameters: uri: URI - The URI of the OpenAPI document to read. Returns: InputStream - An input stream containing the document content. Throws: IOException: If an I/O error occurs during reading. ``` -------------------------------- ### Converter Interface for JSON/YAML to Java Object Tree Source: https://github.com/openapi-processor/openapi-parser/blob/master/io-interfaces/README.adoc Defines the `Converter` interface for transforming JSON/YAML document strings into a Java `Object` tree, typically a `Map`. This interface is essential for parsing document content into a usable Java structure, handling potential conversion errors and supporting various JSON/YAML processing libraries. ```Java Converter interface: Object convert (String api) throws ConverterException; Parameters: api: String - The JSON/YAML document content as a string. Returns: Object - A Java Object tree representing the document. The root is typically a Map, but can be a single value if the document contains only one value. Throws: ConverterException: If an error occurs during the conversion process. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.