Search in sources :

Example 1 with SchemaGeneratorConfigBuilder

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  ")));
    }
}
Also used : SchemaGeneratorConfig(com.github.victools.jsonschema.generator.SchemaGeneratorConfig) ValidationMessage(com.networknt.schema.ValidationMessage) SchemaGeneratorConfigBuilder(com.github.victools.jsonschema.generator.SchemaGeneratorConfigBuilder) JsonSchema(com.networknt.schema.JsonSchema) SchemaGenerator(com.github.victools.jsonschema.generator.SchemaGenerator) JsonNode(com.fasterxml.jackson.databind.JsonNode) Test(org.junit.Test)

Example 2 with SchemaGeneratorConfigBuilder

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;
}
Also used : SchemaGeneratorConfig(com.github.victools.jsonschema.generator.SchemaGeneratorConfig) SchemaGeneratorConfigBuilder(com.github.victools.jsonschema.generator.SchemaGeneratorConfigBuilder) SchemaGenerator(com.github.victools.jsonschema.generator.SchemaGenerator)

Example 3 with SchemaGeneratorConfigBuilder

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);
}
Also used : SchemaGeneratorConfig(com.github.victools.jsonschema.generator.SchemaGeneratorConfig) SchemaGeneratorConfigBuilder(com.github.victools.jsonschema.generator.SchemaGeneratorConfigBuilder) SchemaGenerator(com.github.victools.jsonschema.generator.SchemaGenerator) JsonNode(com.fasterxml.jackson.databind.JsonNode) Test(org.junit.Test)

Example 4 with SchemaGeneratorConfigBuilder

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);
}
Also used : SchemaGeneratorConfig(com.github.victools.jsonschema.generator.SchemaGeneratorConfig) SchemaGeneratorConfigBuilder(com.github.victools.jsonschema.generator.SchemaGeneratorConfigBuilder) SchemaGenerator(com.github.victools.jsonschema.generator.SchemaGenerator) JsonNode(com.fasterxml.jackson.databind.JsonNode) Test(org.junit.Test)

Example 5 with SchemaGeneratorConfigBuilder

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;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) SchemaGenerator(com.github.victools.jsonschema.generator.SchemaGenerator) ArrayList(java.util.ArrayList) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) GeneratedFile(org.kie.kogito.codegen.api.GeneratedFile) SchemaGeneratorConfigBuilder(com.github.victools.jsonschema.generator.SchemaGeneratorConfigBuilder) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

SchemaGeneratorConfigBuilder (com.github.victools.jsonschema.generator.SchemaGeneratorConfigBuilder)15 SchemaGenerator (com.github.victools.jsonschema.generator.SchemaGenerator)14 JsonNode (com.fasterxml.jackson.databind.JsonNode)11 SchemaGeneratorConfig (com.github.victools.jsonschema.generator.SchemaGeneratorConfig)11 Test (org.junit.Test)9 OptionPreset (com.github.victools.jsonschema.generator.OptionPreset)4 Map (java.util.Map)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)3 SchemaVersion (com.github.victools.jsonschema.generator.SchemaVersion)3 List (java.util.List)3 ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)2 Option (com.github.victools.jsonschema.generator.Option)2 JacksonModule (com.github.victools.jsonschema.module.jackson.JacksonModule)2 JsonSchema (com.networknt.schema.JsonSchema)2 ValidationMessage (com.networknt.schema.ValidationMessage)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 ResolvedType (com.fasterxml.classmate.ResolvedType)1 SerializationFeature (com.fasterxml.jackson.databind.SerializationFeature)1 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1