File tree Expand file tree Collapse file tree 2 files changed +91
-0
lines changed
openapi-processor-core/src
main/kotlin/io/openapiprocessor/core/writer/java
test/kotlin/io/openapiprocessor/core/writer/java Expand file tree Collapse file tree 2 files changed +91
-0
lines changed Original file line number Diff line number Diff line change 1+ /*
2+ * Copyright 2025 https://github.com/openapi-processor/openapi-processor-base
3+ * PDX-License-Identifier: Apache-2.0
4+ */
5+
6+ package io.openapiprocessor.core.writer.java
7+
8+ import io.openapiprocessor.core.converter.options.PackageOptions
9+ import java.net.URI
10+
11+ class PackageSelector (private val options : PackageOptions ) {
12+
13+ fun getPackageName (subPackage : String ): String {
14+ return toPackageName(subPackage)
15+ }
16+
17+ fun getPackageName (location : URI , subPackage : String ): String {
18+ if (options.location == null ) {
19+ // create package from packageName option
20+ return toPackageName(subPackage)
21+ }
22+
23+ val locationPackage = toPackage(location)
24+ val childPackage = getChildPackage(locationPackage)
25+ if (childPackage != null ) {
26+ return childPackage
27+ }
28+
29+ return toPackageName(subPackage)
30+ }
31+
32+ private fun toPackageName (subPackage : String ): String {
33+ return listOf (options.base, subPackage).joinToString(" ." )
34+ }
35+
36+ private fun toPackage (location : URI ): String {
37+ return location
38+ .resolve(" ." )
39+ .path
40+ .replace(" /" , " ." )
41+ .dropLast(1 ) // file name
42+ }
43+
44+ private fun getChildPackage (locationPackage : String ): String? {
45+ val childIndex = locationPackage.indexOf(options.location!! )
46+ if (childIndex == - 1 ) {
47+ return null
48+ }
49+
50+ val pkgName = locationPackage.substring(childIndex)
51+ return pkgName
52+ }
53+ }
Original file line number Diff line number Diff line change 1+ /*
2+ * Copyright 2025 https://github.com/openapi-processor/openapi-processor-base
3+ * PDX-License-Identifier: Apache-2.0
4+ */
5+
6+ package io.openapiprocessor.core.writer.java
7+
8+ import io.kotest.core.spec.style.StringSpec
9+ import io.kotest.matchers.shouldBe
10+ import io.openapiprocessor.core.converter.options.PackageOptions
11+ import java.net.URI
12+
13+ class PackageSelectorSpec : StringSpec ({
14+
15+ " crates package name from base" {
16+ val options = PackageOptions ().apply {
17+ base = " io.openapiprocessor.base"
18+ location = null
19+ }
20+
21+ val calc = PackageSelector (options)
22+ val pkg = calc.getPackageName(URI ("project/src/api/openapi.yaml"), "api")
23+
24+ pkg.shouldBe("io.openapiprocessor.base.api")
25+ }
26+
27+ " creates package name from location" {
28+ val options = PackageOptions ().apply {
29+ base = " io.openapiprocessor.base"
30+ location = " io.openapiprocessor.location"
31+ }
32+
33+ val calc = PackageSelector (options)
34+ val pkg = calc.getPackageName(URI ("project/src/java/io.openapiprocessor/location/somewhere/resource.yaml"), "api")
35+
36+ pkg.shouldBe("io.openapiprocessor.location.somewhere")
37+ }
38+ })
You can’t perform that action at this time.
0 commit comments