Skip to content

Commit 6c48d1f

Browse files
committed
add server url documentation (openapi-processor/openapi-processor-base#176)
1 parent 82eb27f commit 6c48d1f

2 files changed

Lines changed: 177 additions & 1 deletion

File tree

docs/modules/ROOT/pages/processor/configuration.adoc

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ Interfaces and models will be generated into the `api` and `model` subpackages o
5454
* and the final package name of the generated models will be `"$\{package-name\}.model"`
5555

5656
==== example
57+
5758
[source,yaml]
5859
----
5960
openapi-processor-mapping: v9
@@ -305,7 +306,7 @@ options:
305306

306307
=== base-path
307308

308-
parent key to group base path related options.
309+
parent key to group base path related options. See xref:processor/server-url.adoc[].
309310

310311
[#_base_pathserver_url]
311312
=== base-path:server-url
@@ -341,6 +342,7 @@ parent key to group `targetDir` related options.
341342

342343
enable or disable clearing of the `targetDir` when the processor is writing the generated files (See also xref:_clear_target_dir[]).
343344

345+
[#_target_dirlayout]
344346
=== target-dir:layout
345347

346348
**optional** (string, `classic` or `standard`, default is `classic`)
@@ -365,6 +367,17 @@ this controls if the `targetDir` is the source root folder or if there is anothe
365367
| \--- model
366368
\--- resources
367369

370+
==== example
371+
372+
[source,yaml]
373+
----
374+
openapi-processor-mapping: v9
375+
376+
options:
377+
target-dir:
378+
layout: standard
379+
----
380+
368381
== compatibility:
369382

370383
This section contains keys to disable breaking changes.
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
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

Comments
 (0)