Skip to content

Commit b6b809f

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

File tree

4 files changed

+154
-0
lines changed

4 files changed

+154
-0
lines changed

docs/modules/ROOT/nav.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
** xref:mapping/result-style.adoc[Global Result Style]
2121
** xref:mapping/single-multi.adoc[Global Single & Multi Mapping]
2222
** xref:mapping/endpoint.adoc[Endpoint Mapping]
23+
** xref:mapping/annotation.adoc[Additional Annotation]
2324
** xref:mapping/null.adoc[Null Mapping]
2425
** xref:mapping/package-name.adoc[package-name Mapping]
2526
* xref:gradle.adoc[Gradle Integration]
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
= annotations
2+
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+
5+
== additional parameter annotations
6+
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.
8+
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.
10+
11+
The annotation type mapping is similar to other mappings and is defined like this:
12+
13+
[source,yaml]
14+
----
15+
- type: {source type} @ {annotation type}
16+
----
17+
18+
* **type** is required.
19+
20+
** **{source type}** is the type name used in the OpenAPI description and names the type that should
21+
receive the additional annotation.
22+
23+
** **{annotation type}** is the fully qualified class name of the java annotation type. It may have parameters (see example below).
24+
25+
26+
== mapping example
27+
28+
Given the following OpenAPI description:
29+
30+
[source,yaml]
31+
----
32+
openapi: 3.0.3
33+
info:
34+
title: openapi-processor-spring sample api
35+
version: 1.0.0
36+
37+
paths:
38+
/foo:
39+
get:
40+
tags:
41+
- foo
42+
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'
49+
responses:
50+
'204':
51+
description: no content
52+
53+
/foo-body:
54+
post:
55+
tags:
56+
- foo
57+
summary: annotation mapping example.
58+
description: a simple endpoint where an annotation mapping is used
59+
requestBody:
60+
content:
61+
application/json:
62+
schema:
63+
$ref: '#/components/schemas/Foo'
64+
required: true
65+
responses:
66+
'204':
67+
description: no content
68+
69+
components:
70+
schemas:
71+
Foo:
72+
type: object
73+
properties:
74+
bar:
75+
type: string
76+
----
77+
78+
and a `mapping.yaml` with annotation type mappings. This one uses endpoint specific annotation mappings to show it with and without parameters.
79+
80+
[source,yaml]
81+
----
82+
openapi-processor-mapping: v2
83+
84+
options:
85+
package-name: io.openapiprocessor.openapi
86+
87+
map:
88+
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")
102+
----
103+
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_ ;-).
105+
106+
Here is the generated interface:
107+
108+
[source,java]
109+
----
110+
package io.openapiprocessor.openapi.api;
111+
112+
import annotation.Bar;
113+
import io.openapiprocessor.openapi.model.Foo;
114+
import org.springframework.web.bind.annotation.GetMapping;
115+
import org.springframework.web.bind.annotation.PostMapping;
116+
import org.springframework.web.bind.annotation.RequestBody;
117+
118+
public interface FooApi {
119+
120+
/**
121+
* annotation mapping example.
122+
*
123+
* <p>a simple endpoint where an annotation mapping is used ona query parameter
124+
*/
125+
@GetMapping(path = "/foo")
126+
void getFoo(@Bar Foo foo);
127+
128+
/**
129+
* annotation mapping example.
130+
*
131+
* <p>a simple endpoint where an annotation mapping is used on the request body
132+
*/
133+
@PostMapping(
134+
path = "/foo",
135+
consumes = {"application/json"})
136+
void postFoo(@RequestBody @Bar(bar = "foo", level = 42) Foo body);
137+
138+
}
139+
140+
----
141+

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ map:
3535
# add a (usually technical) parameter that is not described in the OpenAPI
3636
- add: {parameter name} => {target type}
3737
38+
# add an extra annotation to parameters of type source type
39+
- type: {source type} @ {annotation type}
40+
3841
# list of path specific content mappings, mapped by content type
3942
responses:
4043
- content: {content type} => {target type}
@@ -64,6 +67,9 @@ map:
6467
# add a (usually technical) parameter that is not described in the OpenAPI
6568
- add: {parameter name} => {target type}
6669
70+
# add an extra annotation to parameters of type source type
71+
- type: {source type} @ {annotation type}
72+
6773
# list of path specific content mappings, mapped by content type
6874
responses:
6975
- content: {content type} => {target type}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ map:
6565
# add a (usually technical) parameter that is not described in the OpenAPI
6666
- add: {parameter name} => {target type}
6767
68+
# add an extra annotation to parameters of type source type
69+
- type: {source type} @ {annotation type}
70+
6871
# list of global content mappings, mapped by content type
6972
responses:
7073
- content: {content type} => {target type}
@@ -100,6 +103,9 @@ map:
100103
# add a (usually technical) parameter that is not described in the OpenAPI
101104
- add: {parameter name} => {target type}
102105
106+
# add an extra annotation to parameters of type source type
107+
- type: {source type} @ {annotation type}
108+
103109
# list of path specific content mappings, mapped by content type
104110
responses:
105111
- content: {content type} => {target type}

0 commit comments

Comments
 (0)