Skip to content

Commit 1f9773d

Browse files
committed
add result-style
1 parent 7e972b3 commit 1f9773d

File tree

4 files changed

+87
-10
lines changed

4 files changed

+87
-10
lines changed

docs/modules/ROOT/nav.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
** xref:mapping/parameter.adoc[Global Parameter Mapping]
1717
** xref:mapping/response.adoc[Global Response Mapping]
1818
** xref:mapping/result.adoc[Global Result Mapping]
19+
** xref:mapping/result-style.adoc[Global Result Style]
1920
** xref:mapping/single-multi.adoc[Global Single & Multi Mapping]
2021
** xref:mapping/endpoint.adoc[Endpoint Mapping]
2122
** xref:mapping/null.adoc[Null Mapping]
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
= Result Style
2+
include::partial$links.adoc[]
3+
4+
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.

docs/modules/ROOT/pages/mapping/structure.adoc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ map:
4545
# result wrapper
4646
result: {target type}
4747
48+
#result-style:
49+
result-style: {success|all}
50+
4851
# single wrapper
4952
single: {target type}
5053

docs/modules/ROOT/pages/processor/endpoint-content.adoc

Lines changed: 62 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ A simple path of the OpenAPI description will usually produce a single endpoint
44
interface as described in the introduction.
55

66
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
88
plain text:
99

1010
[source,yaml]
@@ -53,8 +53,8 @@ components:
5353
A client request uses the request `Accept` header to tell the api which result content types it can
5454
handle.
5555

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.
5858

5959
== multiple content types
6060

@@ -85,14 +85,69 @@ public interface Api {
8585
The apis normal response (status '200') can return the result as json or as plain text which leads
8686
to two methods for the same endpoint but with a different `produces` list in the mapping annotation.
8787

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
8989
selecting the correct endpoint.
9090

91-
91+
[#result_style]
9292
== multiple content types & default content type
9393

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.
125+
126+
[source,java]
127+
----
128+
package generated.api;
129+
130+
import org.springframework.http.ResponseEntity;
131+
import org.springframework.web.bind.annotation.GetMapping;
132+
133+
public interface Api {
134+
135+
@GetMapping(
136+
path = "/foo",
137+
produces = {"application/json", "application/xml"})
138+
Foo getFooApplicationJson();
139+
140+
@GetMapping(
141+
path = "/foo",
142+
produces = {"text/plain", "application/xml"})
143+
String getFooTextPlain();
144+
145+
}
146+
----
147+
148+
**previous behavior, before 2021.5**
149+
150+
Example of the previous code, using `result-style: all`.The setting is required to generate the previous code.
96151

97152
[source,java]
98153
----
@@ -116,9 +171,6 @@ public interface Api {
116171
}
117172
----
118173

119-
Both endpoints need to handle the success case (json or text), and the error (xml) case. So both
120-
mappings contain the xml content type. To handle the multiple responses the return type is now
121-
`Object`.
122174

123175
== multiple content types, default content type & result wrapper
124176

0 commit comments

Comments
 (0)