|
| 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 | + |
0 commit comments