How can you generate 'properties' entry, even for Classes without fields? #433
-
Is there any configuration parameter to generate the For example, for the following class: class EmptyClass {
} Currently, I'm getting this: {
"type":"object"
} But I want to get this one: {
"type":"object",
"properties":{}
} This is my Java code: var jacksonModule = new JacksonModule(JacksonOption.RESPECT_JSONPROPERTY_REQUIRED);
var configBuilder = new SchemaGeneratorConfigBuilder(SchemaVersion.DRAFT_2020_12,
OptionPreset.PLAIN_JSON)
.with(jacksonModule)
.without(Option.SCHEMA_VERSION_INDICATOR);
var config = configBuilder.build();
var generator = new SchemaGenerator(config);
var jsonSchema = generator.generateSchema(myClass); |
Beta Was this translation helpful? Give feedback.
Answered by
CarstenWickner
Feb 17, 2024
Replies: 1 comment 1 reply
-
Hi @sashirestela, While there is no standard // add the properties keyword to all empty object definitions
configBuilder.forTypesInGeneral().withTypeAttributeOverride((node, scope, context) -> {
JsonNode typeValue = node.get(context.getKeyword(SchemaKeyword.TAG_TYPE));
String objectTypeValue = context.getKeyword(SchemaKeyword.TAG_TYPE_OBJECT);
if (typeValue == null || node.has(context.getKeyword(SchemaKeyword.TAG_PROPERTIES))) {
// nothing to do here
} else if (typeValue.isTextual() && objectTypeValue.equals(typeValue.textValue())) {
node.putObject(context.getKeyword(SchemaKeyword.TAG_PROPERTIES));
} else if (typeValue.isArray()) {
for (JsonNode singleTypeValue : typeValue) {
if (objectTypeValue.equals(singleTypeValue.textValue())) {
node.putObject(context.getKeyword(SchemaKeyword.TAG_PROPERTIES));
return;
}
}
}
}); That being said: why would you want this? |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
CarstenWickner
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi @sashirestela,
While there is no standard
Option
for it or anything, you can achieve that of course: