Skip to content
This repository was archived by the owner on Mar 16, 2025. It is now read-only.

Commit cf5ff46

Browse files
committed
improve no response content type handling, #80
1 parent a78ab00 commit cf5ff46

1 file changed

Lines changed: 20 additions & 20 deletions

File tree

  • src/main/kotlin/io/openapiprocessor/core/model

src/main/kotlin/io/openapiprocessor/core/model/Endpoint.kt

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -162,42 +162,42 @@ class Endpoint(
162162
private fun getSuccessResponses(): Set<Response> {
163163
val result = mutableMapOf<String, Response>()
164164

165-
responses
166-
.filterKeys { it.startsWith("2") }
167-
.values
168-
.flatten()
165+
// prefer responses with content type.
166+
filterSuccessResponses()
167+
.filter { hasContentType(it) }
169168
.forEach {
170169
result[it.contentType] = it
171170
}
172171

172+
// check for responses without content type (e.g. 204) to generate a void method.
173+
if (result.isEmpty()) {
174+
filterSuccessResponses()
175+
.forEach {
176+
result[it.contentType] = it
177+
}
178+
}
179+
173180
return result
174181
.values
175182
.toSet()
176183
}
177184

178185
private fun getErrorResponses(): Set<Response> {
179186
return responses
180-
.filterKeys { !it.startsWith("2") }
187+
.filterKeys { !isSuccessCode(it) }
181188
.values
182189
.map { it.first() }
183190
.filter { !it.empty }
184191
.toSet()
185192
}
186193

187-
}
194+
private fun filterSuccessResponses() = responses
195+
.filterKeys { isSuccessCode(it) }
196+
.values
197+
.flatten()
188198

189-
/*
190-
private Set<Response> getSuccessResponses () {
191-
Map<String, Response> result = [:]
192-
193-
responses.findAll {
194-
it.key.startsWith ('2')
195-
}.each {
196-
it.value.each {
197-
result.put (it.contentType, it)
198-
}
199-
}
199+
private fun isSuccessCode(code: String) = code.startsWith("2")
200200

201-
result.values () as Set<Response>
202-
}
203-
*/
201+
private fun hasContentType(response: Response) = response.contentType != "?"
202+
203+
}

0 commit comments

Comments
 (0)