Disclaimer: I'm the maintainer of the victools/jsonschema-generator
library mentioned below.
If you're not dead-set on using the (slightly outdated) FasterXML/jackson-module-jsonSchema, you could use the victools/jsonschema-generator library. The latter is supporting the newer JSON Schema Draft versions and gives you a lot of flexibility in terms of what ends up in your generated schema. However, it is (at least as of today) not supporting the same range of jackson specific annotations out-of-the-box.
That being said, there are multiple ways to go about what you're asking.
1. Simply ignore properties that belong to a different context
SchemaGeneratorConfigBuilder configBuilder = new SchemaGeneratorConfigBuilder(
SchemaVersion.DRAFT_2019_09, OptionPreset.PLAIN_JSON);
configBuilder.forFields()
.withIgnoreCheck(field -> {
BelongsToSchema annotation = field.getAnnotation(BelongsToSchema.class);
return annotation != null
&& !Arrays.asList(annotation.schema()).contains("alice");
});
2. Exclude the schema of properties from a different context while mentioning them
configBuilder.forFields()
.withTargetTypeOverridesResolver(field -> {
BelongsToSchema annotation = field.getAnnotation(BelongsToSchema.class);
if (annotation == null || Arrays.asList(annotation.value()).contains("alice")) {
return null;
}
return Collections.singletonList(field.getContext().resolve(Object.class));
});
3. Include an external $ref
instead of the actual subschema
configBuilder.forFields()
.withCustomDefinitionProvider((field, context) -> {
BelongsToSchema annotation = field.getAnnotation(BelongsToSchema.class);
if (annotation == null || Arrays.asList(annotation.value()).contains("alice")) {
return null;
}
ObjectNode customSubschema = context.getGeneratorConfig().createObjectNode()
.put(SchemaKeyword.TAG_REF.forVersion(SchemaVersion.DRAFT_2019_09),
"https://your-external.ref/" + field.getSimpleTypeDescription());
return new CustomPropertyDefinition(customSubschema);
});
There are probably a few more possibilities depending on what it is you want exactly.
I encourage you to play around with it a bit and have a look at the documentation. If you have project-specific questions, feel free to raise those as Issues on GitHub.