You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The `result-style` configuration controls how the processor handles the return type of endpoint success and error response types if both are defined in the OpenAPI.
5
+
6
+
[source,yaml]
7
+
----
8
+
map:
9
+
#result-style: success # use the success result type, this is the default
10
+
result-style: all # use an Object return type
11
+
----
12
+
13
+
* **result-style** (optional).
14
+
15
+
**`success` (default since 2021.5): generates endpoint methods with the success response type even if it has error responses. This assumes that errors are reported by exceptions.
16
+
17
+
**`all` (default before 2021.5): generates endpoint methods with an `Object` return type if it has error responses.
18
+
19
+
20
+
21
+
See xref:processor/endpoint-content.adoc#result_style[endpoint content types] for a more detailed description.
Copy file name to clipboardExpand all lines: docs/modules/ROOT/pages/processor/endpoint-content.adoc
+62-10Lines changed: 62 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,7 @@ A simple path of the OpenAPI description will usually produce a single endpoint
4
4
interface as described in the introduction.
5
5
6
6
OpenAPI allows us to define more complex apis that behave differently based on the request header.
7
-
For example the following api definition can return its response in different formats.As json or as
7
+
For example the following api definition can return its response in different formats.As json or as
8
8
plain text:
9
9
10
10
[source,yaml]
@@ -53,8 +53,8 @@ components:
53
53
A client request uses the request `Accept` header to tell the api which result content types it can
54
54
handle.
55
55
56
-
Using a single endpoint method it has to decide which response to create.This leads to some boring
57
-
`if/else` code.To avoid this the processor creates one endpoint method for each possible response.
56
+
Using a single endpoint method it has to decide which response to create.This leads to some boring
57
+
`if/else` code.To avoid this the processor creates one endpoint method for each possible response.
58
58
59
59
== multiple content types
60
60
@@ -85,14 +85,69 @@ public interface Api {
85
85
The apis normal response (status '200') can return the result as json or as plain text which leads
86
86
to two methods for the same endpoint but with a different `produces` list in the mapping annotation.
87
87
88
-
One method when json gets requested and one when plain text gets requested.Spring will take care of
88
+
One method when json gets requested and one when plain text gets requested.Spring will take care of
89
89
selecting the correct endpoint.
90
90
91
-
91
+
[#result_style]
92
92
== multiple content types & default content type
93
93
94
-
In the (contrived) example our api does also define another content type for all other result status
95
-
codes (usually the errors): xml. This results in the following code:
94
+
In the (contrived) example our api does also define another content type for all other result status codes (usually the errors): xml.
95
+
96
+
If an endpoint returns multiple types, a success response (typically 200 ok) and at least one error response, the processor has to pick a return type for the endpoint methods.
97
+
98
+
With versions *before 2021.5* the processor generates (by default) endpoint methods with an `Object` return value (or if generic something like `ResponseType<?>`) to handle the unrelated success and error response types.
99
+
100
+
This has the drawback that an important piece of information is missing: the success response type.With `Object` as return type it not clear what the success response type is.
101
+
102
+
With version *2021.5* it is possible to generate the endpoints with the success response type even with error responses.It is _ignoring_ the error result types.
103
+
104
+
Since it is common practice to handle errors by throwing exceptions (e.g. in combination with the Spring `@ResponseStatus` annotation) the endpoint methods don't need to handle different return types, and it is possible to simply use the type of the success response.
105
+
106
+
To switch between previous and new behavior there is a new mapping configuration to control the style of the return type named `result-style`.It has two possible values: `success` or `all`.This is currently a global mapping switch.
107
+
108
+
The default is `success`, i.e. the processor will automatically generate the code using the new behavior.In case the previous behavior is required set the `result-style` to `all`.
109
+
110
+
[source,yaml]
111
+
----
112
+
openapi-processor-mapping: v2
113
+
114
+
options:
115
+
package-name: ...
116
+
117
+
map:
118
+
#result-style: success # use the success result type, this is the default
119
+
result-style: all # use an Object return type
120
+
----
121
+
122
+
**new behavior, since 2021.5**
123
+
124
+
Example of the new code, using `result-style: success`.This is the default if `result-style` is not set.
0 commit comments