|
| 1 | += Server Url |
| 2 | +include::partial$links.adoc[] |
| 3 | + |
| 4 | +== base path |
| 5 | + |
| 6 | +OpenAPI offers a `servers` section to describe the server urls available to access the api. |
| 7 | + |
| 8 | +openapi-processor has (simple) support to generate a resource property file with the path of a server url. The processor will resolve all variables with their default value and extracts the urls path. |
| 9 | + |
| 10 | +Given an OpenAPI description with a servers key: |
| 11 | + |
| 12 | +[source,yaml] |
| 13 | +---- |
| 14 | +openapi: 3.1.0 |
| 15 | +info: |
| 16 | + title: server url example |
| 17 | + version: 1.0.0 |
| 18 | +
|
| 19 | +servers: |
| 20 | + - url: "{schema}://{host}:{port}/{path}/v{version}" |
| 21 | + variables: |
| 22 | + schema: |
| 23 | + default: https |
| 24 | + enum: |
| 25 | + - https |
| 26 | + - http |
| 27 | + host: |
| 28 | + default: openapiprocessor.io |
| 29 | + port: |
| 30 | + default: "443" |
| 31 | + path: |
| 32 | + default: api |
| 33 | + version: |
| 34 | + default: "1" |
| 35 | +
|
| 36 | +# ... |
| 37 | +---- |
| 38 | + |
| 39 | +and a mapping |
| 40 | + |
| 41 | +[source,yaml] |
| 42 | +---- |
| 43 | +openapi-processor-mapping: v9 |
| 44 | +options: |
| 45 | + server-url: true |
| 46 | +---- |
| 47 | + |
| 48 | +it will generate a simple resource properties file with a single property `openapi.base.path`: |
| 49 | + |
| 50 | +[source,properties] |
| 51 | +---- |
| 52 | +# api.properties |
| 53 | +openapi.base.path = /api/v1 |
| 54 | +---- |
| 55 | + |
| 56 | +If there are multiple servers its index can be used to select it: |
| 57 | + |
| 58 | +[source,yaml] |
| 59 | +---- |
| 60 | +openapi-processor-mapping: v9 |
| 61 | +options: |
| 62 | + server-url: 0 # same as true |
| 63 | +# server-url: 1 |
| 64 | +# server-url: 2 |
| 65 | +---- |
| 66 | + |
| 67 | +== using the generated properties file |
| 68 | + |
| 69 | +The properties file is used to configure Spring Boots `server.servlet.context-path`: |
| 70 | + |
| 71 | +[source,properties] |
| 72 | +---- |
| 73 | +# application.properties |
| 74 | +
|
| 75 | +#spring.config.import = api.properties |
| 76 | +server.servlet.context-path=${openapi.base.path} |
| 77 | +---- |
| 78 | + |
| 79 | +While it is possible to import the generated properties file it is probably better to simply use the generated properties as an additional profile. |
| 80 | + |
| 81 | +== name of the properties file |
| 82 | + |
| 83 | +The default name of the generate properties file is `api.properties`. it is configurable using the xref:processor/configuration.adoc#_basepath_propertiesname[server-url:properties-name] option. |
| 84 | + |
| 85 | +[source,yaml] |
| 86 | +---- |
| 87 | +openapi-processor-mapping: v9 |
| 88 | +
|
| 89 | +options: |
| 90 | + server-url: |
| 91 | + properties-name: base-path.properties |
| 92 | +---- |
| 93 | + |
| 94 | +== destination directory |
| 95 | + |
| 96 | +By default, the processor will generate the java package structure directly below the `targetDir`. To creates the resource file it needs a second (`resources`) directory as target directory. |
| 97 | + |
| 98 | +This is handled by a new xref:processor/configuration.adoc#_target_dirlayout[option] to set the layout of the `targetDir`. Setting it to `standard` |
| 99 | + |
| 100 | +[source,yaml] |
| 101 | +---- |
| 102 | +openapi-processor-mapping: v9 |
| 103 | +
|
| 104 | +options: |
| 105 | + target-dir: |
| 106 | + layout: standard |
| 107 | +---- |
| 108 | + |
| 109 | +will create the following directory layout: |
| 110 | + |
| 111 | + targetDir |
| 112 | + +--- java |
| 113 | + | \--- io |
| 114 | + | \--- openapiprocessor |
| 115 | + | +--- api |
| 116 | + | \--- model |
| 117 | + \--- resources |
| 118 | + |
| 119 | +and write the properties file to the `resources` directory. |
| 120 | + |
| 121 | +[NOTE] |
| 122 | +To have a destination directory for generating the resource file, setting `server-url` to a truthy value will *automatically* enable the xref:processor/configuration.adoc#_target_dirlayout[`standard`] target dir layout. It is still recommended to set it explicitly for documentation. |
| 123 | + |
| 124 | +== build configuration |
| 125 | + |
| 126 | +Consequence of the new layout is that for compilation it is necessary to update configure of the sources and resources directories in the build configuration. |
| 127 | + |
| 128 | +For example with gradle the `sourceSets` configuration would change to something like this: |
| 129 | + |
| 130 | +[source,kotlin] |
| 131 | +---- |
| 132 | +sourceSets { |
| 133 | + main { |
| 134 | + java { |
| 135 | + // add generated files |
| 136 | + srcDir(layout.buildDirectory.dir("openapi/java")) |
| 137 | + } |
| 138 | + resources { |
| 139 | + // add generated resources |
| 140 | + srcDir(layout.buildDirectory.dir("openapi/resources")) |
| 141 | + } |
| 142 | + } |
| 143 | +} |
| 144 | +---- |
| 145 | + |
| 146 | +== options summary |
| 147 | + |
| 148 | +Here is a short snippet of a `mapping.yaml` as a summary of the options used to configure the base path property file generation. |
| 149 | + |
| 150 | +[source,yaml] |
| 151 | +---- |
| 152 | +openapi-processor-mapping: v9 |
| 153 | +
|
| 154 | +options: |
| 155 | + # ... other options |
| 156 | +
|
| 157 | + target-dir: |
| 158 | + layout: standard |
| 159 | +
|
| 160 | + base-path: |
| 161 | + server-url: 0 |
| 162 | + properties-name: base-path.properties |
| 163 | +---- |
0 commit comments