Skip to content

Commit a7696b8

Browse files
authored
Merge branch 'OpenAPITools:master' into master
2 parents 0461331 + 2ab5365 commit a7696b8

40 files changed

Lines changed: 788 additions & 71 deletions

File tree

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RustServerCodegen.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,16 @@ public String toOperationId(String operationId) {
370370
return sanitizeIdentifier(operationId, CasingType.CAMEL_CASE, "call", "method", true);
371371
}
372372

373+
@Override
374+
public String toParamName(String name) {
375+
// rust-server doesn't support r# in param name.
376+
if (parameterNameMapping.containsKey(name)) {
377+
return parameterNameMapping.get(name);
378+
}
379+
380+
return sanitizeIdentifier(name, CasingType.SNAKE_CASE, "param", "parameter", false);
381+
}
382+
373383
@Override
374384
public String toEnumValue(String value, String datatype) {
375385
// rust-server templates expect value to be in quotes
@@ -1229,6 +1239,9 @@ public CodegenModel fromModel(String name, Schema model) {
12291239
&& (mdl.dataType.startsWith("swagger::OneOf") || mdl.dataType.startsWith("swagger::AnyOf"))) {
12301240
toStringSupport = false;
12311241
partialOrdSupport = false;
1242+
} else if (mdl.dataType != null && mdl.dataType.equals("serde_json::Value")) {
1243+
// Value doesn't implement PartialOrd
1244+
partialOrdSupport = false;
12321245
} else if (mdl.getAdditionalPropertiesType() != null) {
12331246
toStringSupport = false;
12341247
} else if (model instanceof ComposedSchema) {
@@ -1545,7 +1558,18 @@ private void processParam(CodegenParameter param, CodegenOperation op) {
15451558
}
15461559
} else {
15471560
param.vendorExtensions.put("x-format-string", "{:?}");
1548-
if (param.example != null) {
1561+
// Check if this is a model-type enum (allowableValues with values list)
1562+
if (param.allowableValues != null && param.allowableValues.containsKey("values")) {
1563+
List<?> values = (List<?>) param.allowableValues.get("values");
1564+
if (!values.isEmpty()) {
1565+
// Use the first enum value as the example.
1566+
String firstEnumValue = values.get(0).toString();
1567+
String enumVariant = toEnumVarName(firstEnumValue, param.dataType);
1568+
example = param.dataType + "::" + enumVariant;
1569+
} else if (param.example != null) {
1570+
example = "serde_json::from_str::<" + param.dataType + ">(r#\"" + param.example + "\"#).expect(\"Failed to parse JSON example\")";
1571+
}
1572+
} else if (param.example != null) {
15491573
example = "serde_json::from_str::<" + param.dataType + ">(r#\"" + param.example + "\"#).expect(\"Failed to parse JSON example\")";
15501574
}
15511575
}

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RustServerCodegenDeprecated.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1229,6 +1229,9 @@ public CodegenModel fromModel(String name, Schema model) {
12291229
&& (mdl.dataType.startsWith("swagger::OneOf") || mdl.dataType.startsWith("swagger::AnyOf"))) {
12301230
toStringSupport = false;
12311231
partialOrdSupport = false;
1232+
} else if (mdl.dataType != null && mdl.dataType.equals("serde_json::Value")) {
1233+
// Value doesn't implement PartialOrd
1234+
partialOrdSupport = false;
12321235
} else if (mdl.getAdditionalPropertiesType() != null) {
12331236
toStringSupport = false;
12341237
} else if (model instanceof ComposedSchema) {
@@ -1546,7 +1549,18 @@ private void processParam(CodegenParameter param, CodegenOperation op) {
15461549
}
15471550
else {
15481551
param.vendorExtensions.put("x-format-string", "{:?}");
1549-
if (param.example != null) {
1552+
// Check if this is a model-type enum (allowableValues with values list)
1553+
if (param.allowableValues != null && param.allowableValues.containsKey("values")) {
1554+
List<?> values = (List<?>) param.allowableValues.get("values");
1555+
if (!values.isEmpty()) {
1556+
// Use the first enum value as the example.
1557+
String firstEnumValue = values.get(0).toString();
1558+
String enumVariant = toEnumVarName(firstEnumValue, param.dataType);
1559+
example = param.dataType + "::" + enumVariant;
1560+
} else if (param.example != null) {
1561+
example = "serde_json::from_str::<" + param.dataType + ">(r#\"" + param.example + "\"#).expect(\"Failed to parse JSON example\")";
1562+
}
1563+
} else if (param.example != null) {
15501564
example = "serde_json::from_str::<" + param.dataType + ">(r#\"" + param.example + "\"#).expect(\"Failed to parse JSON example\")";
15511565
}
15521566
}

modules/openapi-generator/src/main/resources/kotlin-client/data_class.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ import {{packageName}}.infrastructure.ITransformForStorage
145145
{{#multiplatform}}
146146
@Serializable
147147
{{/multiplatform}}
148-
{{#nonPublicApi}}internal {{/nonPublicApi}}{{^nonPublicApi}}{{#explicitApi}}public {{/explicitApi}}{{/nonPublicApi}}enum class {{{nameInPascalCase}}}({{^nonPublicApi}}{{#explicitApi}}public {{/explicitApi}}{{/nonPublicApi}}val value: {{^isContainer}}{{dataType}}{{/isContainer}}{{#isContainer}}kotlin.String{{/isContainer}}) {
148+
{{#nonPublicApi}}internal {{/nonPublicApi}}{{^nonPublicApi}}{{#explicitApi}}public {{/explicitApi}}{{/nonPublicApi}}enum class {{{nameInPascalCase}}}({{^nonPublicApi}}{{#explicitApi}}public {{/explicitApi}}{{/nonPublicApi}}val value: {{^isContainer}}{{dataType}}{{/isContainer}}{{#isContainer}}{{#mostInnerItems}}{{dataType}}{{/mostInnerItems}}{{/isContainer}}) {
149149
{{#allowableValues}}
150150
{{#enumVars}}
151151
{{^multiplatform}}

modules/openapi-generator/src/main/resources/rust-server/client-request-body-instance.mustache

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,17 @@
8080
{{/required}}
8181
{{#exts}}
8282
{{#x-consumes-plain-text}}
83-
{{#isByteArray}}
84-
let body = String::from_utf8(param_body.0).expect("Body was not valid UTF8");
85-
{{/isByteArray}}
86-
{{^isByteArray}}
83+
{{^isByteArray}}
84+
{{^isBinary}}
8785
let body = param_{{{paramName}}};
88-
{{/isByteArray}}
86+
{{/isBinary}}
87+
{{/isByteArray}}
88+
{{#isByteArray}}
89+
let body = String::from_utf8(param_{{{paramName}}}.0).expect("Body was not valid UTF8");
90+
{{/isByteArray}}
91+
{{#isBinary}}
92+
let body = String::from_utf8(param_{{{paramName}}}.0).expect("Body was not valid UTF8");
93+
{{/isBinary}}
8994
{{/x-consumes-plain-text}}
9095
{{#x-consumes-xml}}
9196
let body = param_{{{paramName}}}.as_xml();

modules/openapi-generator/src/main/resources/rust-server/server-request-body-basic.mustache

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828

2929
{{/x-consumes-plain-text}}
3030
{{#x-consumes-plain-text}}
31+
{{#isBinary}}
32+
Some(swagger::ByteArray(body.to_vec()))
33+
{{/isBinary}}
3134
{{#isByteArray}}
3235
Some(swagger::ByteArray(body.to_vec()))
3336
{{/isByteArray}}

modules/openapi-generator/src/test/java/org/openapitools/codegen/kotlin/KotlinClientCodegenModelTest.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import java.util.Map;
4444

4545
import static org.openapitools.codegen.CodegenConstants.*;
46+
import static org.openapitools.codegen.TestUtils.assertFileContains;
4647

4748
@SuppressWarnings("static-method")
4849
public class KotlinClientCodegenModelTest {
@@ -617,6 +618,32 @@ public void polymorphicJacksonSerialization() throws IOException {
617618
TestUtils.assertFileNotContains(birdKt, "val discriminator");
618619
}
619620

621+
@Test
622+
public void testIntArrayToEnum() throws IOException {
623+
File output = Files.createTempDirectory("test").toFile();
624+
output.deleteOnExit();
625+
626+
final CodegenConfigurator configurator = new CodegenConfigurator()
627+
.setGeneratorName("kotlin")
628+
.setLibrary("jvm-ktor")
629+
.setAdditionalProperties(new HashMap<>() {{
630+
put(CodegenConstants.SERIALIZATION_LIBRARY, "jackson");
631+
put(CodegenConstants.MODEL_PACKAGE, "model");
632+
put(ENUM_PROPERTY_NAMING, "UPPERCASE");
633+
}})
634+
.setInputSpec("src/test/resources/3_0/kotlin/issue15204-int-array-enum.yaml")
635+
.setOutputDir(output.getAbsolutePath().replace("\\", "/"));
636+
637+
final ClientOptInput clientOptInput = configurator.toClientOptInput();
638+
DefaultGenerator generator = new DefaultGenerator();
639+
640+
generator.opts(clientOptInput).generate();
641+
642+
final Path modelKt = Paths.get(output + "/src/main/kotlin/model/ModelWithIntArrayEnum.kt");
643+
644+
TestUtils.assertFileContains(modelKt, "enum class DaysOfWeek(val value: kotlin.Int)");
645+
}
646+
620647
private static class ModelNameTest {
621648
private final String expectedName;
622649
private final String expectedClassName;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
openapi: 3.0.0
2+
info:
3+
title: 'Issue 15204 Int Array Enum'
4+
version: latest
5+
paths:
6+
'/':
7+
get:
8+
operationId: operation
9+
responses:
10+
'200':
11+
description: Success
12+
content:
13+
application/json:
14+
schema:
15+
$ref: '#/components/schemas/ModelWithIntArrayEnum'
16+
components:
17+
schemas:
18+
ModelWithIntArrayEnum:
19+
required:
20+
- daysOfWeek
21+
properties:
22+
daysOfWeek:
23+
type: array
24+
items:
25+
type: integer
26+
enum: [0, 1, 2, 3, 4, 5, 6]

modules/openapi-generator/src/test/resources/3_0/rust-server/openapi-v3.yaml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,7 @@ paths:
500500
summary: Test a Form Post
501501
operationId: FormTest
502502
requestBody:
503+
required: true
503504
content:
504505
application/x-www-form-urlencoded:
505506
schema:
@@ -509,7 +510,13 @@ paths:
509510
type: array
510511
items:
511512
type: string
512-
required: true
513+
enum_field:
514+
type: string
515+
enum:
516+
- one_enum
517+
required:
518+
- requiredArray
519+
- enum_field
513520
responses:
514521
'200':
515522
description: OK
@@ -728,6 +735,9 @@ components:
728735
NullableObject:
729736
type: string
730737
nullable: true
738+
NoTypeObject:
739+
title: an object with no type
740+
description: An object with no type
731741
NullableTest:
732742
type: object
733743
required:

samples/server/petstore/rust-server-deprecated/output/openapi-v3/.openapi-generator/FILES

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@ docs/DuplicateXmlObject.md
1919
docs/EnumWithStarObject.md
2020
docs/Err.md
2121
docs/Error.md
22+
docs/FormTestRequestEnumField.md
2223
docs/Model12345AnyOfObject.md
2324
docs/Model12345AnyOfObjectAnyOf.md
2425
docs/MultigetGet201Response.md
2526
docs/MyId.md
2627
docs/MyIdList.md
28+
docs/NoTypeObject.md
2729
docs/NullableObject.md
2830
docs/NullableTest.md
2931
docs/ObjectHeader.md

samples/server/petstore/rust-server-deprecated/output/openapi-v3/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ cargo run --example client XmlOtherPost
113113
cargo run --example client XmlOtherPut
114114
cargo run --example client XmlPost
115115
cargo run --example client XmlPut
116+
cargo run --example client EnumInPathPathParamGet
116117
cargo run --example client MultiplePathParamsWithVeryLongPathToTestFormattingPathParamAPathParamBGet
117118
cargo run --example client CreateRepo
118119
cargo run --example client GetRepoInfo
@@ -201,11 +202,13 @@ Method | HTTP request | Description
201202
- [EnumWithStarObject](docs/EnumWithStarObject.md)
202203
- [Err](docs/Err.md)
203204
- [Error](docs/Error.md)
205+
- [FormTestRequestEnumField](docs/FormTestRequestEnumField.md)
204206
- [Model12345AnyOfObject](docs/Model12345AnyOfObject.md)
205207
- [Model12345AnyOfObjectAnyOf](docs/Model12345AnyOfObjectAnyOf.md)
206208
- [MultigetGet201Response](docs/MultigetGet201Response.md)
207209
- [MyId](docs/MyId.md)
208210
- [MyIdList](docs/MyIdList.md)
211+
- [NoTypeObject](docs/NoTypeObject.md)
209212
- [NullableObject](docs/NullableObject.md)
210213
- [NullableTest](docs/NullableTest.md)
211214
- [ObjectHeader](docs/ObjectHeader.md)

0 commit comments

Comments
 (0)