Skip to content

Commit

Permalink
Legg til openapi eksempel initaliserer (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
ugur93 authored Jan 17, 2024
1 parent f5950f9 commit 3a7a67b
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
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

0 comments on commit 3a7a67b

Please sign in to comment.