Skip to content

Commit 96b1b25

Browse files
committed
#2, support requestBody $ref (swagger)
1 parent 40bea35 commit 96b1b25

File tree

4 files changed

+42
-6
lines changed

4 files changed

+42
-6
lines changed

openapi-processor-core/src/main/kotlin/io/openapiprocessor/core/parser/swagger/OpenApi.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class OpenApi(private val result: SwaggerParseResult): ParserOpenApi {
2323
val paths = linkedMapOf<String, ParserPath>()
2424

2525
result.openAPI.paths.forEach { (name: String, value: SwaggerPath) ->
26-
paths[name] = Path(name, value)
26+
paths[name] = Path(name, value, RefResolverNative(result.openAPI))
2727
}
2828

2929
return paths

openapi-processor-core/src/main/kotlin/io/openapiprocessor/core/parser/swagger/Operation.kt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ import io.swagger.v3.oas.models.responses.ApiResponse as SwaggerResponse
2121
class Operation(
2222
private val method: HttpMethod,
2323
private val operation: SwaggerOperation,
24-
private val path: SwaggerPath
24+
private val path: SwaggerPath,
25+
private val refResolver: RefResolverNative
2526
): ParserOperation {
2627

2728
override fun getMethod(): HttpMethod = method
@@ -47,11 +48,15 @@ class Operation(
4748
}
4849

4950
override fun getRequestBody(): ParserRequestBody? {
50-
if (operation.requestBody == null) {
51+
var requestBody = operation.requestBody
52+
if (requestBody == null) {
5153
return null
54+
55+
} else if (requestBody.`$ref` != null) {
56+
requestBody = refResolver.resolve(requestBody)
5257
}
5358

54-
return RequestBody (operation.requestBody)
59+
return RequestBody (requestBody)
5560
}
5661

5762
override fun getResponses(): Map<String, ParserResponse> {

openapi-processor-core/src/main/kotlin/io/openapiprocessor/core/parser/swagger/Path.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ import io.swagger.v3.oas.models.Operation as SwaggerOperation
1414
/**
1515
* Swagger Path abstraction.
1616
*/
17-
class Path(private val path: String, private val info: SwaggerPath): ParserPath {
17+
class Path(
18+
private val path: String,
19+
private val info: SwaggerPath,
20+
private val refResolver: RefResolverNative
21+
): ParserPath {
1822

1923
override fun getPath(): String = path
2024

@@ -24,7 +28,7 @@ class Path(private val path: String, private val info: SwaggerPath): ParserPath
2428
HttpMethod.values().map {
2529
val op = info.getOperation(it.method)
2630
if (op != null) {
27-
ops.add (Operation(it, op, info))
31+
ops.add (Operation(it, op, info, refResolver))
2832
}
2933
}
3034

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright 2022 https://github.com/openapi-processor/openapi-processor-base
3+
* PDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.openapiprocessor.core.parser.swagger
7+
8+
import io.swagger.v3.oas.models.OpenAPI
9+
import io.swagger.v3.oas.models.parameters.RequestBody as SwaggerRequestBody
10+
11+
class RefResolverNative(private val api: OpenAPI) {
12+
13+
fun resolve(body: SwaggerRequestBody): SwaggerRequestBody {
14+
val refName = getRefName(body.`$ref`)
15+
val requestBodies = api.components?.requestBodies
16+
return requestBodies?.get(refName)!!
17+
}
18+
19+
private fun getRefName(ref: String): String? {
20+
val split = ref.split('#')
21+
if (split.size > 1) {
22+
val hash = split[1]
23+
return hash.substring(hash.lastIndexOf('/') + 1)
24+
}
25+
return null
26+
}
27+
}

0 commit comments

Comments
 (0)