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

Legg til openapi eksempel initaliserer #43

Merged
merged 1 commit into from
Jan 17, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions bidrag-commons/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,11 @@
<artifactId>bidrag-transport-felles</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<scope>provided</scope>
</dependency>
<!-- test -->
<dependency>
<groupId>io.mockk</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package no.nav.bidrag.commons.util

import io.swagger.v3.oas.models.OpenAPI
import io.swagger.v3.oas.models.PathItem
import io.swagger.v3.oas.models.examples.Example
import org.springdoc.core.customizers.OpenApiCustomizer
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.http.HttpMethod
import org.springframework.http.MediaType

@Configuration
class OpenApiExamplesConfig {
@Bean
fun openApiCustomiser(examples: Collection<OpenApiExample>): OpenApiCustomizer {
return OpenApiCustomizer { openAPI ->
examples.forEach { example ->
openAPI.components.addExamples(example.name, example.example)
examples.groupBy { Pair(it.path, it.method) }.entries.forEach {
openAPI.addExamplesToPath(it.key, it.value)
}
}
}
}

fun OpenAPI.addExamplesToPath(
operation: Pair<String, HttpMethod>,
openApiExamples: List<OpenApiExample>,
) {
val requestBody =
paths[operation.first]?.getOperation(operation.second)
?.requestBody

val jsonBody = requestBody?.content?.get(MediaType.APPLICATION_JSON_VALUE)

openApiExamples
.forEach {
jsonBody?.addExamples(it.name, it.example)
}
}

fun PathItem.getOperation(httpMethod: HttpMethod) =
when (httpMethod) {
HttpMethod.POST -> post
HttpMethod.PUT -> put
HttpMethod.PATCH -> patch
HttpMethod.DELETE -> delete
HttpMethod.OPTIONS -> options
HttpMethod.GET -> get
else -> null
}
}

data class OpenApiExample(
val example: Example,
val method: HttpMethod,
val path: String,
val name: String = example.description,
)
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<mockk.version>1.13.9</mockk.version>
<spring-boot.version>3.2.1</spring-boot.version>
<kotlin-logging-jvm.version>6.0.2</kotlin-logging-jvm.version>
<springdoc-openapi-ui.version>2.3.0</springdoc-openapi-ui.version>

<!-- plugins -->
<maven-compiler.version>3.12.1</maven-compiler.version>
Expand Down Expand Up @@ -87,7 +88,13 @@
<artifactId>kotlin-logging-jvm</artifactId>
<version>${kotlin-logging-jvm.version}</version>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>${springdoc-openapi-ui.version}</version>
</dependency>

<!-- test-->
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-test-junit5</artifactId>
Expand Down
Loading