Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Respect schema annotations when using spring mvc with kotlin #2443

Merged
merged 1 commit into from
Feb 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package org.springdoc.core.configuration;

import com.fasterxml.jackson.module.kotlin.KotlinModule;
import com.fasterxml.jackson.module.kotlin.KotlinModule.Builder;
import org.springdoc.core.properties.SpringDocConfigProperties;
import org.springdoc.core.providers.ObjectMapperProvider;

import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Primary;

/**
* The type Spring doc kotlin module configuration.
Expand All @@ -21,16 +22,19 @@
@ConditionalOnClass(KotlinModule.class)
@ConditionalOnExpression("${springdoc.api-docs.enabled:true} and ${springdoc.enable-kotlin:true}")
@ConditionalOnWebApplication
@ConditionalOnBean(SpringDocConfiguration.class)
class SpringDocJacksonKotlinModuleConfiguration {

/**
* Instantiates a new Spring doc kotlin module configuration.
* Instantiates a new objectMapperProvider with a kotlin module.
*
* @param objectMapperProvider the object mapper provider
* @param springDocConfigProperties the spring doc config properties
*/
public SpringDocJacksonKotlinModuleConfiguration(ObjectMapperProvider objectMapperProvider) {
objectMapperProvider.jsonMapper()
.registerModule(new Builder().build());

@Bean
@Primary
ObjectMapperProvider objectMapperProvider(SpringDocConfigProperties springDocConfigProperties) {
ObjectMapperProvider mapperProvider = new ObjectMapperProvider(springDocConfigProperties);
mapperProvider.jsonMapper().registerModule(new KotlinModule.Builder().build());
return mapperProvider;
}
}
1 change: 1 addition & 0 deletions springdoc-openapi-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<module>springdoc-openapi-actuator-webflux-tests</module>
<module>springdoc-openapi-actuator-webmvc-tests</module>
<module>springdoc-openapi-kotlin-tests</module>
<module>springdoc-openapi-kotlin-mvc-tests</module>
<module>springdoc-openapi-hateoas-tests</module>
<module>springdoc-openapi-data-rest-tests</module>
</modules>
Expand Down
144 changes: 144 additions & 0 deletions springdoc-openapi-tests/springdoc-openapi-kotlin-mvc-tests/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
######################
# Project Specific
######################
/target/www/**
/src/test/javascript/coverage/

######################
# Node
######################
/node/
node_tmp/
node_modules/
npm-debug.log.*
/.awcache/*
/.cache-loader/*

######################
# SASS
######################
.sass-cache/

######################
# Eclipse
######################
*.pydevproject
.project
.metadata
tmp/
tmp/**/*
*.tmp
*.bak
*.swp
*~.nib
local.properties
.classpath
.settings/
.loadpath
.factorypath
/src/main/resources/rebel.xml

# External tool builders
.externalToolBuilders/**

# Locally stored "Eclipse launch configurations"
*.launch

# CDT-specific
.cproject

# PDT-specific
.buildpath

######################
# Intellij
######################
.idea/
*.iml
*.iws
*.ipr
*.ids
*.orig
classes/
out/

######################
# Visual Studio Code
######################
.vscode/

######################
# Maven
######################
/log/
/target/

######################
# Gradle
######################
.gradle/
/build/

######################
# Package Files
######################
*.jar
*.war
*.ear
*.db

######################
# Windows
######################
# Windows image file caches
Thumbs.db

# Folder config file
Desktop.ini

######################
# Mac OSX
######################
.DS_Store
.svn

# Thumbnails
._*

# Files that might appear on external disk
.Spotlight-V100
.Trashes

######################
# Directories
######################
/bin/
/deploy/

######################
# Logs
######################
*.log*

######################
# Others
######################
*.class
*.*~
*~
.merge_file*

######################
# Gradle Wrapper
######################
!gradle/wrapper/gradle-wrapper.jar

######################
# Maven Wrapper
######################
!.mvn/wrapper/maven-wrapper.jar

######################
# ESLint
######################
.eslintcache
83 changes: 83 additions & 0 deletions springdoc-openapi-tests/springdoc-openapi-kotlin-mvc-tests/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springdoc-openapi-tests</artifactId>
<groupId>org.springdoc</groupId>
<version>2.2.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>springdoc-openapi-kotlin-mvc-tests</artifactId>

<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-kotlin</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<artifactId>kotlin-maven-plugin</artifactId>
<groupId>org.jetbrains.kotlin</groupId>
<executions>
<execution>
<id>compile</id>
<phase>process-sources</phase>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<sourceDirs>
<sourceDir>${project.basedir}/src/main/kotlin</sourceDir>
</sourceDirs>
</configuration>
</execution>
<execution>
<id>test-compile</id>
<goals>
<goal>test-compile</goal>
</goals>
<configuration>
<sourceDirs>
<sourceDir>${project.basedir}/src/test/kotlin</sourceDir>
</sourceDirs>
</configuration>
</execution>
</executions>
<configuration>
<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>
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
*
* * Copyright 2019-2020 the original author or authors.
* *
* * Licensed under the Apache License, Version 2.0 (the "License");
* * you may not use this file except in compliance with the License.
* * You may obtain a copy of the License at
* *
* * https://www.apache.org/licenses/LICENSE-2.0
* *
* * Unless required by applicable law or agreed to in writing, software
* * distributed under the License is distributed on an "AS IS" BASIS,
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* * See the License for the specific language governing permissions and
* * limitations under the License.
*
*/

package test.org.springdoc.api

import org.junit.jupiter.api.Test
import org.skyscreamer.jsonassert.JSONAssert
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.test.context.ActiveProfiles
import org.springframework.test.web.servlet.MockMvc
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders
import org.springframework.test.web.servlet.result.MockMvcResultMatchers
import java.nio.charset.StandardCharsets
import java.nio.file.Files
import java.nio.file.Paths

@SpringBootTest
@AutoConfigureMockMvc
@ActiveProfiles("test")
abstract class AbstractKotlinSpringDocMVCTest {

@Autowired
val mockMvc: MockMvc? = null

private val logger = LoggerFactory.getLogger(AbstractKotlinSpringDocMVCTest::class.java)

@Test
fun testApp() {
var result: String? = null
try {
val response = mockMvc!!.perform(MockMvcRequestBuilders.get("/v3/api-docs"))
.andExpect(MockMvcResultMatchers.status().isOk).andReturn()

result = response.response.contentAsString
val className = javaClass.simpleName
val testNumber = className.replace("[^0-9]".toRegex(), "")

val expected = getContent("results/app$testNumber.json")
JSONAssert.assertEquals(expected, result, true)
} catch (e: AssertionError) {
logger.error(result)
throw e
}
}

companion object {
@Throws(Exception::class)
fun getContent(fileName: String): String {
try {
val path = Paths.get(
AbstractKotlinSpringDocMVCTest::class.java.classLoader.getResource(
fileName
)!!.toURI()
)
val fileBytes = Files.readAllBytes(path)
return String(fileBytes, StandardCharsets.UTF_8)
} catch (e: Exception) {
throw RuntimeException("Failed to read file: $fileName", e)
}

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package test.org.springdoc.api.app10

import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController
import test.org.springdoc.api.app8.Greeting

data class Greeting(val greeting: String)

@RestController
class ExampleController {
@GetMapping("/")
fun greet(@RequestParam name: String?) = Greeting("Hello ${name ?: "world"}")

@GetMapping("/test")
fun test(@RequestParam name: String) = Greeting("Hello $name")

@GetMapping("/test-with-default")
fun testWithDefault(@RequestParam(defaultValue = "world") name: String) = Greeting("Hello $name")
}