Skip to content

Conversation

tadam313
Copy link

Motivation of this change

Nullable types in json schema often represented as "field":{ "anyOf":[{"type": "<real_type>"},{"type": "null"}] which falls under union type handling and then type Object would be generated loosing the actual type information from the schema. I think there is a possibility to do more on this front as from type generation perspective nullability is only a matter of type boxing. This PR is to address / recognise this exact scenario and use the type information from the union while leave other union type scenarios unchanged.

Changes

  • added resolveNullableType method to top level SchemaRule to detect exactly this scenario and use boxed type
    • it gets all the remaining properties from the non-null node and copies to the original node and trigger the recursion, so they will be processed as usual

Edge case: when such a field is required then we should not add a NonNull / NotNull annotations since the field is nullable by default. To handle that I changed passing the property schema in type rules instead of the parent. It was only used at NotRequiredRule.java but there I could use parent instead.

The rest of the changes are testing related and some minor improvements. Any feedback is welcome!

@tadam313 tadam313 changed the title Nullable type support Nullable "anyOf" type support May 26, 2025
@ctrimble ctrimble self-requested a review September 1, 2025 06:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant