Skip to content

Commit fe68dcb

Browse files
committed
calculate response interfaces (#247)
1 parent 6867324 commit fe68dcb

File tree

1 file changed

+42
-4
lines changed
  • openapi-processor-core/src/main/kotlin/io/openapiprocessor/core/converter

1 file changed

+42
-4
lines changed

openapi-processor-core/src/main/kotlin/io/openapiprocessor/core/converter/ApiConverter.kt

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import io.openapiprocessor.core.parser.HttpMethod
2020
import io.openapiprocessor.core.parser.RequestBody
2121
import io.openapiprocessor.core.parser.Response
2222
import io.openapiprocessor.core.processor.mapping.v2.ResultStyle
23+
import io.openapiprocessor.core.support.capitalizeFirstChar
2324
import io.openapiprocessor.core.writer.Identifier
2425
import org.slf4j.Logger
2526
import org.slf4j.LoggerFactory
@@ -219,10 +220,7 @@ class ApiConverter(
219220

220221
private fun collectResponses(responses: Map<HttpStatus, Response>, ctx: ApiConverterContext): Map<String, List<ModelResponse>> {
221222
val resultResponses: MutableMap<String, List<ModelResponse>> = mutableMapOf()
222-
223-
val resultStyle = getResultStyle(ctx.path, ctx.method)
224-
val responseCollector = ContentTypeResponseCollector(responses, resultStyle)
225-
val interfaceCollector = ContentTypeInterfaceCollector(ctx.path, ctx.method, responseCollector)
223+
val contentTypeInterfaces = collectContentTypeInterfaces(responses, ctx)
226224

227225
responses.forEach { (httpStatus, httpResponse) ->
228226
val results = createResponses(
@@ -236,6 +234,38 @@ class ApiConverter(
236234
return resultResponses
237235
}
238236

237+
private fun collectContentTypeInterfaces(responses: Map<HttpStatus, Response>, ctx: ApiConverterContext)
238+
: Map<ContentType, ContentTypeInterface> {
239+
val resultStyle = getResultStyle(ctx.path, ctx.method)
240+
val responseCollector = ContentTypeResponseCollector(responses, resultStyle)
241+
242+
// to check if a response marker interface is wanted it is necessary to know if the responses have the same
243+
// result data type. In case they have the same data type we do not need the marker interface.
244+
//
245+
// Unfortunately we have to calculate the result data types to achieve this because it is currently not possible
246+
// to detect this from the parsed OpenAPI.
247+
248+
val checkResponses: MutableMap<String, List<ModelResponse>> = mutableMapOf()
249+
250+
val checkDataTypes = ctx.dataTypes.copy()
251+
responses.forEach { (httpStatus, httpResponse) ->
252+
val results = createResponses(
253+
httpStatus,
254+
httpResponse,
255+
ctx.with(checkDataTypes)
256+
)
257+
258+
checkResponses[httpStatus] = results
259+
}
260+
261+
val interfaceCollector = ContentTypeInterfaceCollector(ctx.path, ctx.method)
262+
val contentTypeInterfaces = interfaceCollector.collectContentTypeInterfaces(
263+
responseCollector.contentTypeResponses,
264+
checkResponses
265+
)
266+
return contentTypeInterfaces
267+
}
268+
239269
private fun createParameter(parameter: Parameter, ctx: ApiConverterContext): ModelParameter {
240270
val info = SchemaInfo (
241271
SchemaInfo.Endpoint(ctx.path, ctx.method),
@@ -409,4 +439,12 @@ class ApiConverter(
409439
return targetInterfaceName
410440
}
411441

442+
private fun getResponseMarkerInterfaceName(path: String, method: HttpMethod, contentType: String): String {
443+
return listOf(
444+
method.method.capitalizeFirstChar(),
445+
identifier.toClass(path),
446+
identifier.toClass(contentType),
447+
"Response"
448+
).joinToString("")
449+
}
412450
}

0 commit comments

Comments
 (0)