use of com.github.victools.jsonschema.generator.SchemaGeneratorConfigBuilder in project jsonschema-generator by victools.
the class SubtypeResolutionIntegrationTest method testIntegration.
@Test
public void testIntegration() throws Exception {
JacksonModule module = new JacksonModule(JacksonOption.FLATTENED_ENUMS_FROM_JSONVALUE);
SchemaGeneratorConfig config = new SchemaGeneratorConfigBuilder(SchemaVersion.DRAFT_2019_09, OptionPreset.PLAIN_JSON).with(Option.DEFINITIONS_FOR_ALL_OBJECTS, Option.NULLABLE_FIELDS_BY_DEFAULT).with(module).build();
SchemaGenerator generator = new SchemaGenerator(config);
JsonNode result = generator.generateSchema(TestClassForSubtypeResolution.class);
String rawJsonSchema = result.toString();
JSONAssert.assertEquals('\n' + rawJsonSchema + '\n', loadResource("subtype-integration-test-result.json"), rawJsonSchema, JSONCompareMode.STRICT);
JsonSchema schemaForValidation = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V201909).getSchema(result);
String jsonInstance = config.getObjectMapper().writeValueAsString(new TestClassForSubtypeResolution());
Set<ValidationMessage> validationResult = schemaForValidation.validate(config.getObjectMapper().readTree(jsonInstance));
if (!validationResult.isEmpty()) {
Assert.fail("\n" + jsonInstance + "\n " + validationResult.stream().map(ValidationMessage::getMessage).collect(Collectors.joining("\n ")));
}
}
use of com.github.victools.jsonschema.generator.SchemaGeneratorConfigBuilder in project jsonschema-generator by victools.
the class SchemaGeneratorMojo method getGenerator.
/**
* Get the JSON Schema generator. Create it when required.
* <br>
* Configuring it the specified options and adding the required modules.
*
* @return The configured generator
* @throws MojoExecutionException Error exception
*/
private SchemaGenerator getGenerator() throws MojoExecutionException {
if (this.generator == null) {
this.getLog().debug("Initializing Schema Generator");
// Start with the generator builder
SchemaGeneratorConfigBuilder configBuilder = new SchemaGeneratorConfigBuilder(this.schemaVersion, this.getOptionPreset());
// Add options when required
this.setOptions(configBuilder);
// Register the modules when specified
this.setModules(configBuilder);
// And construct the generator
SchemaGeneratorConfig config = configBuilder.build();
this.generator = new SchemaGenerator(config);
}
return this.generator;
}
use of com.github.victools.jsonschema.generator.SchemaGeneratorConfigBuilder in project jsonschema-generator by victools.
the class IntegrationTest method testIntegration.
/**
* Test
*
* @throws Exception
*/
@Test
public void testIntegration() throws Exception {
// active all optional modules
JakartaValidationModule module = new JakartaValidationModule(JakartaValidationOption.NOT_NULLABLE_FIELD_IS_REQUIRED, JakartaValidationOption.NOT_NULLABLE_METHOD_IS_REQUIRED, JakartaValidationOption.INCLUDE_PATTERN_EXPRESSIONS);
SchemaGeneratorConfig config = new SchemaGeneratorConfigBuilder(SchemaVersion.DRAFT_2019_09, OptionPreset.PLAIN_JSON).with(Option.NULLABLE_ARRAY_ITEMS_ALLOWED).with(module).build();
SchemaGenerator generator = new SchemaGenerator(config);
JsonNode result = generator.generateSchema(TestClass.class);
String rawJsonSchema = result.toString();
JSONAssert.assertEquals('\n' + rawJsonSchema + '\n', loadResource("integration-test-result.json"), rawJsonSchema, JSONCompareMode.STRICT);
}
use of com.github.victools.jsonschema.generator.SchemaGeneratorConfigBuilder in project jsonschema-generator by victools.
the class IntegrationTest method testIntegration.
/**
* Test
*
* @throws Exception
*/
@Test
public void testIntegration() throws Exception {
// active all optional modules
JavaxValidationModule module = new JavaxValidationModule(JavaxValidationOption.NOT_NULLABLE_FIELD_IS_REQUIRED, JavaxValidationOption.NOT_NULLABLE_METHOD_IS_REQUIRED, JavaxValidationOption.INCLUDE_PATTERN_EXPRESSIONS);
SchemaGeneratorConfig config = new SchemaGeneratorConfigBuilder(SchemaVersion.DRAFT_2019_09, OptionPreset.PLAIN_JSON).with(Option.NULLABLE_ARRAY_ITEMS_ALLOWED).with(module).build();
SchemaGenerator generator = new SchemaGenerator(config);
JsonNode result = generator.generateSchema(TestClass.class);
String rawJsonSchema = result.toString();
JSONAssert.assertEquals('\n' + rawJsonSchema + '\n', loadResource("integration-test-result.json"), rawJsonSchema, JSONCompareMode.STRICT);
}
use of com.github.victools.jsonschema.generator.SchemaGeneratorConfigBuilder in project kogito-runtimes by kiegroup.
the class JsonSchemaGenerator method generate.
public Collection<GeneratedFile> generate() throws IOException {
SchemaGeneratorConfigBuilder builder = new SchemaGeneratorConfigBuilder(schemaVersion, OptionPreset.PLAIN_JSON);
builder.with(Option.DEFINITIONS_FOR_ALL_OBJECTS);
builder.forTypesInGeneral().withStringFormatResolver(target -> target.getSimpleTypeDescription().equals("Date") ? "date-time" : null);
builder.forFields().withIgnoreCheck(JsonSchemaGenerator::checkFields).withCustomDefinitionProvider(this::getInputOutput);
SchemaGenerator generator = new SchemaGenerator(builder.build());
ObjectWriter writer = new ObjectMapper().writer();
Collection<GeneratedFile> files = new ArrayList<>();
for (Map.Entry<String, List<Class<?>>> entry : map.entrySet()) {
ObjectNode merged = null;
for (Class<?> c : entry.getValue()) {
ObjectNode read = generator.generateSchema(c);
if (merged == null) {
merged = read;
} else {
JsonUtils.merge(read, merged);
}
}
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
writer.writeValue(outputStream, merged);
files.add(new GeneratedFile(JSON_SCHEMA_TYPE, pathFor(entry.getKey()), outputStream.toByteArray()));
}
}
return files;
}
Aggregations