Skip to content

Commit 8574933

Browse files
committed
add generatr options to mapping yaml
1 parent 52a30e3 commit 8574933

File tree

5 files changed

+79
-13
lines changed

5 files changed

+79
-13
lines changed

src/main/groovy/com/github/hauner/openapi/spring/converter/ApiOptions.groovy

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ class ApiOptions {
4646
*/
4747
String packageName
4848

49+
/**
50+
* provide enabling Bean Validation (JSR303) annotations. Default is false (disabled)
51+
*/
52+
boolean beanValidation = false
53+
4954
/**
5055
* provide additional type mapping information to map OpenAPI types to java types. The list can
5156
* contain the following mappings:
@@ -59,9 +64,4 @@ class ApiOptions {
5964
*/
6065
List<Mapping> typeMappings
6166

62-
/**
63-
* provide enabling Bean Validation (JSR303) annotations. Default is false (disabled)
64-
*/
65-
boolean beanValidation = false;
66-
6767
}

src/main/groovy/com/github/hauner/openapi/spring/generatr/SpringGeneratr.groovy

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,18 +87,36 @@ class SpringGeneratr implements OpenApiGeneratr {
8787
}
8888

8989
private ApiOptions convertOptions (Map<String, ?> generatrOptions) {
90-
def options = new ApiOptions()
90+
def reader = new MappingReader ()
91+
def converter = new MappingConverter ()
92+
def mapping = reader.read (generatrOptions.typeMappings as String)
93+
94+
def options = new ApiOptions ()
9195
options.apiPath = generatrOptions.apiPath
9296
options.targetDir = generatrOptions.targetDir
93-
options.packageName = generatrOptions.packageName
9497

95-
def reader = new MappingReader ()
96-
def converter = new MappingConverter()
97-
def mapping = reader.read (generatrOptions.typeMappings as String)
98+
if (generatrOptions.packageName) {
99+
options.packageName = generatrOptions.packageName
100+
println "warning: 'options:package-name' should be set in the mapping yaml!"
101+
}
102+
103+
if (generatrOptions.containsKey ('beanValidation')) {
104+
options.beanValidation = generatrOptions.beanValidation
105+
println "warning: 'options:bean-validation' should be set in the mapping yaml!"
106+
}
107+
98108
if (mapping) {
109+
if (mapping?.options?.packageName != null) {
110+
options.packageName = mapping.options.packageName
111+
}
112+
113+
if (mapping?.options?.beanValidation != null) {
114+
options.beanValidation = mapping.options.beanValidation
115+
}
116+
99117
options.typeMappings = converter.convert (mapping)
100118
}
101-
options.beanValidation = generatrOptions.beanValidation != null && generatrOptions.beanValidation == true
119+
102120
options
103121
}
104122

src/main/groovy/com/github/hauner/openapi/spring/generatr/mapping/Mapping.groovy

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,12 @@ class Mapping {
2929
String openapiGeneratrSpring
3030

3131
/**
32-
* the mappings
32+
* general options
33+
*/
34+
Options options
35+
36+
/**
37+
* the type mappings
3338
*/
3439
Map map
3540

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2020 the original authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.github.hauner.openapi.spring.generatr.mapping
18+
19+
/**
20+
* general options
21+
*
22+
* @author Martin Hauner
23+
*/
24+
class Options {
25+
26+
/**
27+
* the root package name of the generated interfaces & models (required)
28+
*
29+
* Interfaces and models will be generated into the `api` and `model` subpackages of `packageName`.
30+
* - so the final package name of the generated interfaces will be `"${packageName}.api"`
31+
* - and the final package name of the generated models will be `"${packageName}.model"`
32+
*/
33+
String packageName
34+
35+
/**
36+
* bean validation (optional)
37+
*/
38+
Boolean beanValidation
39+
40+
}

src/test/groovy/com/github/hauner/openapi/spring/generatr/MappingExampleSpec.groovy

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ class MappingExampleSpec extends Specification {
2929

3030

3131
String yaml = """
32+
options:
33+
package-name: com.github.hauner.openapi
34+
bean-validation: true
35+
3236
map:
3337
types:
3438
- from: array
@@ -55,7 +59,6 @@ map:
5559
to: com.github.hauner.openapi.FooBar
5660
5761
paths:
58-
# not implemented
5962
/first:
6063
exclude: true
6164

0 commit comments

Comments
 (0)