Skip to content

Commit 9807703

Browse files
committed
group target-dir/base-path options (#176)
1 parent 7b66616 commit 9807703

File tree

20 files changed

+262
-77
lines changed

20 files changed

+262
-77
lines changed

openapi-processor-core/src/main/kotlin/io/openapiprocessor/core/converter/ApiConverter.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class ApiConverter(
7777
}
7878

7979
private fun getServerPath(api: OpenApi): String? {
80-
if (!options.pathPrefix) {
80+
if (!options.basePathOptions.enabled) {
8181
return null
8282
}
8383

@@ -86,7 +86,7 @@ class ApiConverter(
8686
return null
8787
}
8888

89-
return servers[options.pathPrefixServerIndex!!].getUri().path
89+
return servers[options.basePathOptions.serverUrl!!].getUri().path
9090
}
9191

9292
private fun createInterface(
@@ -197,7 +197,8 @@ class ApiConverter(
197197
parameter.getName(),
198198
"",
199199
parameter.getSchema(),
200-
resolver)
200+
resolver,
201+
"parameters")
201202

202203
val dataType = convertDataType(info, dataTypes)
203204

openapi-processor-core/src/main/kotlin/io/openapiprocessor/core/converter/ApiOptions.kt

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
package io.openapiprocessor.core.converter
77

88
import io.openapiprocessor.core.converter.mapping.*
9+
import io.openapiprocessor.core.converter.options.BasePathOptions
10+
import io.openapiprocessor.core.converter.options.TargetDirOptions
911
import io.openapiprocessor.core.support.Empty
1012

1113
/**
@@ -14,25 +16,15 @@ import io.openapiprocessor.core.support.Empty
1416
class ApiOptions: MappingSettings {
1517

1618
/**
17-
* the destination folder for generating interfaces & models. This is the parent of the
19+
* the destination folder for generating interfaces & DTOs. This is the parent of the
1820
* {@link #packageName} folder tree.
1921
*/
2022
var targetDir: String? = null
2123

2224
/**
23-
* enable/disable clearing of [targetDir] (optional).
25+
* target-dir related options.
2426
*/
25-
var clearTargetDir = true
26-
27-
/**
28-
* the layout of the target dir
29-
*
30-
* classic: targetDir/packages...
31-
* standard: targetDir/java, targetDir/resources
32-
*/
33-
var targetDirLayout: String = "classic"
34-
35-
val standardLayout: Boolean get() = TargetDirLayout.isStandard(targetDirLayout)
27+
var targetDirOptions: TargetDirOptions = TargetDirOptions()
3628

3729
/**
3830
* the root package of the generated interfaces/model. The package folder tree will be created
@@ -116,14 +108,9 @@ class ApiOptions: MappingSettings {
116108
var jsonPropertyAnnotation = JsonPropertyAnnotationMode.Always
117109

118110
/**
119-
* enable/disable use of server url (optional).
120-
*/
121-
var pathPrefix = false
122-
123-
/**
124-
* index of the server to use as server prefix (optional).
111+
* base path related options
125112
*/
126-
var pathPrefixServerIndex: Int? = null
113+
var basePathOptions: BasePathOptions = BasePathOptions()
127114

128115
/**
129116
* provide additional type mapping information to map OpenAPI types to java types.

openapi-processor-core/src/main/kotlin/io/openapiprocessor/core/converter/OptionsConverter.kt

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
package io.openapiprocessor.core.converter
77

8+
import io.openapiprocessor.core.converter.options.TargetDirLayout
89
import io.openapiprocessor.core.processor.MappingConverter
910
import io.openapiprocessor.core.processor.MappingReader
1011
import io.openapiprocessor.core.processor.mapping.MappingVersion
@@ -57,7 +58,8 @@ class OptionsConverter(private val checkObsoleteProcessorOptions: Boolean = fals
5758

5859
is MappingV2 -> {
5960
with(mapping.options) {
60-
options.clearTargetDir = clearTargetDir
61+
options.targetDirOptions.clear = targetDir.clear ?: clearTargetDir
62+
options.targetDirOptions.layout = TargetDirLayout.from(targetDir.layout)
6163
}
6264

6365
options.packageName = mapping.options.packageName
@@ -70,8 +72,9 @@ class OptionsConverter(private val checkObsoleteProcessorOptions: Boolean = fals
7072
options.beanValidationFormat = format
7173

7274
val (enablePathPrefix, pathPrefixServerIndex) = checkServerUrl(mapping.options)
73-
options.pathPrefix = enablePathPrefix
74-
options.pathPrefixServerIndex = pathPrefixServerIndex
75+
options.basePathOptions.enabled = enablePathPrefix
76+
options.basePathOptions.serverUrl = pathPrefixServerIndex
77+
options.basePathOptions.profileName = mapping.options.basePath.profileName
7578

7679
options.javadoc = mapping.options.javadoc
7780
options.oneOfInterface = mapping.options.oneOfInterface
@@ -111,10 +114,10 @@ class OptionsConverter(private val checkObsoleteProcessorOptions: Boolean = fals
111114
}
112115

113116
private fun checkServerUrl(options: Options): Pair<Boolean, Int?> {
114-
return when (options.serverUrl) {
117+
return when (options.basePath.serverUrl) {
115118
"false" -> Pair(false, null)
116119
"true" -> Pair(true, 0)
117-
else -> Pair(true, options.serverUrl.toInt())
120+
else -> Pair(true, options.basePath.serverUrl?.toInt())
118121
}
119122
}
120123

@@ -137,6 +140,5 @@ class OptionsConverter(private val checkObsoleteProcessorOptions: Boolean = fals
137140
log.warn("'typeMappings' option is deprecated, please use 'mapping'!")
138141
}
139142
}
140-
141143
}
142144

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright 2024 https://github.com/openapi-processor/openapi-processor-core
3+
* PDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.openapiprocessor.core.converter.options
7+
8+
class BasePathOptions {
9+
/**
10+
* enable/disable base path setup
11+
*/
12+
var enabled = false
13+
14+
/**
15+
* server-url index to use as base path.
16+
*/
17+
var serverUrl: Int? = null
18+
19+
/**
20+
* profile name for base path.
21+
*/
22+
var profileName: String = "api.properties"
23+
}

openapi-processor-core/src/main/kotlin/io/openapiprocessor/core/converter/TargetDirLayout.kt renamed to openapi-processor-core/src/main/kotlin/io/openapiprocessor/core/converter/options/TargetDirLayout.kt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* PDX-License-Identifier: Apache-2.0
44
*/
55

6-
package io.openapiprocessor.core.converter
6+
package io.openapiprocessor.core.converter.options
77

88
enum class TargetDirLayout {
99
CLASSIC, // targetDir/{packages}
@@ -13,5 +13,17 @@ enum class TargetDirLayout {
1313
fun isStandard(layout: String): Boolean {
1414
return STANDARD.name.lowercase() == layout
1515
}
16+
17+
fun isStandard(layout: TargetDirLayout): Boolean {
18+
return STANDARD == layout
19+
}
20+
21+
fun from(layout: String): TargetDirLayout {
22+
return valueOf(layout.uppercase())
23+
}
24+
}
25+
26+
fun isStandard(): Boolean {
27+
return this == STANDARD
1628
}
1729
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright 2024 https://github.com/openapi-processor/openapi-processor-core
3+
* PDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.openapiprocessor.core.converter.options
7+
8+
import io.openapiprocessor.core.converter.options.TargetDirLayout.Companion.isStandard
9+
10+
class TargetDirOptions {
11+
/**
12+
* enable/disable clearing of targetDir (optional).
13+
*/
14+
var clear = true
15+
16+
/**
17+
* the layout of the target dir
18+
*
19+
* classic: targetDir/packages...
20+
* standard: targetDir/java, targetDir/resources
21+
*/
22+
var layout = TargetDirLayout.CLASSIC
23+
24+
val standardLayout: Boolean get() = isStandard(layout)
25+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* Copyright 2024 https://github.com/openapi-processor/openapi-processor-core
3+
* PDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.openapiprocessor.core.processor.mapping.v2
7+
8+
data class BasePath(
9+
/**
10+
* server-url index to use as base path.
11+
*/
12+
val serverUrl: String = "false",
13+
14+
/**
15+
* profile name for base path.
16+
*/
17+
val profileName: String = "api.properties"
18+
)

openapi-processor-core/src/main/kotlin/io/openapiprocessor/core/processor/mapping/v2/Options.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ data class Options(
2727
*/
2828
val clearTargetDir: Boolean = true,
2929

30+
/**
31+
* target-dir related options.
32+
*/
33+
val targetDir: TargetDir = TargetDir(),
34+
3035
/**
3136
* bean validation (optional)
3237
*/
@@ -53,9 +58,9 @@ data class Options(
5358
val modelNameSuffix: String = String.Empty,
5459

5560
/**
56-
* generate interfaces with pth prefix (optional)
61+
* base path related options (optional)
5762
*/
58-
val serverUrl: String = "false",
63+
val basePath: BasePath = BasePath(),
5964

6065
/**
6166
* generate common interface for an `oneOf` object list (optional)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright 2024 https://github.com/openapi-processor/openapi-processor-core
3+
* PDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.openapiprocessor.core.processor.mapping.v2
7+
8+
data class TargetDir(
9+
/**
10+
* enable/disable clearing of targetDir (optional).
11+
*/
12+
val clear: Boolean? = null,
13+
14+
/**
15+
* layout of targetDir.
16+
* classic: targetDir/packages
17+
* standard: targetDir/java/packages & targetDir/resources
18+
*/
19+
val layout: String = "classic"
20+
)

openapi-processor-core/src/main/kotlin/io/openapiprocessor/core/writer/DefaultWriterFactory.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
package io.openapiprocessor.core.writer
77

88
import io.openapiprocessor.core.converter.ApiOptions
9-
import io.openapiprocessor.core.converter.TargetDirLayout.Companion.isStandard
109
import io.openapiprocessor.core.support.toURI
1110
import io.openapiprocessor.core.writer.java.PathWriter
1211
import org.slf4j.Logger
@@ -41,7 +40,7 @@ open class DefaultWriterFactory(val options: ApiOptions): WriterFactory, InitWri
4140
val pkgPaths = HashMap<String, Path>()
4241

4342
log.debug ("initializing target folders")
44-
if (options.clearTargetDir) {
43+
if (options.targetDirOptions.clear) {
4544
clearTargetDir()
4645
}
4746

@@ -64,7 +63,7 @@ open class DefaultWriterFactory(val options: ApiOptions): WriterFactory, InitWri
6463
log.debug("initialized target folder: {}", validationPath.toAbsolutePath().toString())
6564
}
6665

67-
if (options.standardLayout) {
66+
if (options.targetDirOptions.standardLayout) {
6867
resourcesPath = initTargetResources()
6968
log.debug("initialized target folder: {}", resourcesPath.toAbsolutePath().toString())
7069
}
@@ -107,7 +106,7 @@ open class DefaultWriterFactory(val options: ApiOptions): WriterFactory, InitWri
107106

108107
private fun createTargetPackage(apiPkg: String): Path {
109108
val items = mutableListOf(options.targetDir)
110-
if (isStandard(options.targetDirLayout)) {
109+
if (options.targetDirOptions.layout.isStandard()) {
111110
items.add("java")
112111
}
113112
items.add(apiPkg)

openapi-processor-core/src/main/kotlin/io/openapiprocessor/core/writer/java/InterfaceWriter.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class InterfaceWriter(
4242

4343
generatedWriter.writeUse(target)
4444

45-
if (apiOptions.pathPrefix && itf.hasPathPrefix()) {
45+
if (apiOptions.basePathOptions.enabled && itf.hasPathPrefix()) {
4646
annotationWriter.write(target, getPrefixAnnotation(itf.getPathPrefix()))
4747
target.write("\n")
4848
}
@@ -68,7 +68,7 @@ class InterfaceWriter(
6868
imports.addAll(annotation.imports)
6969
imports.addAll(annotation.referencedImports)
7070

71-
if (apiOptions.pathPrefix && itf.hasPathPrefix()) {
71+
if (apiOptions.basePathOptions.enabled && itf.hasPathPrefix()) {
7272
imports.addAll(getPrefixAnnotation().imports)
7373
}
7474

openapi-processor-core/src/main/resources/mapping/v9/mapping.example.yaml

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@ options: # general processor options [required]
2222
# suffix for model class names and enum names. Default is none.
2323
model-name-suffix: Resource
2424

25-
# path prefix from server-url
26-
server-url: 0
27-
2825
# default (i.e pojo) or record
2926
model-type: default
3027

@@ -46,6 +43,17 @@ options: # general processor options [required]
4643
# control @JsonProperty annotation (always|auto|never)
4744
json-property-annotation: always
4845

46+
# target-dir related configuration
47+
target-dir:
48+
clear: true
49+
layout: standard
50+
51+
# server-url related configuration
52+
base-path:
53+
# false, true = 0, 1, ..
54+
prefix: 0
55+
# property resource with the uri base path
56+
properties-name: openapi.properties
4957

5058
compatibility:
5159
bean-validation-valid-on-reactive: false

0 commit comments

Comments
 (0)