Generate JSON schemas
The json-schema generator writes one JSON Schema Draft 7 file per named
schema of the contract, for example to validate payloads outside the JVM or
to register them in a schema registry. It generates no model source.
Configure the execution
Maven:
<configuration>
<generatorName>json-schema</generatorName>
<inputSpec>${project.basedir}/src/main/resources/asyncapi.yaml</inputSpec>
<schemaPackage>com.example.accounts.json</schemaPackage>
</configuration>
Gradle:
asyncApiGenerator {
executions {
register("json") {
generatorName.set("json-schema")
inputSpec.set(file("src/main/resources/asyncapi.yaml"))
schemaPackage.set("com.example.accounts.json")
}
}
}
CLI:
asyncapi-gen \
--input-spec src/main/resources/asyncapi.yaml \
--generator-name json-schema \
--schema-package com.example.accounts.json
schemaPackage is required and selects the output directory, for example
com/example/accounts/json/AccountEvent.schema.json. Maven and Gradle add the
output directory to the project resources.
What is generated
- Each AsyncAPI Schema Object under
components.schemas, and each native JSON Schema payload (schemaFormatapplication/schema+json;version=draft-07orapplication/schema+yaml;version=draft-07), becomes one<Name>.schema.jsonfile. - A reference to another component schema becomes a relative reference to
that schema's file, such as
"$ref": "AccountStatus.schema.json", so keep the generated files together. - Two sources that would produce the same file name, such as a Schema Object and a native JSON Schema with the same name, fail generation.
The generator does not promise coverage of every JSON Schema keyword. A
keyword that cannot be represented fails generation instead of being dropped.
The complete approved output for a sample contract is protected by
JsonSchemaApprovalTest.