Skip to content

Commit c295dcc

Browse files
committed
create package from location (#115)
1 parent 1c1ea1b commit c295dcc

File tree

4 files changed

+25
-9
lines changed

4 files changed

+25
-9
lines changed

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import io.openapiprocessor.core.parser.RequestBody
2222
import io.openapiprocessor.core.parser.Response
2323
import io.openapiprocessor.core.processor.mapping.v2.ResultStyle
2424
import io.openapiprocessor.core.support.capitalizeFirstChar
25+
import io.openapiprocessor.core.support.toPackageName
2526
import io.openapiprocessor.core.writer.Identifier
2627
import org.slf4j.Logger
2728
import org.slf4j.LoggerFactory
@@ -124,16 +125,19 @@ class ApiConverter(
124125
return itf
125126
}
126127

127-
itf = Interface(
128-
targetInterfaceName,
129-
listOf(options.packageName, "api").joinToString("."),
130-
pathPrefix,
131-
identifier)
132-
128+
itf = Interface(targetInterfaceName, getPackageName(operation), pathPrefix, identifier)
133129
interfaces[targetInterfaceName] = itf
134130
return itf
135131
}
136132

133+
private fun getPackageName(operation: Operation): String {
134+
if (options.packageNameFromPath) {
135+
return toPackageName(operation.getDocumentUri(), options.packageName)
136+
} else {
137+
return listOf(options.packageName, "api").joinToString(".")
138+
}
139+
}
140+
137141
private fun createEndpoint(path: String, operation: Operation, dataTypes: DataTypes, resolver: RefResolver): Endpoint? {
138142
return try {
139143
val ctx = ApiConverterContext(

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@
55

66
package io.openapiprocessor.core.converter
77

8-
import io.openapiprocessor.core.converter.mapping.*
8+
import io.openapiprocessor.core.converter.mapping.TargetType
9+
import io.openapiprocessor.core.converter.mapping.TypeMapping
10+
import io.openapiprocessor.core.converter.mapping.UnknownDataTypeException
911
import io.openapiprocessor.core.converter.wrapper.NullDataTypeWrapper
1012
import io.openapiprocessor.core.model.DataTypeCollector
1113
import io.openapiprocessor.core.model.DataTypes
1214
import io.openapiprocessor.core.model.Documentation
1315
import io.openapiprocessor.core.model.datatypes.*
1416
import io.openapiprocessor.core.support.capitalizeFirstChar
17+
import io.openapiprocessor.core.support.toPackageName
1518
import io.openapiprocessor.core.writer.Identifier
1619
import java.util.*
1720

@@ -273,14 +276,22 @@ class DataTypeConverter(
273276

274277
return ObjectDataType(
275278
DataTypeName(schemaInfo.getName(), getTypeNameWithSuffix(schemaInfo.getName())),
276-
listOf(options.packageName, "model").joinToString("."),
279+
getPackageName(schemaInfo),
277280
properties = properties,
278281
constraints = constraints,
279282
deprecated = schemaInfo.getDeprecated(),
280283
documentation = Documentation(description = schemaInfo.description)
281284
)
282285
}
283286

287+
private fun getPackageName(schemaInfo: SchemaInfo): String {
288+
if (options.packageNameFromPath) {
289+
return toPackageName(schemaInfo.getDocumentUri(), options.packageName)
290+
} else {
291+
return listOf(options.packageName, "model").joinToString(".")
292+
}
293+
}
294+
284295
private fun createSimpleDataType(schemaInfo: SchemaInfo, dataTypes: DataTypes): DataType {
285296
val dataType = createSimpleDataTypeX(schemaInfo, dataTypes)
286297

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ open class DefaultWriterFactory(val options: ApiOptions): WriterFactory, InitWri
2929
private lateinit var resourcesPath: Path
3030

3131
override fun createWriter(packageName: String, className: String): Writer {
32-
if (packagePaths[packageName] == null) {
32+
if (options.packageNameFromPath && packagePaths[packageName] == null) {
3333
val pkg = packageName.substring(options.packageName.length + 1)
3434
val (name, path) = initTargetPackage(pkg)
3535
packagePaths[name] = path

openapi-processor-core/src/test/kotlin/io/openapiprocessor/core/writer/DefaultWriterFactorySpec.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ class DefaultWriterFactorySpec : StringSpec({
257257
"lazy initialize additional package folders" {
258258
options.targetDir = listOf(target.toString()).joinToString(File.separator)
259259
options.targetDirOptions.layout = TargetDirLayout.CLASSIC
260+
options.packageNameFromPath = true
260261

261262
val writerFactory = DefaultWriterFactory(options)
262263
writerFactory.init()

0 commit comments

Comments
 (0)