Optional properties using Jackson2 #183
Merged
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.
This PR is continuation of #179, thanks @Yona-Appletree for feature request and first implementation.
This PR adds
optionalProperties
parameter with allowed valuesuseSpecifiedAnnotations
,useLibraryDefinition
andall
.It also changes how Jackson2 modules are treated, this is incompatible change, please see details below.
Optional Properties
1.
optionalProperties
=useSpecifiedAnnotations
Let's say we want to generate following TypeScript interface:
We can do this with this configuration:
and Java class:
This was already possible and it is still default behavior.
2.
optionalProperties
=useLibraryDefinition
With this new feature we can leverage Jackson2 to determine which properties are optional and which required. Then it is possible to use Jackson2 annotations, JAXB annotations or even Kotlin nullable types to specify property optionality.
Jackson2 library
Following configuration:
allows us to use
@JsonProperty
to specify optionality:JAXB library
Similarly for JAXB. Configuration:
class:
Kotlin nullable types
Jackson2 provides
KotlinModule
which makes nullable types accessible to typescript-generator.This module can be configured using this snippet:
Then we can write Kotlin data class like this:
and typescript-generator will know from Jackson2 which properties are required/optional.
3.
optionalProperties
=all
This makes all properties optional. This setting have same effect as
declarePropertiesAsOptional
parameter (which is now deprecated).Jackson2 modules
Before this PR typescript-generator by default loaded all Jackson2 modules automatically. This automatic module discovery relies on module presence on classpath which is relatively fragile mechanism and for example
JaxbAnnotationModule
currently breaks@JsonProperty.required
functionality.So now typescript-generator by default loads no modules. Previous behavior can be turned on using
jackson2ModuleDiscovery
parameter. Individual modules can be specified usingjackson2Modules
parameter (recommended way). OlddisableJackson2ModuleDiscovery
parameter is effectively removed.