Skip to content

Commit bf44ac2

Browse files
committed
improve endpoint creation
1 parent 923fcd0 commit bf44ac2

File tree

3 files changed

+26
-42
lines changed

3 files changed

+26
-42
lines changed

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

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -133,22 +133,21 @@ class ApiConverter(
133133

134134
val parameters = collectParameters(operation, ctx)
135135
val requestBodies = collectRequestBodies(operation, ctx)
136+
val responses = collectResponses (operation, ctx)
136137

137138
val ep = Endpoint(
138139
path,
139140
operation.getMethod(),
140141
parameters + requestBodies.parameters,
141142
requestBodies.bodies,
143+
responses,
142144
operation.getOperationId(),
143145
operation.isDeprecated(),
144146
Documentation(
145147
summary = operation.summary,
146148
description = operation.description
147-
)
148-
)
149+
))
149150

150-
collectResponses (operation.getResponses(), ep, dataTypes, resolver)
151-
ep.initEndpointResponses ()
152151
checkSuccessResponse(ep)
153152
ep
154153
} catch (e: UnknownDataTypeException) {
@@ -216,17 +215,20 @@ class ApiConverter(
216215
return RequestBodies(bodies, params)
217216
}
218217

219-
private fun collectResponses(responses: Map<String, Response>, ep: Endpoint, dataTypes: DataTypes, resolver: RefResolver) {
220-
responses.forEach { (httpStatus, httpResponse) ->
218+
private fun collectResponses(operation: Operation, ctx: ConverterContext): Map<String, List<ModelResponse>> {
219+
val responses: MutableMap<String, List<ModelResponse>> = mutableMapOf()
220+
221+
operation.getResponses().forEach { (httpStatus, httpResponse) ->
221222
val results = createResponses(
222-
ep,
223+
operation,
223224
httpStatus,
224225
httpResponse,
225-
dataTypes,
226-
resolver)
226+
ctx)
227227

228-
ep.addResponses (httpStatus, results)
228+
responses[httpStatus] = results
229229
}
230+
231+
return responses
230232
}
231233

232234
private fun createParameter(op: Operation, parameter: Parameter, ctx: ConverterContext): ModelParameter {
@@ -327,11 +329,14 @@ class ApiConverter(
327329
return parameters
328330
}
329331

330-
private fun createResponses(ep: Endpoint, httpStatus: String, response: Response, dataTypes: DataTypes, resolver: RefResolver): List<ModelResponse> {
332+
private fun createResponses(operation: Operation, httpStatus: String, response: Response, ctx: ConverterContext): List<ModelResponse> {
331333
if (response.getContent().isEmpty()) {
332334
val info = SchemaInfo (
333-
SchemaInfo.Endpoint(ep.path, ep.method),
334-
"", "", nullSchema, resolver)
335+
SchemaInfo.Endpoint(ctx.path, operation.getMethod()),
336+
"",
337+
"",
338+
nullSchema,
339+
ctx.resolver)
335340

336341
val dataType = NoneDataType()
337342
val singleDataType = singleDataTypeWrapper.wrap (dataType, info)
@@ -345,13 +350,13 @@ class ApiConverter(
345350
val schema = mediaType.getSchema()
346351

347352
val info = SchemaInfo (
348-
SchemaInfo.Endpoint(ep.path, ep.method),
349-
getInlineResponseName (ep.path, ep.method, httpStatus),
353+
SchemaInfo.Endpoint(ctx.path, operation.getMethod()),
354+
getInlineResponseName (ctx.path, operation.getMethod(), httpStatus),
350355
contentType,
351356
schema,
352-
resolver)
357+
ctx.resolver)
353358

354-
val dataType = convertDataType(info, dataTypes)
359+
val dataType = convertDataType(info, ctx.dataTypes)
355360
val changedType = if (!info.isArray ()) { // todo fails if ref
356361
singleDataTypeWrapper.wrap(dataType, info)
357362
} else {

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

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,13 @@ class Endpoint(
1818
val method: HttpMethod,
1919
val parameters: List<Parameter>,
2020
val requestBodies: List<RequestBody>,
21+
val responses: Map<String, List<Response>>,
2122
val operationId: String? = null,
2223
val deprecated: Boolean = false,
2324
private val documentation: Documentation? = null
2425
) {
25-
// todo
26-
private /*val*/ var responses: MutableMap<String, List<Response>> = mutableMapOf()
27-
2826
// grouped responses
29-
lateinit var endpointResponses: List<EndpointResponse>// = emptyList()
30-
31-
// todo move to constructor
32-
fun addResponses(httpStatus: String, statusResponses: List<Response>) {
33-
responses[httpStatus] = statusResponses
34-
}
35-
36-
// todo move addResponse() to constructor then run this from constructor
37-
// fluent
38-
fun initEndpointResponses (): Endpoint {
39-
endpointResponses = createEndpointResponses ()
40-
return this
41-
}
27+
val endpointResponses: List<EndpointResponse> = createEndpointResponses()
4228

4329
// hmmm
4430
fun getRequestBody(): RequestBody {
@@ -199,5 +185,4 @@ class Endpoint(
199185
private fun isSuccessCode(code: String) = code.startsWith("2")
200186

201187
private fun hasContentType(response: Response) = response.contentType != "?"
202-
203188
}

openapi-processor-core/src/test/kotlin/io/openapiprocessor/core/builder/api/EndpointBuilder.kt

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,21 +60,15 @@ class EndpointBuilder(
6060
}
6161

6262
fun build(): Endpoint {
63-
val ep = Endpoint(
63+
return Endpoint(
6464
path,
6565
method,
6666
parameters,
6767
bodies,
68+
responses,
6869
operationId = operationId,
6970
deprecated = deprecated,
7071
documentation = Documentation(summary, description)
7172
)
72-
73-
responses.forEach { (k, v) ->
74-
ep.addResponses(k, v)
75-
}
76-
77-
return ep.initEndpointResponses()
7873
}
79-
8074
}

0 commit comments

Comments
 (0)