Skip to content

Commit 526fc4f

Browse files
committed
#143, updated documentation
1 parent b6b809f commit 526fc4f

File tree

2 files changed

+175
-48
lines changed

2 files changed

+175
-48
lines changed
Lines changed: 174 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
= annotations
2+
include::partial$links.adoc[]
23

3-
It is possible to add additional annotations to a `source type`. Currently, this is *ONLY* available for endpoint method parameters. Since a `requestBody` is passed as parameter the mapping will work fot it too.
4+
It is possible to add additional annotations to a `source type`. Currently, this is available as
45

5-
== additional parameter annotations
6+
* global _annotation type mapping_:
7+
+
8+
it adds an annotation to the model class generated for the `source type`.
69
7-
To add an annotation to a `source type` (i.e. an OpenAPI type) parameter the mapping supports an _annotation type mapping_. It is defined like below, and it should be added to the `map/parameters` section in the mapping.yaml.
10+
* global & endpoint parameter _annotation type mapping_:
11+
+
12+
it adds an annotation to a parameter of the `source type`. Since the request body is passed as parameter the mapping will work for it too.
813
9-
It is also available as an endpoint (method) mapping to restrict the mapping to a specific endpoint. This will go to the `map/paths/<endpoint path>/parameters` section in the mapping.yaml.
14+
It is defined like below, and it should be added to the `map/types` or `map/parameters` section in the mapping.yaml.
15+
16+
It is also available as an endpoint (http method) mapping to restrict the mapping to a specific endpoint. This will go to the `map/paths/<endpoint path>/parameters` section in the mapping.yaml.
1017

1118
The annotation type mapping is similar to other mappings and is defined like this:
1219

@@ -22,96 +29,123 @@ receive the additional annotation.
2229

2330
** **{annotation type}** is the fully qualified class name of the java annotation type. It may have parameters (see example below).
2431

32+
The link:{oap-samples}[samples project] has a small example using annotation mappings similar to the one described below.
2533

2634
== mapping example
2735

28-
Given the following OpenAPI description:
36+
Given the following OpenAPI description, that describe two (echo like) endpoints that receive an object via post and return the same object. In the mapping file we add a custom bean validation annotation. It checks the sum of both properties in `Foo` and `Bar`.
2937

3038
[source,yaml]
3139
----
32-
openapi: 3.0.3
40+
openapi: 3.1.0
3341
info:
3442
title: openapi-processor-spring sample api
3543
version: 1.0.0
3644
3745
paths:
3846
/foo:
39-
get:
47+
post:
4048
tags:
4149
- foo
4250
summary: annotation mapping example.
43-
description: a simple endpoint where an annotation mapping is used
44-
parameters:
45-
- in: query
46-
name: foo
47-
schema:
48-
$ref: '#/components/schemas/Foo'
51+
description: a simple endpoint where an annotation mapping is used on the request body
52+
requestBody:
53+
content:
54+
application/json:
55+
schema:
56+
$ref: '#/components/schemas/Foo'
57+
required: true
4958
responses:
50-
'204':
51-
description: no content
52-
53-
/foo-body:
59+
'200':
60+
description: echo of the source parameter
61+
content:
62+
application/json:
63+
schema:
64+
$ref: '#/components/schemas/Foo'
65+
66+
/bar:
5467
post:
5568
tags:
56-
- foo
69+
- bar
5770
summary: annotation mapping example.
58-
description: a simple endpoint where an annotation mapping is used
71+
description: a simple endpoint where an annotation mapping is used on the request body
5972
requestBody:
6073
content:
6174
application/json:
6275
schema:
63-
$ref: '#/components/schemas/Foo'
76+
$ref: '#/components/schemas/Bar'
6477
required: true
6578
responses:
66-
'204':
67-
description: no content
79+
'200':
80+
description: echo of the source parameter
81+
content:
82+
application/json:
83+
schema:
84+
$ref: '#/components/schemas/Bar'
6885
6986
components:
7087
schemas:
7188
Foo:
7289
type: object
7390
properties:
74-
bar:
75-
type: string
91+
foo1:
92+
type: integer
93+
minimum: 0
94+
foo2:
95+
type: integer
96+
minimum: -10
97+
98+
Bar:
99+
type: object
100+
properties:
101+
bar1:
102+
type: integer
103+
bar2:
104+
type: integer
76105
----
77106

78-
and a `mapping.yaml` with annotation type mappings. This one uses endpoint specific annotation mappings to show it with and without parameters.
107+
and a `mapping.yaml` with annotation type mappings:
79108

80109
[source,yaml]
81110
----
82111
openapi-processor-mapping: v2
83112
84113
options:
85114
package-name: io.openapiprocessor.openapi
115+
javadoc: true
116+
format-code: true
117+
bean-validation: true
86118
87119
map:
120+
types:
121+
- type: Bar @ io.openapiprocessor.samples.validations.Sum(24) # <1>
88122
89-
paths:
90-
/foo:
91-
get:
92-
parameters:
93-
- type: Foo @ annotation.Bar()
94-
95-
post:
96-
parameters:
97-
- type: Foo @ annotation.Bar(bar = "foo", level = 42)
98-
# this does work too
99-
# - type: Foo @ annotation.Bar
100-
# - type: Foo @ annotation.Bar()
101-
# - type: Foo @ annotation.Bar("bar")
123+
parameters:
124+
- type: Foo @ io.openapiprocessor.samples.validations.Sum(value = 42) # <2>
125+
126+
# this formats do work too <3>
127+
# - type: Foo @ annotation.Bar
128+
# - type: Foo @ annotation.Bar()
129+
# - type: Foo @ annotation.Bar("bar")
130+
# - type: Foo @ annotation.Bar(value = "bar", foo = 2)
102131
----
103132

104-
To show the result with and without parameters the mappings are added only to the get/post http methods of the endpoint. We could call this an _endpoint http method annotation type mapping_ ;-).
133+
The `Sum` annotation in the example is a custom bean validation but the feature itself is not limited to bean validation.
134+
135+
<1> the `Bar` mapping is using a global type annotation mapping, so the annotation is added to the generated `Bar` class.
136+
<2> the `Foo` mapping adds the annotation to the parameter of the endpoint methods that use `Foo`.
137+
<3> this is a list of examples that shows annotation parameters. It is nearly java code.
105138

106-
Here is the generated interface:
139+
Here are the generated interfaces, first the `FooApi`:
107140

108141
[source,java]
109142
----
110143
package io.openapiprocessor.openapi.api;
111144
112-
import annotation.Bar;
113145
import io.openapiprocessor.openapi.model.Foo;
114-
import org.springframework.web.bind.annotation.GetMapping;
146+
import io.openapiprocessor.samples.validations.Sum;
147+
import javax.validation.Valid;
148+
import javax.validation.constraints.NotNull;
115149
import org.springframework.web.bind.annotation.PostMapping;
116150
import org.springframework.web.bind.annotation.RequestBody;
117151
@@ -120,22 +154,114 @@ public interface FooApi {
120154
/**
121155
* annotation mapping example.
122156
*
123-
* <p>a simple endpoint where an annotation mapping is used ona query parameter
157+
* <p>a simple endpoint where an annotation mapping is used on the request body
158+
*
159+
* @return echo of the source parameter
124160
*/
125-
@GetMapping(path = "/foo")
126-
void getFoo(@Bar Foo foo);
161+
@PostMapping(
162+
path = "/foo",
163+
consumes = {"application/json"},
164+
produces = {"application/json"})
165+
Foo postFoo(@RequestBody @Sum(value = 42) @Valid @NotNull Foo body); // <1>
166+
167+
}
168+
----
169+
<1> here is the additional annotation.
170+
171+
and the `BarApi` and the `Bar` class:
172+
173+
[source,java]
174+
----
175+
package io.openapiprocessor.openapi.api;
176+
177+
import io.openapiprocessor.openapi.model.Bar;
178+
import javax.validation.Valid;
179+
import javax.validation.constraints.NotNull;
180+
import org.springframework.web.bind.annotation.PostMapping;
181+
import org.springframework.web.bind.annotation.RequestBody;
182+
183+
public interface BarApi {
127184
128185
/**
129186
* annotation mapping example.
130187
*
131188
* <p>a simple endpoint where an annotation mapping is used on the request body
189+
*
190+
* @return echo of the source parameter
132191
*/
133192
@PostMapping(
134-
path = "/foo",
135-
consumes = {"application/json"})
136-
void postFoo(@RequestBody @Bar(bar = "foo", level = 42) Foo body);
193+
path = "/bar",
194+
consumes = {"application/json"},
195+
produces = {"application/json"})
196+
Bar postBar(@RequestBody @Valid @NotNull Bar body); // <1>
137197
138198
}
199+
----
139200

201+
[source,java]
202+
----
203+
package io.openapiprocessor.openapi.api;
204+
205+
import io.openapiprocessor.openapi.model.Bar;
206+
import javax.validation.Valid;
207+
import javax.validation.constraints.NotNull;
208+
import org.springframework.web.bind.annotation.PostMapping;
209+
import org.springframework.web.bind.annotation.RequestBody;
210+
211+
public interface BarApi {
212+
213+
/**
214+
* annotation mapping example.
215+
*
216+
* <p>a simple endpoint where an annotation mapping is used on the request body
217+
*
218+
* @return echo of the source parameter
219+
*/
220+
@PostMapping(
221+
path = "/bar",
222+
consumes = {"application/json"},
223+
produces = {"application/json"})
224+
Bar postBar(@RequestBody @Valid @NotNull Bar body); // <1>
225+
226+
}
140227
----
141228

229+
<1> no annotation here, mapping says it should be on the class:
230+
231+
[source,java]
232+
----
233+
package io.openapiprocessor.openapi.model;
234+
235+
import com.fasterxml.jackson.annotation.JsonProperty;
236+
import io.openapiprocessor.samples.validations.Sum;
237+
238+
@Sum(24) // <1>
239+
public class Bar {
240+
241+
@JsonProperty("bar1")
242+
private Integer bar1;
243+
244+
@JsonProperty("bar2")
245+
private Integer bar2;
246+
247+
public Integer getBar1() {
248+
return bar1;
249+
}
250+
251+
public void setBar1(Integer bar1) {
252+
this.bar1 = bar1;
253+
}
254+
255+
public Integer getBar2() {
256+
return bar2;
257+
}
258+
259+
public void setBar2(Integer bar2) {
260+
this.bar2 = bar2;
261+
}
262+
263+
}
264+
----
265+
266+
<1> and here it is :-)
267+

docs/modules/ROOT/partials/links.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
:oaps-bintray: https://bintray.com/openapi-processor/openapi-processor
1212
:oap-gradle: https://github.com/openapi-processor/openapi-processor-gradle
1313
:oap-playground: https://playground.openapiprocessor.io
14+
:oap-samples: https://github.com/openapi-processor/openapi-processor-samples
1415
:oap-central: https://search.maven.org/search?q=io.openapiprocessor
1516
:badge-central: https://img.shields.io/maven-central/v/io.openapiprocessor/openapi-processor-spring?label=Maven%20Central
1617
:oapc-inttests: https://github.com/openapi-processor/openapi-processor-core/tree/master/src/testInt/resources/tests

0 commit comments

Comments
 (0)