|
| 1 | +--- |
| 2 | +sidebar_position: 4 |
| 3 | +--- |
| 4 | + |
| 5 | +import Tabs from '@theme/Tabs'; |
| 6 | +import TabItem from '@theme/TabItem'; |
| 7 | +import CodeBlock from '@theme/CodeBlock'; |
| 8 | +import CodeSchemaGroovy from '!!raw-loader!./snippets/_schema_groovy.md'; |
| 9 | +import CodeSchemaMaven from '!!raw-loader!./snippets/_schema_maven.md'; |
| 10 | + |
| 11 | +# Documenting Schemas |
| 12 | + |
| 13 | +Under the hood springwolf relies on swagger-core `ModelConverters`. |
| 14 | + |
| 15 | +By default, the type and example values for the properties are guessed. |
| 16 | +The default Jackson `ModelResolver` supports schema definitions via `@Schema` to overwrite the property definitions. |
| 17 | + |
| 18 | +## Using `@Schema` |
| 19 | + |
| 20 | +The `@Schema` annotation allows to set many properties like `description`, `example`, `required` to document payloads. |
| 21 | + |
| 22 | +All properties are part of the produced AsyncApi file. However, not all of them are displayed in springwolf-ui. The ones listed above are included. |
| 23 | + |
| 24 | +### Usage |
| 25 | + |
| 26 | +Add the following dependency: |
| 27 | + |
| 28 | +<Tabs> |
| 29 | + <TabItem value="Groovy" label="Groovy" default> |
| 30 | + <CodeBlock language="groovy">{CodeSchemaGroovy}</CodeBlock> |
| 31 | + </TabItem> |
| 32 | + <TabItem value="Maven" label="Maven"> |
| 33 | + <CodeBlock language="xml">{CodeSchemaMaven}</CodeBlock> |
| 34 | + </TabItem> |
| 35 | +</Tabs> |
| 36 | + |
| 37 | +Then, add the `@Schema` annotation to the payload class: |
| 38 | +```java |
| 39 | +import io.swagger.v3.oas.annotations.media.Schema; |
| 40 | + |
| 41 | +@Schema(description = "Example payload model") |
| 42 | +public class ExamplePayloadDto { |
| 43 | + @Schema(description = "Some string field", example = "some string value", required = true) |
| 44 | + private String someString; |
| 45 | + |
| 46 | + public String getSomeString() { |
| 47 | + return someString; |
| 48 | + } |
| 49 | +} |
| 50 | +``` |
| 51 | + |
| 52 | +For a full example, take a look at [ExamplePayloadDto.java in springwolf-amqp-example](https://github.com/springwolf/springwolf-core/blob/master/springwolf-examples/springwolf-amqp-example/src/main/java/io/github/stavshamir/springwolf/example/dtos/ExamplePayloadDto.java) |
| 53 | + |
| 54 | +## Custom ModelConverters |
| 55 | + |
| 56 | +Additionally, custom `ModelConverters` are supported. |
| 57 | +These are needed when swagger is unable to extract a schema from a class. |
| 58 | + |
| 59 | +One example is `javax.money.MonetaryAmount`. |
| 60 | +Adding a model converter is demoed in [springwolf-add-ons/springwolf-common-model-converters](https://github.com/springwolf/springwolf-core/tree/master/springwolf-add-ons/springwolf-common-model-converters) |
0 commit comments