sda-commons-async-api¶
Experimental
Please be aware that SDA SE is likely to change or remove this artifact in the future
This module contains the AsyncApiGenerator
to generate AsyncAPI specs from a template and model classes in a
code first approach.
The AsyncAPI specification is the industry standard for defining asynchronous APIs.
Usage¶
If the code first approach is used to create an AsyncAPI spec this module provides assistance. The suggested way to use this module is:
- A template file defining the general
info
rmation,channels
andcomponents.messages
using the AsyncAPI spec.components.schemas
should be omitted. - The schema is defined and documented as Java classes in the code as they are used in message handlers and consumers. Jackson, Jakarta Validation and Swagger 2 annotations can be used for documentation.
- The root classes of messages are referenced in
components.messages.YourMessage.payload.$ref
asclass://your.package.MessageModel
. - The
AsyncApiGenerator
is used to combine the template and the generated Json Schema of the models to a self-contained spec file. - The generated AsyncAPI spec is committed into source control. This way, the commit history will show intended and unintended changes to the API and the API spec is accessible any time without executing any code.
- The API can be view in AsyncAPI Studio.
It is suggested to use it as a test dependency, build the AsyncAPI in a unit test and verify that it
is up-to-date.
The GoldenFileAssertions
from the test module help here.
Example: Build AsyncAPI for Cars
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
|
Usage with Existing Schemas¶
In some cases it is not possible to generate a schema with appropriate documentation, e.g. when a framework requires to use classes from dependencies that do not contain the expected annotations.
In this case the schema may be added to the template. This should be used as fallback only, because the schema is not connected to the actual code, it may diverge over time.
Example: Build AsyncAPI with handcrafted schema
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
|
1 |
|
Generating Schema Files¶
If desired, the module also allows to generate the JSON schema files, for example to use them to
validate test data.
Please take a look at JsonSchemaBuilder
and it's implementations.