use of com.github.victools.jsonschema.generator.SchemaGeneratorConfig in project jsonschema-generator by victools.
the class AttributeCollector method collectFieldAttributes.
/**
* Collect a field's contextual attributes (i.e. everything not related to the structure).
*
* @param field the field for which to collect JSON schema attributes
* @param generationContext generation context, including configuration to apply when looking-up attribute values
* @return node holding all collected attributes (possibly empty)
*/
public static ObjectNode collectFieldAttributes(FieldScope field, SchemaGenerationContext generationContext) {
SchemaGeneratorConfig config = generationContext.getGeneratorConfig();
ObjectNode node = config.createObjectNode();
AttributeCollector collector = new AttributeCollector(config.getObjectMapper());
collector.setTitle(node, config.resolveTitle(field), generationContext);
collector.setDescription(node, config.resolveDescription(field), generationContext);
collector.setDefault(node, config.resolveDefault(field), generationContext);
collector.setEnum(node, config.resolveEnum(field), generationContext);
collector.setReadOnly(node, config.isReadOnly(field), generationContext);
collector.setWriteOnly(node, config.isWriteOnly(field), generationContext);
collector.setAdditionalProperties(node, config.resolveAdditionalProperties(field), generationContext);
collector.setPatternProperties(node, config.resolvePatternProperties(field), generationContext);
collector.setStringMinLength(node, config.resolveStringMinLength(field), generationContext);
collector.setStringMaxLength(node, config.resolveStringMaxLength(field), generationContext);
collector.setStringFormat(node, config.resolveStringFormat(field), generationContext);
collector.setStringPattern(node, config.resolveStringPattern(field), generationContext);
collector.setNumberInclusiveMinimum(node, config.resolveNumberInclusiveMinimum(field), generationContext);
collector.setNumberExclusiveMinimum(node, config.resolveNumberExclusiveMinimum(field), generationContext);
collector.setNumberInclusiveMaximum(node, config.resolveNumberInclusiveMaximum(field), generationContext);
collector.setNumberExclusiveMaximum(node, config.resolveNumberExclusiveMaximum(field), generationContext);
collector.setNumberMultipleOf(node, config.resolveNumberMultipleOf(field), generationContext);
collector.setArrayMinItems(node, config.resolveArrayMinItems(field), generationContext);
collector.setArrayMaxItems(node, config.resolveArrayMaxItems(field), generationContext);
collector.setArrayUniqueItems(node, config.resolveArrayUniqueItems(field), generationContext);
config.getFieldAttributeOverrides().forEach(override -> override.overrideInstanceAttributes(node, field, generationContext));
return node;
}
use of com.github.victools.jsonschema.generator.SchemaGeneratorConfig in project jsonschema-generator by victools.
the class AbstractTypeAwareTest method prepareContextForVersion.
/**
* Override generation context mock methods that are version dependent.
*
* @param schemaVersion designated JSON Schema version
*/
protected void prepareContextForVersion(SchemaVersion schemaVersion) {
TypeContext typeContext = TypeContextFactory.createDefaultTypeContext();
ResolvedType resolvedTestClass = typeContext.resolve(this.testClass);
this.testClassMembers = typeContext.resolveWithMembers(resolvedTestClass);
this.context = Mockito.mock(SchemaGenerationContext.class, Mockito.RETURNS_DEEP_STUBS);
Mockito.when(this.context.getTypeContext()).thenReturn(typeContext);
SchemaGeneratorConfig config = Mockito.mock(SchemaGeneratorConfig.class);
Mockito.when(this.context.getGeneratorConfig()).thenReturn(config);
Mockito.when(config.resolveArrayMaxItems(Mockito.any(FieldScope.class))).thenReturn(null);
Mockito.when(config.resolveArrayMaxItems(Mockito.any(MethodScope.class))).thenReturn(null);
Mockito.when(config.resolveArrayMaxItemsForType(Mockito.any())).thenReturn(null);
Mockito.when(config.resolveArrayMinItems(Mockito.any(FieldScope.class))).thenReturn(null);
Mockito.when(config.resolveArrayMinItems(Mockito.any(MethodScope.class))).thenReturn(null);
Mockito.when(config.resolveArrayMinItemsForType(Mockito.any())).thenReturn(null);
Mockito.when(config.resolveArrayUniqueItems(Mockito.any(FieldScope.class))).thenReturn(null);
Mockito.when(config.resolveArrayUniqueItems(Mockito.any(MethodScope.class))).thenReturn(null);
Mockito.when(config.resolveArrayUniqueItemsForType(Mockito.any())).thenReturn(null);
Mockito.when(config.resolveStringMaxLength(Mockito.any(FieldScope.class))).thenReturn(null);
Mockito.when(config.resolveStringMaxLength(Mockito.any(MethodScope.class))).thenReturn(null);
Mockito.when(config.resolveStringMaxLengthForType(Mockito.any())).thenReturn(null);
Mockito.when(config.resolveStringMinLength(Mockito.any(FieldScope.class))).thenReturn(null);
Mockito.when(config.resolveStringMinLength(Mockito.any(MethodScope.class))).thenReturn(null);
Mockito.when(config.resolveStringMinLengthForType(Mockito.any())).thenReturn(null);
Mockito.when(config.getSchemaVersion()).thenReturn(schemaVersion);
Answer<String> keywordLookup = invocation -> ((SchemaKeyword) invocation.getArgument(0)).forVersion(schemaVersion);
Mockito.when(config.getKeyword(Mockito.any())).thenAnswer(keywordLookup);
Mockito.when(this.context.getKeyword(Mockito.any())).thenAnswer(keywordLookup);
ObjectMapper objectMapper = new ObjectMapper();
Mockito.when(config.getObjectMapper()).thenReturn(objectMapper);
Mockito.when(config.createArrayNode()).thenAnswer((_invocation) -> objectMapper.createArrayNode());
Mockito.when(config.createObjectNode()).thenAnswer((_invocation) -> objectMapper.createObjectNode());
Mockito.when(this.context.createStandardDefinitionReference(Mockito.any(ResolvedType.class), Mockito.any())).thenAnswer((_invocation) -> objectMapper.createObjectNode());
}
use of com.github.victools.jsonschema.generator.SchemaGeneratorConfig 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.SchemaGeneratorConfig in project jsonschema-generator by victools.
the class SchemaGenerationContextImplTest method setUp.
@Before
public void setUp() {
SchemaVersion schemaVersion = SchemaVersion.DRAFT_2019_09;
this.prepareContextForVersion(schemaVersion);
SchemaGeneratorConfig config = Mockito.mock(SchemaGeneratorConfig.class);
Mockito.when(config.getSchemaVersion()).thenReturn(schemaVersion);
Mockito.when(config.getKeyword(Mockito.any())).thenAnswer(invocation -> ((SchemaKeyword) invocation.getArgument(0)).forVersion(schemaVersion));
Mockito.when(config.resolveTitle(Mockito.any(FieldScope.class))).thenReturn("Field Title");
Mockito.when(config.resolveTitle(Mockito.any(MethodScope.class))).thenReturn("Method Title");
Mockito.when(config.resolveDescriptionForType(Mockito.any())).thenReturn("Type Description");
Mockito.when(config.resolveStringMinLength(Mockito.any(FieldScope.class))).thenReturn(null);
Mockito.when(config.resolveStringMaxLength(Mockito.any(FieldScope.class))).thenReturn(null);
Mockito.when(config.resolveArrayMinItems(Mockito.any(FieldScope.class))).thenReturn(null);
Mockito.when(config.resolveArrayMaxItems(Mockito.any(FieldScope.class))).thenReturn(null);
Mockito.when(config.resolveArrayUniqueItems(Mockito.any(FieldScope.class))).thenReturn(null);
Mockito.when(config.resolveStringMinLength(Mockito.any(MethodScope.class))).thenReturn(null);
Mockito.when(config.resolveStringMaxLength(Mockito.any(MethodScope.class))).thenReturn(null);
Mockito.when(config.resolveArrayMinItems(Mockito.any(MethodScope.class))).thenReturn(null);
Mockito.when(config.resolveArrayMaxItems(Mockito.any(MethodScope.class))).thenReturn(null);
Mockito.when(config.resolveArrayUniqueItems(Mockito.any(MethodScope.class))).thenReturn(null);
Mockito.when(config.resolveStringMinLengthForType(Mockito.any())).thenReturn(null);
Mockito.when(config.resolveStringMaxLengthForType(Mockito.any())).thenReturn(null);
Mockito.when(config.resolveArrayMinItemsForType(Mockito.any())).thenReturn(null);
Mockito.when(config.resolveArrayMaxItemsForType(Mockito.any())).thenReturn(null);
Mockito.when(config.resolveArrayUniqueItemsForType(Mockito.any())).thenReturn(null);
ObjectMapper objectMapper = new ObjectMapper();
Mockito.when(config.createObjectNode()).then(_invocation -> objectMapper.createObjectNode());
Mockito.when(config.createArrayNode()).then(_invocation -> objectMapper.createArrayNode());
this.contextImpl = new SchemaGenerationContextImpl(config, this.getContext().getTypeContext());
}
use of com.github.victools.jsonschema.generator.SchemaGeneratorConfig 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;
}
Aggregations