Skip to content

Commit 4380179

Browse files
committed
#25, should generate valid type name
(cherry picked from commit 9014b54)
1 parent f7dea95 commit 4380179

7 files changed

Lines changed: 86 additions & 3 deletions

File tree

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@ import io.openapiprocessor.core.support.capitalizeFirstChar
1313
class ModelClassNameCreator(private val suffix: String) {
1414

1515
fun createName(dataTypeName: String): String {
16+
val classTypeName = toClass(dataTypeName)
17+
1618
if (dataTypeName.isEmpty())
17-
return dataTypeName
19+
return classTypeName
1820

1921
if (dataTypeName.endsWith(suffix.capitalizeFirstChar()))
20-
return dataTypeName
22+
return classTypeName
2123

22-
return "$dataTypeName${suffix.capitalizeFirstChar()}"
24+
return "$classTypeName${suffix.capitalizeFirstChar()}"
2325
}
2426

2527
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,8 @@ class ModelClassNameCreatorSpec : StringSpec({
2020
creator.createName("FooX") shouldBe "FooX"
2121
}
2222

23+
"create valid java class name" {
24+
val creator = ModelClassNameCreator("")
25+
creator.createName("foo-foo") shouldBe "FooFoo"
26+
}
2327
})

openapi-processor-core/src/testInt/resources/tests/params-enum/generated.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ items:
22
- generated/api/EnumApi.java
33
- generated/model/Bar.java
44
- generated/model/Foo.java
5+
- generated/model/FooFoo.java

openapi-processor-core/src/testInt/resources/tests/params-enum/generated/api/EnumApi.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import annotation.Parameter;
55
import generated.model.Bar;
66
import generated.model.Foo;
7+
import generated.model.FooFoo;
78
import generated.support.Generated;
89

910
@Generated(value = "openapi-processor-core", version = "test")
@@ -14,4 +15,7 @@ void getEndpoint(
1415
@Parameter Foo foo,
1516
@Parameter Bar bar);
1617

18+
@Mapping("/endpoint-dashed")
19+
void getEndpointDashed(@Parameter FooFoo fooFoo);
20+
1721
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package generated.model;
2+
3+
import com.fasterxml.jackson.annotation.JsonCreator;
4+
import com.fasterxml.jackson.annotation.JsonValue;
5+
import generated.support.Generated;
6+
7+
@Generated(value = "openapi-processor-core", version = "test")
8+
public enum FooFoo {
9+
FOO("foo"),
10+
FOO_2("foo-2"),
11+
FOO_FOO("foo-foo");
12+
13+
private final String value;
14+
15+
FooFoo(String value) {
16+
this.value = value;
17+
}
18+
19+
@JsonValue
20+
public String getValue() {
21+
return this.value;
22+
}
23+
24+
@JsonCreator
25+
public static FooFoo fromValue(String value) {
26+
for (FooFoo val : FooFoo.values()) {
27+
if (val.value.equals(value)) {
28+
return val;
29+
}
30+
}
31+
throw new IllegalArgumentException(value);
32+
}
33+
34+
}

openapi-processor-core/src/testInt/resources/tests/params-enum/inputs/openapi30.yaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,25 @@ paths:
3030
'204':
3131
description: empty
3232

33+
/endpoint-dashed:
34+
get:
35+
tags:
36+
- enum
37+
parameters:
38+
- name: foo-foo
39+
description: enum parameter
40+
in: query
41+
required: true
42+
schema:
43+
type: string
44+
enum:
45+
- foo
46+
- foo-2
47+
- foo-foo
48+
responses:
49+
'204':
50+
description: empty
51+
3352
components:
3453
schemas:
3554

openapi-processor-core/src/testInt/resources/tests/params-enum/inputs/openapi31.yaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,25 @@ paths:
3030
'204':
3131
description: empty
3232

33+
/endpoint-dashed:
34+
get:
35+
tags:
36+
- enum
37+
parameters:
38+
- name: foo-foo
39+
description: enum parameter
40+
in: query
41+
required: true
42+
schema:
43+
type: string
44+
enum:
45+
- foo
46+
- foo-2
47+
- foo-foo
48+
responses:
49+
'204':
50+
description: empty
51+
3352
components:
3453
schemas:
3554

0 commit comments

Comments
 (0)