use of com.github.victools.jsonschema.generator.FieldScope in project jsonschema-generator by victools.
the class SchemaGenerationContextImpl method populateFieldSchema.
/**
* Preparation Step: create a node for a schema representing the given field's associated value type.
*
* @param field field/property to populate the schema node for
* @return schema node representing the given field/property
*/
private ObjectNode populateFieldSchema(FieldScope field) {
List<ResolvedType> typeOverrides = this.generatorConfig.resolveTargetTypeOverrides(field);
if (typeOverrides == null) {
typeOverrides = this.generatorConfig.resolveSubtypes(field.getType(), this);
}
List<FieldScope> fieldOptions;
if (typeOverrides == null || typeOverrides.isEmpty()) {
fieldOptions = Collections.singletonList(field);
} else {
fieldOptions = typeOverrides.stream().map(field::withOverriddenType).collect(Collectors.toList());
}
// consider declared type (instead of overridden one) for determining null-ability
boolean isNullable = !field.getRawMember().isEnumConstant() && (!field.isFakeContainerItemScope() || this.generatorConfig.shouldAllowNullableArrayItems()) && this.generatorConfig.isNullable(field);
if (fieldOptions.size() == 1) {
return this.createFieldSchema(fieldOptions.get(0), isNullable, false, null);
}
ObjectNode subSchema = this.generatorConfig.createObjectNode();
ArrayNode anyOfArray = subSchema.withArray(this.getKeyword(SchemaKeyword.TAG_ANYOF));
if (isNullable) {
anyOfArray.addObject().put(this.getKeyword(SchemaKeyword.TAG_TYPE), this.getKeyword(SchemaKeyword.TAG_TYPE_NULL));
}
fieldOptions.forEach(option -> anyOfArray.add(this.createFieldSchema(option, false, false, null)));
return subSchema;
}
use of com.github.victools.jsonschema.generator.FieldScope in project jsonschema-generator by victools.
the class SchemaGenerationContextImpl method generateArrayDefinition.
/**
* Preparation Step: add the given targetType (which was previously determined to be an array type).
*
* @param targetScope (possibly generic) array type to add
* @param definition node in the JSON schema to which all collected attributes should be added
* @param isNullable whether the field/method's return value the targetType refers to is allowed to be null in the declaring type
*/
private void generateArrayDefinition(TypeScope targetScope, ObjectNode definition, boolean isNullable) {
if (isNullable) {
ArrayNode typeArray = this.generatorConfig.createArrayNode().add(this.getKeyword(SchemaKeyword.TAG_TYPE_ARRAY)).add(this.getKeyword(SchemaKeyword.TAG_TYPE_NULL));
definition.set(this.getKeyword(SchemaKeyword.TAG_TYPE), typeArray);
} else {
definition.put(this.getKeyword(SchemaKeyword.TAG_TYPE), this.getKeyword(SchemaKeyword.TAG_TYPE_ARRAY));
}
if (targetScope instanceof MemberScope<?, ?> && !((MemberScope<?, ?>) targetScope).isFakeContainerItemScope()) {
MemberScope<?, ?> fakeArrayItemMember = ((MemberScope<?, ?>) targetScope).asFakeContainerItemScope();
JsonNode fakeItemDefinition;
if (targetScope instanceof FieldScope) {
fakeItemDefinition = this.populateFieldSchema((FieldScope) fakeArrayItemMember);
} else if (targetScope instanceof MethodScope) {
fakeItemDefinition = this.populateMethodSchema((MethodScope) fakeArrayItemMember);
} else {
throw new IllegalStateException("Unsupported member type: " + targetScope.getClass().getName());
}
definition.set(this.getKeyword(SchemaKeyword.TAG_ITEMS), fakeItemDefinition);
} else {
ObjectNode arrayItemTypeRef = this.generatorConfig.createObjectNode();
definition.set(this.getKeyword(SchemaKeyword.TAG_ITEMS), arrayItemTypeRef);
this.traverseGenericType(targetScope.getContainerItemType(), arrayItemTypeRef, false);
}
}
use of com.github.victools.jsonschema.generator.FieldScope in project jsonschema-generator by victools.
the class JacksonModuleTest method testPropertyNameOverride.
@Test
@Parameters
public void testPropertyNameOverride(String fieldName, String expectedOverrideValue, String kebabCaseName) throws Exception {
new JacksonModule().applyToConfigBuilder(this.configBuilder);
ArgumentCaptor<ConfigFunction<FieldScope, String>> captor = ArgumentCaptor.forClass(ConfigFunction.class);
Mockito.verify(this.fieldConfigPart, Mockito.times(2)).withPropertyNameOverrideResolver(captor.capture());
FieldScope field = new TestType(TestClassForPropertyNameOverride.class).getMemberField(fieldName);
List<String> overrideValues = captor.getAllValues().stream().map(nameOverride -> nameOverride.apply(field)).collect(Collectors.toList());
Assert.assertEquals(expectedOverrideValue, overrideValues.get(0));
Assert.assertEquals(kebabCaseName, overrideValues.get(1));
}
use of com.github.victools.jsonschema.generator.FieldScope in project jsonschema-generator by victools.
the class JacksonModuleTest method testReadOnlyWriteOnly.
@Test
@Parameters
public void testReadOnlyWriteOnly(String fieldName, boolean expectedReadOnly, boolean expectedWriteOnly) {
new JacksonModule().applyToConfigBuilder(this.configBuilder);
FieldScope field = new TestType(TestClassWithRequiredAnnotatedFields.class).getMemberField(fieldName);
Assert.assertEquals(this.fieldConfigPart.isReadOnly(field), expectedReadOnly);
Assert.assertEquals(this.fieldConfigPart.isWriteOnly(field), expectedWriteOnly);
}
use of com.github.victools.jsonschema.generator.FieldScope in project jsonschema-generator by victools.
the class JacksonModuleTest method testDescriptionForTypeResolver.
@Test
@Parameters
public void testDescriptionForTypeResolver(String fieldName, String expectedDescription) throws Exception {
new JacksonModule().applyToConfigBuilder(this.configBuilder);
FieldScope field = new TestType(TestClassForDescription.class).getMemberField(fieldName);
ArgumentCaptor<ConfigFunction<TypeScope, String>> captor = ArgumentCaptor.forClass(ConfigFunction.class);
Mockito.verify(this.typesInGeneralConfigPart).withDescriptionResolver(captor.capture());
String description = captor.getValue().apply(field);
Assert.assertEquals(expectedDescription, description);
}
Aggregations