Skip to content

KotlinRequiredPropertyCustomizer marks all Java DTO properties as required when using springdoc-openapi #67

Description

@vpelikh

Description

When using io.github.vpelikh:springdoc-openapi:4.0.0, the KotlinRequiredPropertyCustomizer (registered as a ModelConverter) runs Kotlin reflection over every model — not just Kotlin ones. It only skips java.* packages.

For a Java class, Kotlin reflection sees platform types, and KType.isMarkedNullable() returns false, so every property of every Java DTO gets marked required.

Impact

In the reporter's application, this caused an unintended expansion of the required fields in 142 of 150 schemas.

Workaround

Set springdoc.enable-kotlin: false in application.yaml to disable the automatic customizer, then re-register only KotlinNullablePropertyCustomizer manually:

@Configuration
class KotlinAwareModelResolverConfiguration {

  @Bean
  ModelResolver kotlinAwareModelResolver(SpringDocConfigProperties properties) {
    var openapi31 = properties.isOpenapi31();
    var base = openapi31 ? Json31.mapper() : Json.mapper();

    var mapper = SpringDocHalJacksonModuleUtils.configureHalOnBuilder(
      base.rebuild()
        .addModule(new SpringDocSealedClassModule())
        .addModule(new KotlinModule.Builder().build()))
      .build();

    return new ModelResolver(mapper).openapi31(openapi31);
  }

  @Bean
  KotlinNullablePropertyCustomizer kotlinNullablePropertyCustomizer(ObjectMapperProvider objectMapperProvider) {
    return new KotlinNullablePropertyCustomizer(objectMapperProvider);
  }
}

Source

Reported by @mschout in the Jackson 3 migration PR:
swagger-api/swagger-core#5031 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions