Skip to content

Add opt-in customizer to drop Kotlin properties with a default value from required - #3464

Draft
anastasiia-baboshina wants to merge 1 commit into
springdoc:mainfrom
anastasiia-baboshina:fix/3463-kotlin-default-optional
Draft

anastasiia-baboshina wants to merge 1 commit into
springdoc:mainfrom
anastasiia-baboshina:fix/3463-kotlin-default-optional

Conversation

@anastasiia-baboshina

Copy link
Copy Markdown

What

Kotlin properties with a default value (for example val tags: List<String> = emptyList()) are listed in the required array of the schema. A client can leave them out, and the Kotlin default is used, so the documented schema is stricter than the real API.

This adds KotlinDefaultValueOptionalPropertyCustomizer, a model converter that removes these properties from required.

  • A property marked with @Schema(requiredMode = REQUIRED) stays required.
  • Renamed properties (@JsonProperty, @JsonNaming) are handled: Kotlin names are mapped to JSON names through Jackson, like in KotlinxSerializationRequiredPropertyCustomizer.
  • If required becomes empty, it is removed from the schema.
  • It only changes Kotlin classes that have a primary constructor.

Why it is opt-in

The customizer is disabled by default. A schema is built once per class, so a DTO used as a request and as a response has one required list. For a request, "has a default" means optional. For a response, the property is always sent, so it is better to keep it required. Because of this, I did not want to change the default output for everyone. Enable it with:

springdoc.model-converters.kotlin-default-value-optional-property-customizer.enabled=true

If you prefer another default, or a different rule (for example only for request bodies), I am happy to change it. The property name is only a suggestion.

How I checked it

I used the example from the issue, with Jackson 3 and the Jackson 3 Kotlin module. I added test app28 in springdoc-openapi-kotlin-webmvc-tests (Kotlin class and expected JSON in results/3.1.0/app28.json).

  • Before the change: the test fails, required has 4 items instead of 2.
  • After the change: the test passes, required is ["forced", "name"].
  • Full runs in Docker (maven:3-eclipse-temurin-21, JDK 21):
    • springdoc-openapi-starter-common: 81 tests, 0 failures
    • springdoc-openapi-starter-webmvc-api: 686 tests, 0 failures
    • springdoc-openapi-starter-webflux-api: 141 tests, 0 failures
    • springdoc-openapi-kotlin-webmvc-tests: 46 tests, 0 failures (includes the new test and the existing snapshots)
    • springdoc-openapi-kotlin-webflux-tests: 25 tests, 0 failures
    • springdoc-openapi-kotlin-data-rest-tests: 1 test, 0 failures

The test class removes the customizer from the shared ModelConverters after it runs, so it does not change the schemas of other tests in the same JVM.

Not checked: the case from the issue with the Jackson 2 Kotlin module (com.fasterxml.jackson.module:jackson-module-kotlin) on the classpath. The customizer works on the schema, not on the module, so I expect the same result, but I did not run it.

Fixes #3463

…from required

jackson-module-kotlin reports a non-null Kotlin property with a default
value as required for serialization, so it was listed in the "required"
array of the schema, although a client can omit it.

Add KotlinDefaultValueOptionalPropertyCustomizer. It removes such
properties from "required", keeps the ones marked with
@Schema(requiredMode = REQUIRED) and maps Kotlin names to JSON names.
It is disabled by default because a class used as a request and as a
response has a single schema. Enable it with
springdoc.model-converters.kotlin-default-value-optional-property-customizer.enabled=true

Fixes springdoc#3463

This branch has not been deployed

No deployments
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.

Kotlin properties with a default value are documented as required

1 participant