fix: support objects in enum keyword#529
Closed
tearfur wants to merge 1 commit into
Closed
Conversation
Member
|
Hi @tearfur, Two notes:
You can use the one in the class CodePairDefinitionProvider implements CustomDefinitionProviderV2 {
private static final String BASE_SCHEMA = "{\"type\":\"object\",\"properties\":{\"firstCode\":{\"type\":\"string\"},\"secondCode\":{\"type\":\"string\"},\"scheme\":{\"type\":\"string\"}}}";
@Override
public CustomDefinition provideCustomSchemaDefinition(ResolvedType javaType, SchemaGenerationContext context) {
if (javaType.isInstanceOf(CodePair.class)) {
var customNode = context.getGeneratorConfig().getObjectMapper().readTree(BASE_SCHEMA);
var enumArray = customNode.putArray(context.getKeyword(SchemaKeyword.TAG_ENUM);
for (SchemeVersion scheme : SchemeVersion.values()) {
for (FirstCode first : FirstCode.values()) {
if (first.getScheme() != scheme) {
continue;
}
for (SecondCode second : SecondCode.values()) {
if (second.getScheme() != scheme) {
continue;
}
var permutation = context.getGeneratorConfig().createObjectNode()
.put("firstCode", first.getCode())
.put("secondCode", second.getCode())
.put("scheme", scheme.name());
enumArray.add(permutation);
}
}
}
return new CustomDefinition(customNode);
}
return null;
}
}You can clean this up and streamline it etc., but the above is mainly meant to illustrate the approach anyway. |
Contributor
Author
|
Your CustomDefinition example was very helpful, thanks! I will close this PR because I don't need it anymore. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds support for including JSON objects in the
enumkeyword withwithEnumResolver(), as the spec permits this.https://json-schema.org/draft/2020-12/draft-bhutton-json-schema-validation-01#name-enum
https://json-schema.org/draft/2019-09/draft-handrews-json-schema-validation-02#rfc.section.6.1.2
https://json-schema.org/draft-07/draft-handrews-json-schema-validation-01#rfc.section.6.1.2
https://json-schema.org/draft-06/draft-wright-json-schema-validation-01#rfc.section.6.23
I have tested this in my use-case. If there's a better way to do this, feel free to let me know.