use of com.github.victools.jsonschema.generator.MemberScope 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.MemberScope in project jsonschema-generator by victools.
the class SchemaGenerationContextImpl method generateObjectDefinition.
/**
* Preparation Step: add the given targetType (which was previously determined to be anything but an array type).
*
* @param targetType object type to add
* @param definition node in the JSON schema to which all collected attributes should be added
*/
private void generateObjectDefinition(ResolvedType targetType, ObjectNode definition) {
definition.put(this.getKeyword(SchemaKeyword.TAG_TYPE), this.getKeyword(SchemaKeyword.TAG_TYPE_OBJECT));
/*
* Collecting fields and methods according to their order in the Java Byte Code.
* Depending on the compiler implementation, that order may or may not match the declaration order in the source code.
*/
final Map<String, MemberScope<?, ?>> targetProperties = new LinkedHashMap<>();
final Set<String> requiredProperties = new HashSet<>();
this.collectObjectProperties(targetType, targetProperties, requiredProperties);
if (!targetProperties.isEmpty()) {
ObjectNode propertiesNode = this.generatorConfig.createObjectNode();
List<MemberScope<?, ?>> sortedProperties = targetProperties.values().stream().sorted(this.generatorConfig::sortProperties).collect(Collectors.toList());
for (MemberScope<?, ?> property : sortedProperties) {
JsonNode subSchema;
if (property instanceof FieldScope) {
subSchema = this.populateFieldSchema((FieldScope) property);
} else if (property instanceof MethodScope) {
subSchema = this.populateMethodSchema((MethodScope) property);
} else {
throw new IllegalStateException("Unsupported member scope of type " + property.getClass());
}
propertiesNode.set(property.getSchemaPropertyName(), subSchema);
}
definition.set(this.getKeyword(SchemaKeyword.TAG_PROPERTIES), propertiesNode);
if (!requiredProperties.isEmpty()) {
ArrayNode requiredNode = this.generatorConfig.createArrayNode();
// list required properties in the same order as the property
sortedProperties.stream().map(MemberScope::getSchemaPropertyName).filter(requiredProperties::contains).forEach(requiredNode::add);
definition.set(this.getKeyword(SchemaKeyword.TAG_REQUIRED), requiredNode);
}
}
}
use of com.github.victools.jsonschema.generator.MemberScope in project jsonschema-generator by victools.
the class Swagger2Module method overrideInstanceAttributes.
/**
* Consider various remaining aspects.
* <ul>
* <li>{@code @Schema(not = ...)}</li>
* <li>{@code @Schema(allOf = ...)}</li>
* <li>{@code @Schema(minProperties = ...)}</li>
* <li>{@code @Schema(maxProperties = ...)}</li>
* <li>{@code @Schema(requiredProperties = ...)}</li>
* </ul>
*
* @param memberAttributes already collected schema for the field/method
* @param member targeted field/method
* @param context generation context
*/
protected void overrideInstanceAttributes(ObjectNode memberAttributes, MemberScope<?, ?> member, SchemaGenerationContext context) {
Schema annotation = this.getSchemaAnnotationValue(member, Function.identity(), x -> true).orElse(null);
if (annotation == null) {
return;
}
if (annotation.not() != Void.class) {
memberAttributes.set(context.getKeyword(SchemaKeyword.TAG_NOT), context.createDefinitionReference(context.getTypeContext().resolve(annotation.not())));
}
if (annotation.allOf().length > 0) {
Stream.of(annotation.allOf()).map(rawType -> context.getTypeContext().resolve(rawType)).map(context::createDefinitionReference).forEach(memberAttributes.withArray(context.getKeyword(SchemaKeyword.TAG_ALLOF))::add);
}
if (annotation.minProperties() > 0) {
memberAttributes.put(context.getKeyword(SchemaKeyword.TAG_PROPERTIES_MIN), annotation.minProperties());
}
if (annotation.maxProperties() > 0) {
memberAttributes.put(context.getKeyword(SchemaKeyword.TAG_PROPERTIES_MAX), annotation.maxProperties());
}
if (annotation.requiredProperties().length > 0) {
Set<String> alreadyMentionedRequiredFields = new HashSet<>();
ArrayNode requiredFieldNames = memberAttributes.withArray(context.getKeyword(SchemaKeyword.TAG_REQUIRED));
requiredFieldNames.forEach(arrayItem -> alreadyMentionedRequiredFields.add(arrayItem.asText()));
Stream.of(annotation.requiredProperties()).filter(field -> !alreadyMentionedRequiredFields.contains(field)).forEach(requiredFieldNames::add);
}
}
Aggregations