Skip to content

Commit 11e20db

Browse files
committed
add missing application/x-www-form-urlencoded when destructuring a request body (openapi-processor/openapi-processor-spring#479)
1 parent 2770f5c commit 11e20db

3 files changed

Lines changed: 20 additions & 5 deletions

File tree

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,8 @@ class ApiConverter(
179179
Documentation(
180180
summary = operation.summary,
181181
description = operation.description
182-
))
182+
),
183+
requestBodies.consumesContentTypes)
183184

184185
checkSuccessResponse(ep)
185186
ep
@@ -222,7 +223,11 @@ class ApiConverter(
222223
return mappingFinder.findDropParameterTypeMappings(MappingFinderQuery(path, method))
223224
}
224225

225-
data class RequestBodies(val bodies: List<ModelRequestBody>, val parameters: List<ModelParameter>)
226+
data class RequestBodies(
227+
val bodies: List<ModelRequestBody>,
228+
val parameters: List<ModelParameter>,
229+
/* additional content types that are lost otherwise */
230+
val consumesContentTypes: Set<String> = emptySet())
226231

227232
private fun collectRequestBodies(requestBody: RequestBody?, ctx: ApiConverterContext): RequestBodies {
228233
if (requestBody == null) {
@@ -231,6 +236,7 @@ class ApiConverter(
231236

232237
val bodies: MutableList<ModelRequestBody> = mutableListOf()
233238
val params: MutableList<ModelParameter> = mutableListOf()
239+
val consumesContentTypes = mutableSetOf<String>()
234240

235241
requestBody.getContent().forEach { (contentType, mediaType) ->
236242
val info = SchemaInfo(
@@ -247,14 +253,15 @@ class ApiConverter(
247253
if (isBodyStyleObject(info)) {
248254
bodies.add(createRequestBody(contentType, info, requestBody, ctx.dataTypes))
249255
} else {
256+
consumesContentTypes.add(contentType)
250257
params.addAll(createUrlencodedParameter(info, requestBody, ctx.dataTypes))
251258
}
252259
} else {
253260
bodies.add(createRequestBody(contentType, info, requestBody, ctx.dataTypes))
254261
}
255262
}
256263

257-
return RequestBodies(bodies, params)
264+
return RequestBodies(bodies, params, consumesContentTypes)
258265
}
259266

260267
private fun isBodyStyleObject(info: SchemaInfo): Boolean {

openapi-processor-core/src/main/kotlin/io/openapiprocessor/core/model/Endpoint.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ package io.openapiprocessor.core.model
99
import io.openapiprocessor.core.model.parameters.MultipartParameter
1010
import io.openapiprocessor.core.model.parameters.Parameter
1111
import io.openapiprocessor.core.openapi.HttpMethod
12-
import kotlin.collections.plusAssign
1312

1413
/**
1514
* Endpoint properties.
@@ -22,7 +21,9 @@ class Endpoint(
2221
val responses: Map<HttpStatus, List<Response>>,
2322
val operationId: String? = null,
2423
val deprecated: Boolean = false,
25-
private val documentation: Documentation? = null
24+
private val documentation: Documentation? = null,
25+
/** additional content types that can't be autodetected here */
26+
private val consumesContentTypes: Set<String> = emptySet()
2627
) {
2728
// grouped responses
2829
val endpointResponses: List<EndpointResponse> = createEndpointResponses()
@@ -71,6 +72,8 @@ class Endpoint(
7172
contentTypes.add ("multipart/form-data")
7273
}
7374

75+
contentTypes.addAll (consumesContentTypes)
76+
7477
return contentTypes
7578
}
7679

openapi-processor-core/src/test/kotlin/io/openapiprocessor/core/converter/ApiConverterRequestBodySpec.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import io.kotest.core.spec.style.StringSpec
1010
import io.kotest.matchers.booleans.shouldBeFalse
1111
import io.kotest.matchers.booleans.shouldBeTrue
1212
import io.kotest.matchers.collections.shouldBeEmpty
13+
import io.kotest.matchers.collections.shouldContainAnyOf
1314
import io.kotest.matchers.collections.shouldContainExactly
1415
import io.kotest.matchers.shouldBe
1516
import io.kotest.matchers.string.shouldContain
@@ -293,6 +294,8 @@ class ApiConverterRequestBodySpec: StringSpec({
293294
val foo = ep.parameters[0]
294295
val bar = ep.parameters[1]
295296

297+
ep.getConsumesContentTypes() shouldContainAnyOf listOf("application/x-www-form-urlencoded")
298+
296299
foo.dataType.getName() shouldBe "string"
297300
foo.dataType.getTypeName() shouldBe "String"
298301
foo.name shouldBe "foo"
@@ -384,6 +387,8 @@ class ApiConverterRequestBodySpec: StringSpec({
384387
ep.parameters.isEmpty()
385388
val body = ep.requestBodies.first()
386389

390+
ep.getConsumesContentTypes() shouldContainAnyOf listOf("application/x-www-form-urlencoded")
391+
387392
body.dataType.getTypeName() shouldBe "FooPostRequestBody"
388393
body.name shouldBe "body"
389394
}

0 commit comments

Comments
 (0)