@@ -27,21 +27,22 @@ method.
2727- WebFlux support, may need its own generatr.
2828
2929
30-
3130# Status
3231
3332(November 2019) this is work in progress.
3433
3534current status & limitations:
3635
3736## status
37+
3838- generates interfaces & models for all endpoints
39+ - supports query parameters (i.e. ` in: query ` ) for basic and object types
40+ - supports responses with basic and object types
41+ - supports type mappings with generics (one level only)
3942
40- ## limitations
43+ ## known limitations
4144- property names in the openapi description must be java compatible (i.e. no ` @JsonProperty ` yet on model classes)
4245- limited parameter support
43- - query parameters (i.e. ` in: query ` )
44- - does handle basic data types and ` object ` s
4546 - no path parameters (i.e. ` in: path ` )
4647 - no header parameters (i.e. ` in: header ` )
4748 - no cookie parameters (i.e. ` in: cookie ` )
@@ -143,46 +144,6 @@ add a random collection type. The generatr does currently recognize the followin
143144- ` java.util.List `
144145- ` java.util.Set `
145146
146- #### ` x-java-type `
147-
148- > no longer supported
149- >
150- > an ` x-java-type ` extension looks like a simple solution to help the generatr create the expected
151- > code. But has a significant drawback. It has to be part of the api description. As an api provider
152- > I don't care which technology is used to access my api. So I don't want to add any technology specific
153- > details in the api description.
154-
155- The generatr does support an [ OpenAPI extension] [ openapi-spec-exts ] for array schemas. By adding the
156- ` x-java-type ` extension to the array schema it is possible to override the default:
157-
158- /array-collection:
159- get:
160- responses:
161- '200':
162- content:
163- application/vnd.collection:
164- schema:
165- type: array
166- x-java-type: java.util.Collection
167- items:
168- type: string
169- description: none
170-
171- The
172-
173- x-java-type: java.util.Collection
174-
175- line will change the endpoint to:
176-
177- @GetMapping(path = "/array-collection", produces = {"application/json"})
178- ResponseEntity<Collection<String>> getArrayCollection();
179-
180-
181- The generatr needs to know the given type to generate proper java code so we can't simply add a random
182- collection type. The generatr does currently recognize the following types:
183-
184- - ` java.util.Collection `
185-
186147## Endpoint Response
187148
188149All generated endpoints have a [ ` ResponseEntity<> ` ] [ spring-responseentity ] result. This allows an endpoint
@@ -191,8 +152,6 @@ it could just return its pojo result.
191152
192153## Endpoint Parameters
193154
194- todo
195-
196155### Query Parameters
197156
198157#### simple query parameters
@@ -219,89 +178,68 @@ will generate the following interface method:
219178 @GetMapping(path = "/endpoint")
220179 ResponseEntity<void> getEndpoint(@RequestParam(name = "foo", required = false, defaultValue = "not set") String foo);
221180
222- #### object query parameter
181+ # gradle
223182
224- > no longer supported, will be re-implemented using type mappings
183+ To use openapi-generatr-spring in a gradle project the gradle file of the project requires a few additional instructions.
225184
226- In case multiple query parameters should be mapped into a single model object like in this description :
185+ 1 . the generatr itself is a ` buildscript ` dependency :
227186
228- paths:
229- /endpoint:
230- get:
231- parameters:
232- - name: props
233- description: object via multiple query parameters
234- in: query
235- required: false
236- schema:
237- type: object
238- properties:
239- prop1:
240- type: string
241- prop2:
242- type: string
243- style: form
244- explode: true
245- responses:
246- '204':
247- description: empty
187+ buildscript {
188+ dependencies {
189+ // adds generatr-spring
190+ classpath 'com.github.hauner.openapi:openapi-generatr-spring:<version>'
191+ }
192+ }
248193
249- the generatr will by default create this interface method:
250-
251- @GetMapping(path = "/endpoint")
252- ResponseEntity<void> getEndpointObject(@RequestParam Props props);
194+ 2 . the [ openapi-generatr-gradle] [ generatr-gradle ] is activated in the ` plugins ` configuration:
253195
254- Default values for the object properties will be ignored even if defined. Springs [ ` @RequestParam ` ] [ spring-requestparam ]
255- annotation does not support default values for multiple properties. The generatr also ignores ` style ` & ` exploded `
256- which are mainly interesting for client side code generation.
257-
258- If the controller method should be one of the following variations
259-
260- @GetMapping(path = "/endpoint")
261- ResponseEntity<void> getEndpointObject(@RequestParam(name = "foo") Map foo);
196+ plugins {
197+ ....
198+ // add generatr-gradle plugin
199+ id 'com.github.hauner.openapi-generatr' version '<version>'
200+ }
201+
202+ 3 . the plugin will find the generatr on the build classpath and adds a ` generatrSpring ` configuration block that is
203+ used to configure the generatr.
262204
263- > used to to convert the query parameter ` foo ` to a map.
205+ generatrSpring {
206+ // the path to the open api yaml file.
207+ apiPath = "$projectDir/src/api/openapi.yaml"
264208
265- @GetMapping(path = "/endpoint")
266- ResponseEntity<void> getEndpointObject(@RequestParam Map<String,String> allParams);
267-
268- > populates the map with all query parameter names and values.
269-
270- @GetMapping(path = "/endpoint")
271- ResponseEntity<void> getEndpointObject(@RequestParam MultiValueMap<String,String> allParams);
272-
273- > same as the previous but can handle multiple query parameters of the same name
274-
275- the generatr needs some help to generate the proper code. Adding the ` x-type-java ` extension property to the schema
276- tells the generatr which Java type it should use.
277-
278- parameters:
279- - name: props
280- description: object via multiple query parameters
281- in: query
282- schema:
283- type: object
284- x-java-type: java.util.Map
285- properties:
286- prop1:
287- type: string
288- prop2:
289- type: string
290-
291- The possible values of ` x-type-java ` for query parameters are
292-
293- - ` java.util.Map `
294-
295- this should be used to convert a single query parameter to a map. It is the first variation above.
209+ // the destination folder for generating interfaces & models. This is the parent of the
210+ // {packageName} folder tree.
211+ targetDir = "$projectDir/build/openapi"
296212
297- - ` java.util.Map<> `
298-
299- this should be used to add all query parameter to a single map. It is the second variation above.
213+ // the root package of the generated interfaces/model. The package folder tree will be
214+ // created inside {targetDir}. Interfaces and models will be placed into the "api" and
215+ // "model" subpackages of packageName:
216+ // - interfaces => "${packageName}.api"
217+ // - models => "${packageName}.model"
218+ packageName = "com.github.hauner.openapi.sample"
219+
220+ // show warnings from the open api parser.
221+ showWarnings = true
222+
223+ // mapping if required (see java type mapping)
224+ typeMapping = "$projectDir/openapi-mapping.yaml"
225+ }
226+
227+ 4 . the plugin will also add a gradle task ` generateSpring ` to run the generatr.
228+
229+ 5 . to automatically generate & compile the generatr output the ` sourceSets ` are extended to include the generatr output
230+ and the ` compileJava ` task gets a dependency on ` generateSpring ` so the generatr runs before compilation:
300231
301- - ` org.springframework.util.MultiValueMap `
232+ sourceSets {
233+ main {
234+ java {
235+ // add generated files
236+ srcDir 'build/openapi'
237+ }
238+ }
239+ }
302240
303- this should be used to add all query parameter (which may have multiple parameters with the same name) to a single
304- multi value map. It is the third variation above.
241+ // generate api before compiling
242+ compileJava.dependsOn ('generateSpring')
305243
306244
307245# Sample
@@ -344,4 +282,5 @@ See the [existing integration tests][generatr-int-resources] for a couple of exa
344282[ spring-responseentity ] : https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/http/ResponseEntity.html
345283
346284[ generatr-int-resources ] : https://github.com/hauner/openapi-generatr-spring/tree/master/src/testInt/resources
285+ [ generatr-gradle ] : https://github.com/hauner/openapi-generatr-gradle
347286[ generatr-sample ] : https://github.com/hauner/openapi-generatr-spring-mvc-sample
0 commit comments