Skip to content

Commit

Permalink
fixup! feat: make it possible to return data from /details as CSV #284
Browse files Browse the repository at this point in the history
  • Loading branch information
fengelniederhammer committed Aug 4, 2023
1 parent 17574d1 commit 2f6af2c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.genspectrum.lapis.controller
import io.swagger.v3.oas.models.Operation
import io.swagger.v3.oas.models.media.Content
import org.springdoc.core.customizers.OperationCustomizer
import org.springframework.http.MediaType
import org.springframework.stereotype.Component
import org.springframework.web.method.HandlerMethod

Expand All @@ -11,7 +12,7 @@ class OperationsSorter : OperationCustomizer {
override fun customize(operation: Operation, handlerMethod: HandlerMethod): Operation {
operation.responses.forEach { _, response ->
val applicationJsonFirstContents =
response.content.toSortedMap(compareByDescending { it.contains("application/json") })
response.content.toSortedMap(compareByDescending<String> { it.contains(MediaType.APPLICATION_JSON_VALUE) }.thenBy { it })

response.content = Content().apply {
for ((mediaTypeName, mediaType) in applicationJsonFirstContents) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import io.swagger.v3.oas.models.responses.ApiResponse
import io.swagger.v3.oas.models.responses.ApiResponses
import org.hamcrest.CoreMatchers.`is`
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.hasSize
import org.junit.jupiter.api.Test
import org.springframework.web.method.HandlerMethod

Expand All @@ -21,11 +22,13 @@ class OperationsSorterTest {
content = Content()
content["application/json"] = null
content["text/csv"] = null
content["text/tab-separated-values"] = null
}
responses["csvFirst"] = ApiResponse().apply {
content = Content()
content["text/csv"] = null
content["application/json"] = null
content["text/tab-separated-values"] = null
}
}

Expand All @@ -36,8 +39,10 @@ class OperationsSorterTest {

val jsonFirstKeys = result.responses["jsonFirst"]?.content?.keys
assertThat(jsonFirstKeys?.first(), `is`("application/json"))
assertThat(jsonFirstKeys, hasSize(3))

val csvFirstKeys = result.responses["csvFirst"]?.content?.keys
assertThat(csvFirstKeys?.first(), `is`("application/json"))
assertThat(jsonFirstKeys, hasSize(3))
}
}

0 comments on commit 2f6af2c

Please sign in to comment.