Module for the jsonschema-generator – deriving JSON Schema attributes from swagger
(2.x) annotations
- From
@Schema(description = …)
on types in general, derive"description"
. - From
@Schema(title = …)
on types in general, derive"title"
. - From
@Schema(subTypes = …)
on types in general, derive"anyOf"
alternatives. - From
@Schema(anyOf = …)
on types in general (as alternative tosubTypes
), derive"anyOf"
alternatives. - From
@Schema(name = …)
on types in general, derive the keys/names in"definitions"
/"$defs"
. - From
@Schema(description = …)
on fields/methods, derive"description"
. - From
@Schema(title = …)
on fields/methods, derive"title"
. - From
@Schema(implementation = …)
on fields/methods, override represented type. - From
@Schema(hidden = true)
on fields/methods, skip certain properties. - From
@Schema(name = …)
on fields/methods, override property names. - From
@Schema(ref = …)
on fields/methods, replace subschema with"$ref"
to external/separate schema. - From
@Schema(allOf = …)
on fields/methods, include"allOf"
parts. - From
@Schema(not = …)
on fields/methods, include the indicated"not"
subschema. - From
@Schema(required = true)
on fields/methods, mark property as"required"
in the schema containing the property. - From
@Schema(requiredProperties = …)
on fields/methods, derive its"required"
properties. - From
@Schema(minProperties = …)
on fields/methods, derive its"minProperties"
. - From
@Schema(maxProperties = …)
on fields/methods, derive its"maxProperties"
. - From
@Schema(nullable = true)
on fields/methods, includenull
in its"type"
. - From
@Schema(allowableValues = …)
on fields/methods, derive its"const"
/"enum"
. - From
@Schema(defaultValue = …)
on fields/methods, derive its"default"
. - From
@Schema(accessMode = AccessMode.READ_ONLY)
on fields/methods, to mark them as"readOnly"
. - From
@Schema(accessMode = AccessMode.WRITE_ONLY)
on fields/methods, to mark them as"writeOnly"
. - From
@Schema(minLength = …)
on fields/methods, derive its"minLength"
. - From
@Schema(maxLength = …)
on fields/methods, derive its"maxLength"
. - From
@Schema(format = …)
on fields/methods, derive its"format"
. - From
@Schema(pattern = …)
on fields/methods, derive its"pattern"
. - From
@Schema(multipleOf = …)
on fields/methods, derive its"multipleOf"
. - From
@Schema(minimum = …, exclusiveMinimum = …)
on fields/methods, derive its"minimum"
/"exclusiveMinimum"
. - From
@Schema(maximum = …, exclusiveMaximum = …)
on fields/methods, derive its"maximum"
/"exclusiveMaximum"
. - From
@ArraySchema(minItems = …)
on fields/methods, derive its"minItems"
. - From
@ArraySchema(maxItems = …)
on fields/methods, derive its"maxItems"
. - From
@ArraySchema(uniqueItems = …)
on fields/methods, derive its"uniqueItems"
.
Schema attributes derived from @Schema
on fields are also applied to their respective getter methods.
Schema attributes derived from @Schema
on getter methods are also applied to their associated fields.
JavaDoc is being used throughout the codebase, offering contextual information in your respective IDE or being available online through services like javadoc.io.
Additional documentation can be found in the Project Wiki.
<dependency>
<groupId>com.github.victools</groupId>
<artifactId>jsonschema-module-swagger-2</artifactId>
<version>[4.21.0,5.0.0)</version>
</dependency>
The release versions of the main generator library and this module are aligned. It is recommended to use identical versions for both dependencies to ensure compatibility.
import com.github.victools.jsonschema.generator.SchemaGeneratorConfigBuilder;
import com.github.victools.jsonschema.generator.SchemaVersion;
import com.github.victools.jsonschema.module.swagger2.Swagger2Module;
Swagger2Module module = new Swagger2Module();
SchemaGeneratorConfigBuilder configBuilder = new SchemaGeneratorConfigBuilder(SchemaVersion.DRAFT_2019_09)
.with(module);
import com.fasterxml.jackson.databind.JsonNode;
import com.github.victools.jsonschema.generator.OptionPreset;
import com.github.victools.jsonschema.generator.SchemaGenerator;
import com.github.victools.jsonschema.generator.SchemaGeneratorConfig;
import com.github.victools.jsonschema.generator.SchemaGeneratorConfigBuilder;
import com.github.victools.jsonschema.generator.SchemaVersion;
import com.github.victools.jsonschema.module.swagger2.Swagger2Module;
Swagger2Module module = new Swagger2Module();
SchemaGeneratorConfigBuilder configBuilder = new SchemaGeneratorConfigBuilder(SchemaVersion.DRAFT_2019_09, OptionPreset.PLAIN_JSON)
.with(module);
SchemaGeneratorConfig config = configBuilder.build();
SchemaGenerator generator = new SchemaGenerator(config);
JsonNode jsonSchema = generator.generateSchema(YourClass.class);
System.out.println(jsonSchema.toString());