Skip to content

Commit 2326eef

Browse files
authored
Fixups for rust-server hyper1 support (#22321)
1 parent d318752 commit 2326eef

File tree

37 files changed

+734
-70
lines changed

37 files changed

+734
-70
lines changed

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/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/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)

samples/server/petstore/rust-server-deprecated/output/openapi-v3/api/openapi.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -780,6 +780,9 @@ components:
780780
NullableObject:
781781
nullable: true
782782
type: string
783+
NoTypeObject:
784+
description: An object with no type
785+
title: an object with no type
783786
NullableTest:
784787
properties:
785788
nullable:
@@ -847,12 +850,21 @@ components:
847850
anyOf:
848851
- $ref: "#/components/schemas/StringObject"
849852
- $ref: "#/components/schemas/UuidObject"
853+
FormTest_request_enum_field:
854+
enum:
855+
- one_enum
856+
type: string
850857
FormTest_request:
851858
properties:
852859
requiredArray:
853860
items:
854861
type: string
855862
type: array
863+
enum_field:
864+
$ref: "#/components/schemas/FormTest_request_enum_field"
865+
required:
866+
- enum_field
867+
- requiredArray
856868
type: object
857869
AnyOfObject_anyOf:
858870
enum:

samples/server/petstore/rust-server-deprecated/output/openapi-v3/bin/cli.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,9 @@ enum Operation {
112112
/// Test a Form Post
113113
FormTest {
114114
#[structopt(parse(try_from_str = parse_json), long)]
115-
required_array: Option<Vec<String>>,
115+
required_array: Vec<String>,
116+
#[structopt(parse(try_from_str = parse_json))]
117+
enum_field: models::FormTestRequestEnumField,
116118
},
117119
GetWithBooleanParameter {
118120
/// Let's check apostrophes get encoded properly!
@@ -352,11 +354,13 @@ async fn main() -> Result<()> {
352354
}
353355
Operation::FormTest {
354356
required_array,
357+
enum_field,
355358
} => {
356359
info!("Performing a FormTest request");
357360

358361
let result = client.form_test(
359362
required_array.as_ref(),
363+
enum_field,
360364
).await?;
361365
debug!("Result: {:?}", result);
362366

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# FormTestRequestEnumField
2+
3+
## Properties
4+
Name | Type | Description | Notes
5+
------------ | ------------- | ------------- | -------------
6+
7+
[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)
8+
9+

0 commit comments

Comments
 (0)