Skip to content

[Bug] NPE in KotlinDeprecatedPropertyCustomizer - resolvedSchema is null #3121

@youming-lin

Description

@youming-lin

Describe the bug

Stack trace:

java.lang.NullPointerException: Cannot invoke "io.swagger.v3.oas.models.media.Schema.get$ref()" because "resolvedSchema" is null
	at org.springdoc.core.customizers.KotlinDeprecatedPropertyCustomizer.resolve(KotlinDeprecatedPropertyCustomizer.kt:67) ~[springdoc-openapi-starter-common-2.8.13.jar:2.8.13]
	at org.springdoc.core.converters.KotlinInlineClassUnwrappingConverter.resolve(KotlinInlineClassUnwrappingConverter.kt:42) ~[springdoc-openapi-starter-common-2.8.13.jar:2.8.13]
	at io.swagger.v3.core.converter.ModelConverterContextImpl.resolve(ModelConverterContextImpl.java:97) ~[swagger-core-jakarta-2.2.36.jar:2.2.36]
	at io.swagger.v3.core.jackson.ModelResolver.resolve(ModelResolver.java:824) ~[swagger-core-jakarta-2.2.36.jar:2.2.36]
	at org.springdoc.core.converters.AdditionalModelsConverter.resolve(AdditionalModelsConverter.java:176) ~[springdoc-openapi-starter-common-2.8.13.jar:2.8.13]
	at org.springdoc.core.converters.FileSupportConverter.resolve(FileSupportConverter.java:72) ~[springdoc-openapi-starter-common-2.8.13.jar:2.8.13]
	at org.springdoc.core.converters.ResponseSupportConverter.resolve(ResponseSupportConverter.java:87) ~[springdoc-openapi-starter-common-2.8.13.jar:2.8.13]
	at org.springdoc.core.converters.SchemaPropertyDeprecatingConverter.resolve(SchemaPropertyDeprecatingConverter.java:95) ~[springdoc-openapi-starter-common-2.8.13.jar:2.8.13]
<rest omitted>

I'm upgrading app to Spring Boot 3, which necessitated Springdoc update. I'm using v2.8.13.

The bug is caused when the following conditions are met:

  1. Usage of Kotlin
  2. Usage of @JsonUnwrapped
  3. Usage of @Deprecated in the nested object that is being unwrapped

To Reproduce
Steps to reproduce the behavior:

  • What version of spring-boot you are using?
    v3.5.7
  • What modules and versions of springdoc-openapi are you using?
    springdoc-openapi-starter-webmvc-ui, v2.8.13
  • What is the actual and the expected result using OpenAPI Description (yml or json)?
    Actual: affected type is missing in yaml
    Expected: the type is correctly generated and included in yml
  • Provide with a sample code (HelloController) or Test that reproduces the problem

Sample code to reproduce error:

package com.example.demo

import com.fasterxml.jackson.annotation.JsonUnwrapped
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

@RestController
@RequestMapping("/test")
@SpringBootApplication
class DemoApp {
    @GetMapping
    fun getTestData(): Foo = Foo(bar = Bar(baz = "test"))
}

data class Foo(
    @field:JsonUnwrapped
    val bar: Bar,
)

data class Bar(
    @Deprecated("This is deprecated")
    val baz: String,
)

fun main(args: Array<String>) {
    runApplication<DemoApp>(*args)
}

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.5.7</version>
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>KotlinDeprecatedPropertyCustomizer-bug</name>
    <description>Example showing bug in KotlinDeprecatedPropertyCustomizer</description>
    <properties>
        <java.version>21</java.version>
        <kotlin.version>2.0.20</kotlin.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-reflect</artifactId>
        </dependency>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-stdlib</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-test-junit5</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springdoc</groupId>
            <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
            <version>2.8.13</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
        </dependency>
    </dependencies>

    <build>
        <sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
        <testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.jetbrains.kotlin</groupId>
                <artifactId>kotlin-maven-plugin</artifactId>
                <configuration>
                    <args>
                        <arg>-Xjsr305=strict</arg>
                    </args>
                    <compilerPlugins>
                        <plugin>spring</plugin>
                    </compilerPlugins>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.jetbrains.kotlin</groupId>
                        <artifactId>kotlin-maven-allopen</artifactId>
                        <version>${kotlin.version}</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>

</project>

After server starts, hit http://localhost:8080/swagger-ui/index.html. Observe exception in logs.

Expected behavior

  • A clear and concise description of what you expected to happen.
    No exception, and type is included in yaml.
  • What is the expected result using OpenAPI Description (yml or json)?
    Yaml should include the response type; currently it's missing.

Screenshots
If applicable, add screenshots to help explain your problem.

Additional context
Add any other context about the problem here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions