This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose.
This Java package is automatically generated by the OpenAPI JSON Schema Generator project:
- OpenAPI document version: 1.0.0
- Java Package version: 1.0.0
- OpenAPI JSON Schema Generator, Generator: JavaClientGenerator
- Java 17
- Maven
To install the API client library to your local Maven repository, simply execute:
mvn clean install
To deploy it to a remote Maven repository instead, configure the settings of the repository and execute:
mvn clean deploy
Refer to the OSSRH Guide for more information.
Add this dependency to your project's POM:
<dependency>
<groupId>org.openapijsonschematools</groupId>
<artifactId>petstore</artifactId>
<version>1.0.0</version>
<scope>compile</scope>
</dependency>
At first generate the JAR by executing:
mvn clean package
Then manually install the following JARs:
target/petstore-1.0.0.jar
target/lib/*.jar
This Java code validates data to schema classes and return back an immutable instance containing the data. This ensure that
- valid data cannot be mutated and become invalid to a set of schemas
- the one exception is that files are not immutable, so schema instances storing/sending/receiving files are not immutable
Here is the mapping from json schema types to Java types:
Json Schema Type | Java Base Class |
---|---|
object | FrozenMap (Map) |
array | FrozenList (List) |
string | String |
number | Number (int, long, float, double) |
integer | int, long, float, double (with values equal to integers) |
boolean | boolean |
null | Void (null) |
AnyType (unset) | @Nullable Object |
In openapi v3.0.3 there are ~ 28 json schema keywords. Almost all of them can apply if type is unset. I have chosen to separate the storage of
- json schema definition info
- output classes for validated Map (json schema type object) payloads
- output classes for validated List (json schema type array) payloads
Reason
This json schema data is stored in each class that is written for a schema, in a component or other openapi document location. This class is only responsible for storing schema info. Output classes like those that store map payloads are written separately and are returned by the JsonSchema.validate method when that method is passed in Map input. This prevents payload property access methods from colliding with json schema definition.
Most component schemas (models) are probably of type object. Which is a map data structure. Json schema allows string keys in this map, which means schema properties can have key names that are invalid Java variable names. Names like:
- "hi-there"
- "1variable"
- "@now"
- " "
- "from"
To allow these use cases to work, FrozenMap (which extends AbstractMap) is used as the base class of type object schemas. This means that one can use normal Map methods on instances of these classes.
Other Details
- getters are written for validly named required and optional properties
- null is only allowed in as a value if type: "null" was included or nullable: true was set
- because null is an allowed property value, it is not used to represent an unset property state
- if an optional property is requested and it does not exist in the Map, an UnsetPropertyException is thrown
N schemas can be validated on the same payload. To allow multiple schemas to validate, the data must be stored using one base class whether or not a json schema format constraint exists in the schema.
In json schema, type: number with no format validates both integers and floats, so int and float values are stored for type number.
String + Date Example
For example the string payload '2023-12-20' is validates to both of these schemas:
- string only
- type: string
- string and date format
- type: string
format: date
Because of use cases like this, a LocalDate is allowed as an input to this schema, but the data is stored as a string.
Please follow the installation procedure and then use the JsonSchema classes in org.openapijsonschematools.client.components.schemas to validate input payloads and instances of validated Map and List output classes. Json schemas allow multiple types for one schema, so a schema's validate method can have allowed input and output types.
import org.openapijsonschematools.client.configurations.ApiConfiguration;
import org.openapijsonschematools.client.configurations.SchemaConfiguration;
import org.openapijsonschematools.client.configurations.JsonSchemaKeywordFlags;
import org.openapijsonschematools.client.exceptions.ValidationException;
import org.openapijsonschematools.client.exceptions.NotImplementedException;
import org.openapijsonschematools.client.exceptions.ApiException;
import org.openapijsonschematools.client.schemas.validation.MapUtils;
import org.openapijsonschematools.client.schemas.validation.FrozenList;
import org.openapijsonschematools.client.schemas.validation.FrozenMap;
import org.openapijsonschematools.client.RootServerInfo;
import org.openapijsonschematools.client.servers.RootServer0;
import org.openapijsonschematools.client.servers.RootServer1;
import org.openapijsonschematools.client.servers.RootServer2;
import org.openapijsonschematools.client.paths.solidus.get.responses.SolidusGetCode200Response;
import org.openapijsonschematools.client.servers.RootServerInfo;
import org.openapijsonschematools.client.apis.tags.Fake;
import org.openapijsonschematools.client.paths.solidus.get.SolidusGetResponses;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.AbstractMap;
// if you want to use a server that is not SERVER_0 pass it in here and change the ServerIndex input below
ApiConfiguration.ServerInfo serverInfo = new ApiConfiguration.ServerInfoBuilder()
.rootServerInfo(
new RootServerInfo.RootServerInfoBuilder()
.rootServer0(new RootServer0())
.build()
)
.build();
ApiConfiguration.ServerIndexInfo serverIndexInfo = new ApiConfiguration.ServerIndexInfoBuilder()
.rootServerInfoServerIndex(RootServerInfo.ServerIndex.SERVER_0)
.build();
Duration timeout = Duration.ofSeconds(1L);
Map<String, List<String>> defaultHeaders = Map.of("User-Agent", List.of("OpenAPI-JSON-Schema-Generator/1.0.0/java"));
ApiConfiguration apiConfiguration = new ApiConfiguration(
serverInfo
serverIndexInfo,
timeout,
defaultHeaders
);
SchemaConfiguration schemaConfiguration = new SchemaConfiguration(new JsonSchemaKeywordFlags.Builder().build());
Fake apiClient = new Fake(apiConfiguration, schemaConfiguration);
var request = new SolidusGet.GetRequestBuilder().build();
SolidusGetResponses.EndpointResponse response;
try {
response = apiClient.get(request);
} catch (ApiException e) {
// server returned a response/contentType not defined in the openapi document
throw e;
} catch (ValidationException e) {
// the returned response body or header values do not conform the the schema validation requirements
throw e;
} catch (IOException | InterruptedException e) {
// an exception happened when making the request
throw e;
} catch (NotImplementedException e) {
// the request body serialization or deserialization has not yet been implemented
// or the header content type deserialization has not yet been implemented for this contentType
throw e;
}
SolidusGetResponses.EndpointSolidusGetCode200Response castResponse = (SolidusGetResponses.EndpointSolidusGetCode200Response) response;
server_index | Class | Description |
---|---|---|
0 | RootServer0 | petstore server |
1 | RootServer1 | The local server |
2 | RootServer2 | staging server with no variables |
Class | Description |
---|---|
ApiKey | apiKey in header |
ApiKeyQuery | apiKey in query |
BearerTest | http bearer with JWT bearer format |
HttpBasicTest | http basic |
HttpSignatureTest | http + signature |
OpenIdConnectTest | openIdConnect |
PetstoreAuth | oauth2 implicit flow with two scopes |
All URIs are relative to the selected server
- The server is selected by passing in serverInfo + serverIndexInfo into configurations.ApiConfiguration
- The security info is selected by passing in securityInfo + securityIndexInfo into configurations.ApiConfiguration
- serverIndex + securityIndex can also be passed in to endpoint calls, see endpoint documentation
Class | Description |
---|---|
Client.Client1 | client model |
Pet.Pet1 | Pet object that needs to be added to the store, multiple content types |
RefUserArray.RefUserArray1 | |
UserArray.UserArray1 | List of user object |
Class | Description |
---|---|
HeadersWithNoBody.HeadersWithNoBody1 | A response that contains headers but no body |
RefSuccessDescriptionOnly.RefSuccessDescriptionOnly1 | |
RefSuccessfulXmlAndJsonArrayOfPet.RefSuccessfulXmlAndJsonArrayOfPet1 | |
SuccessDescriptionOnly.SuccessDescriptionOnly1 | Success |
SuccessInlineContentAndHeader.SuccessInlineContentAndHeader1 | successful operation |
SuccessWithJsonApiResponse.SuccessWithJsonApiResponse1 | successful operation |
SuccessfulXmlAndJsonArrayOfPet.SuccessfulXmlAndJsonArrayOfPet1 | successful operation, multiple content types |
Class | Description |
---|---|
Int32JsonContentTypeHeader.Int32JsonContentTypeHeader1 | int32 JSON content-type header |
NumberHeader.NumberHeader1 | number header description |
RefContentSchemaHeader.RefContentSchemaHeader1 | int32 JSON content-type header |
RefSchemaHeader.RefSchemaHeader1 | header that has a ref in the schema |
RefStringHeader.RefStringHeader1 | |
StringHeader.StringHeader1 | string header description |
Class | Description |
---|---|
ComponentRefSchemaStringWithValidation.ComponentRefSchemaStringWithValidation1 | a path string with validation |
PathUserName.PathUserName1 | the use name to use |
RefPathUserName.RefPathUserName1 | |
RefSchemaStringWithValidation.RefSchemaStringWithValidation1 | a path string with validation |