Skip to content

Commit 9ba82fb

Browse files
authored
Merge pull request #75 from hauner/#74
resolves #74
2 parents 52a30e3 + 3a91875 commit 9ba82fb

File tree

5 files changed

+88
-14
lines changed

5 files changed

+88
-14
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: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class SpringGeneratr implements OpenApiGeneratr {
7878
new DataTypeWriter(
7979
headerWriter: headerWriter,
8080
beanValidationFactory: beanValidationFactory,
81-
apiOptions: options,
81+
apiOptions: options
8282
),
8383
new StringEnumWriter(headerWriter: headerWriter)
8484
)
@@ -87,18 +87,44 @@ 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
93+
94+
if (generatrOptions.containsKey ('mapping')) {
95+
mapping = reader.read (generatrOptions.mapping as String)
96+
97+
} else if (generatrOptions.containsKey ('typeMappings')) {
98+
mapping = reader.read (generatrOptions.typeMappings as String)
99+
println "warning: 'typeMappings' option is deprecated, use 'mapping'!"
100+
}
101+
102+
def options = new ApiOptions ()
91103
options.apiPath = generatrOptions.apiPath
92104
options.targetDir = generatrOptions.targetDir
93-
options.packageName = generatrOptions.packageName
94105

95-
def reader = new MappingReader ()
96-
def converter = new MappingConverter()
97-
def mapping = reader.read (generatrOptions.typeMappings as String)
106+
if (generatrOptions.packageName) {
107+
options.packageName = generatrOptions.packageName
108+
println "warning: 'options:package-name' should be set in the mapping yaml!"
109+
}
110+
111+
if (generatrOptions.containsKey ('beanValidation')) {
112+
options.beanValidation = generatrOptions.beanValidation
113+
println "warning: 'options:bean-validation' should be set in the mapping yaml!"
114+
}
115+
98116
if (mapping) {
117+
if (mapping?.options?.packageName != null) {
118+
options.packageName = mapping.options.packageName
119+
}
120+
121+
if (mapping?.options?.beanValidation != null) {
122+
options.beanValidation = mapping.options.beanValidation
123+
}
124+
99125
options.typeMappings = converter.convert (mapping)
100126
}
101-
options.beanValidation = generatrOptions.beanValidation != null && generatrOptions.beanValidation == true
127+
102128
options
103129
}
104130

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)