-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Legg til openapi eksempel initaliserer (#43)
- Loading branch information
Showing
3 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
bidrag-commons/src/main/kotlin/no/nav/bidrag/commons/util/OpenApiExamplesConfig.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters