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

Commit 64debf8

Browse files
committed
handle response style, #68
1 parent 2ade5c1 commit 64debf8

5 files changed

Lines changed: 276 additions & 114 deletions

File tree

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

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package io.openapiprocessor.core.model
77

88
import io.openapiprocessor.core.model.datatypes.AnyOneOfObjectDataType
99
import io.openapiprocessor.core.model.datatypes.ResultDataType
10+
import io.openapiprocessor.core.processor.mapping.v2.ResultStyle
1011

1112
/**
1213
* The responses that can be returned by an endpoint method for one (successful) response.
@@ -29,18 +30,36 @@ class EndpointResponse(
2930
get() = main.contentType
3031

3132
/**
32-
* provides the response type.
33+
* provides the response type based on the requested style.
3334
*
34-
* If the endpoint has multiple responses and there is no result data type the response type
35-
* is `Object`. If the response has a result data type the response type is `ResultDataType<?>`.
35+
* [ResultStyle.SUCCESS]
36+
* - response type is the single success response type regardless of the number of available
37+
* error responses.
38+
*
39+
* [ResultStyle.ALL]
40+
* - If the endpoint has multiple responses the response type is `Object`. If the response is
41+
* wrapped by a result data type (i.e. wrapper) the response type is `ResultDataType<?>`.
42+
* - If the endpoint has only a success response type it is used as the response type.
43+
*
44+
* @param style required style
45+
* @return the response data type in the requested style
46+
*/
47+
fun getResponseType(style: ResultStyle): String {
48+
if (isAnyOneOfResponse())
49+
return getMultiResponseTypeName()
50+
51+
if (style == ResultStyle.ALL && errors.isNotEmpty())
52+
return getMultiResponseTypeName()
53+
54+
return getSingleResponseTypeName()
55+
}
56+
57+
/**
58+
* test only: provides the response type.
3659
*/
3760
val responseType: String
3861
get() {
39-
return if (hasMultipleResponses()) {
40-
getMultiResponseTypeName()
41-
} else {
42-
getSingleResponseTypeName()
43-
}
62+
return getResponseType(ResultStyle.SUCCESS)
4463
}
4564

4665
val description: String?
@@ -49,16 +68,19 @@ class EndpointResponse(
4968
/**
5069
* provides the imports required for {@link #getResponseType()}.
5170
*
71+
* @param style required style
5272
* @return list of imports
5373
*/
54-
val responseImports: Set<String>
55-
get() {
56-
return if (hasMultipleResponses()) {
57-
getImportsMulti()
58-
} else {
59-
getImportsSingle()
60-
}
61-
}
74+
75+
fun getResponseImports(style: ResultStyle): Set<String> {
76+
if (isAnyOneOfResponse())
77+
return getImportsMulti()
78+
79+
if (style == ResultStyle.ALL && errors.isNotEmpty())
80+
return getImportsMulti()
81+
82+
return getImportsSingle()
83+
}
6284

6385
/**
6486
* returns a list with all content types.
@@ -76,14 +98,8 @@ class EndpointResponse(
7698
return result
7799
}
78100

79-
/**
80-
* if this response has multiple types.
81-
*/
82-
private fun hasMultipleResponses(): Boolean {
83-
if (main.responseType is AnyOneOfObjectDataType) {
84-
return true
85-
}
86-
return errors.isNotEmpty()
101+
private fun isAnyOneOfResponse(): Boolean {
102+
return main.responseType is AnyOneOfObjectDataType
87103
}
88104

89105
/**
@@ -109,14 +125,7 @@ class EndpointResponse(
109125
}
110126

111127
private fun getImportsSingle(): Set<String> {
112-
val imports = mutableSetOf<String>()
113-
114-
imports.addAll (main.imports)
115-
errors.forEach {
116-
imports.addAll (it.imports)
117-
}
118-
119-
return imports
128+
return main.imports
120129
}
121130

122131
}

src/main/kotlin/io/openapiprocessor/core/writer/java/InterfaceWriter.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package io.openapiprocessor.core.writer.java
1818

1919
import io.openapiprocessor.core.framework.FrameworkAnnotations
2020
import io.openapiprocessor.core.converter.ApiOptions
21+
import io.openapiprocessor.core.converter.resultStyle
2122
import io.openapiprocessor.core.model.Endpoint
2223
import io.openapiprocessor.core.model.Interface
2324
import io.openapiprocessor.core.model.parameters.AdditionalParameter
@@ -99,7 +100,9 @@ class InterfaceWriter(
99100
}
100101

101102
ep.endpointResponses.forEach { mr ->
102-
val responseImports: MutableSet<String> = mr.responseImports.toMutableSet()
103+
val responseImports: MutableSet<String> = mr.getResponseImports(
104+
apiOptions.resultStyle).toMutableSet()
105+
103106
if (responseImports.isNotEmpty()) {
104107
imports.addAll (responseImports)
105108
}

src/main/kotlin/io/openapiprocessor/core/writer/java/MethodWriter.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package io.openapiprocessor.core.writer.java
77

88
import io.openapiprocessor.core.converter.ApiOptions
9+
import io.openapiprocessor.core.converter.resultStyle
910
import io.openapiprocessor.core.model.Endpoint
1011
import io.openapiprocessor.core.model.EndpointResponse
1112
import io.openapiprocessor.core.model.parameters.AdditionalParameter
@@ -44,8 +45,8 @@ open class MethodWriter(
4445

4546
target.write (
4647
"""
47-
| ${createMappingAnnotation (endpoint, endpointResponse)}
48-
| ${endpointResponse.responseType} ${createMethodName (endpoint, endpointResponse)}(${createParameters(endpoint)});
48+
| ${createMappingAnnotation(endpoint, endpointResponse)}
49+
| ${createResult(endpointResponse)} ${createMethodName(endpoint, endpointResponse)}(${createParameters(endpoint)});
4950
|
5051
""".trimMargin())
5152
}
@@ -56,6 +57,10 @@ open class MethodWriter(
5657
return annotation.toString ()
5758
}
5859

60+
private fun createResult(endpointResponse: EndpointResponse): String {
61+
return endpointResponse.getResponseType(apiOptions.resultStyle)
62+
}
63+
5964
private fun createMethodName(endpoint: Endpoint, endpointResponse: EndpointResponse): String {
6065
val tokens: MutableList<String>
6166

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright 2021 https://github.com/openapi-processor/openapi-processor-core
3+
* PDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.openapiprocessor.core.model
7+
8+
import io.kotest.core.spec.style.StringSpec
9+
import io.kotest.matchers.shouldBe
10+
import io.openapiprocessor.core.model.datatypes.AnyOneOfObjectDataType
11+
import io.openapiprocessor.core.model.datatypes.DataTypeName
12+
import io.openapiprocessor.core.model.datatypes.ObjectDataType
13+
import io.openapiprocessor.core.model.datatypes.StringDataType
14+
import io.openapiprocessor.core.processor.mapping.v2.ResultStyle
15+
16+
class EndpointResponseSpec: StringSpec({
17+
18+
"anyOf/oneOf always uses multi response" {
19+
val er = EndpointResponse(
20+
Response("", AnyOneOfObjectDataType("Foo", "pkg", "anyOf")),
21+
emptySet()
22+
)
23+
24+
er.getResponseType(ResultStyle.ALL) shouldBe "Object"
25+
er.getResponseImports(ResultStyle.ALL) shouldBe emptySet()
26+
er.getResponseType(ResultStyle.SUCCESS) shouldBe "Object"
27+
er.getResponseImports(ResultStyle.SUCCESS) shouldBe emptySet()
28+
}
29+
30+
"result style all with errors uses multi response" {
31+
val er = EndpointResponse(
32+
Response("", StringDataType()),
33+
setOf(Response("text/plain", StringDataType()))
34+
)
35+
36+
er.getResponseType(ResultStyle.ALL) shouldBe "Object"
37+
er.getResponseImports(ResultStyle.ALL) shouldBe emptySet()
38+
}
39+
40+
"result style all without errors uses single response" {
41+
val er = EndpointResponse(
42+
Response("", ObjectDataType(DataTypeName("Foo"), "pkg", linkedMapOf(
43+
"bar" to StringDataType()
44+
))),
45+
emptySet()
46+
)
47+
48+
er.getResponseType(ResultStyle.ALL) shouldBe "Foo"
49+
er.getResponseImports(ResultStyle.ALL) shouldBe setOf("pkg.Foo")
50+
}
51+
52+
"result style single uses single response" {
53+
val er = EndpointResponse(
54+
Response("", ObjectDataType(DataTypeName("Foo"), "pkg", linkedMapOf(
55+
"bar" to StringDataType()
56+
))),
57+
setOf(Response("text/plain", StringDataType()))
58+
)
59+
60+
er.getResponseType(ResultStyle.SUCCESS) shouldBe "Foo"
61+
er.getResponseImports(ResultStyle.SUCCESS) shouldBe setOf("pkg.Foo")
62+
}
63+
64+
})

0 commit comments

Comments
 (0)