how to handle custom annotations and custom keywords(properties) #432
-
Hi, @ClassMetadata(name=“ClassName”, owner=“team name”, desc=“class description”)
public class ClassName {
@FieldMetadata(desc=“Field description”, encrypted=true, sensitiveData= true)
private String fieldName;
} should generate something like {
"metadata": {
"owner": "team name"
},
"type": "object",
"description": "class description",
"properties": {
"fieldName": {
"type": "string",
"description": "Field description",
"sensitiveData": true,
"encrypted": true
}
}
} Can someone help |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
HI @ramlg, If you want to add custom schema keywords/attributes, you'll be looking into what I call "Attribute Overrides" here. configBuilder.forTypesInGeneral().withTypeAttributeOverride((node, scope, context) -> {
Class<?> erasedType = scope.getErasedType();
ClassMetadata metadataAnnotation = erasedType.getAnnotation(ClassMetadata.class);
if (metadataAnnotation != null && !metadataAnnotation.owner().isEmpty()) {
node.putObject("metadata")
.put("owner", metadataAnnotation.owner());
}
}); For other standard keywords, you can decide to include it in the above "Attribute Override" or use the built-in support, e.g. configBuilder.forTypesInGeneral().withDescriptionResolver(scope -> Optional.of(scope.getType().getErasedType())
.map(erasedType -> erasedType.getAnnotation(ClassMetadata.class))
.map(ClassMetadata::desc)
.filter(description -> !description.isEmpty())
.orElse(null)); The same pattern applies to your configBuilder.forFields().withInstanceAttributeOverride((node, field, context) -> {
FieldMetadata metadataAnnotation = field.getAnnotationConsideringFieldAndGetterIfSupported(FieldMetadata.class);
if (metadataAnnotation == null) {
return;
}
if (metadataAnnotation.sensitiveData()) {
node.put("sensitiveData", true);
}
if (metadataAnnotation.encrypted()) {
node.put("encrypted", true);
}
});
configBuilder.forFields().withDescriptionResolver(field -> Optional.ofNullable(field.getAnnotationConsideringFieldAndGetterIfSupported(FieldMetadata.class))
.map(FieldMetadata::desc)
.filter(description -> !description.isEmpty())
.orElse(null)); I cobbled this together from the various existing examples and didn't test this in particular now, but it should get you in the right direction. 😉 |
Beta Was this translation helpful? Give feedback.
HI @ramlg,
If you want to add custom schema keywords/attributes, you'll be looking into what I call "Attribute Overrides" here.
E.g., https://victools.github.io/jsonschema-generator/#type-attribute-overrides
The example in the documentation is close to what you want. Adjusted for your sample case that could look like: