Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Rust-Axum] Fix uuid in header params causing compilation errors #18563

Merged
merged 5 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions bin/configs/manual/rust-axum-header-uuid.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
generatorName: rust-axum
outputDir: samples/server/petstore/rust-axum/output/rust-axum-header-uuid
inputSpec: modules/openapi-generator/src/test/resources/3_0/rust-axum/rust-axum-header-uuid.yaml
templateDir: modules/openapi-generator/src/main/resources/rust-axum
generateAliasAsModel: true
additionalProperties:
hideGenerationTimestamp: "true"
packageName: rust-axum-header-uui
enablePostProcessFile: true
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@

import java.io.File;
import java.io.IOException;
import java.io.ObjectInputFilter.Config;
import java.math.BigInteger;
import java.nio.file.Path;
import java.util.*;
Expand Down Expand Up @@ -552,6 +553,12 @@ public CodegenOperation fromOperation(String path, String httpMethod, Operation
}
}

// Include renderUuidConversionImpl exactly once in the vendorExtensions map when
// at least one `uuid::Uuid` converted from a header value in the resulting Rust code.
final Boolean renderUuidConversionImpl = op.headerParams.stream().anyMatch(h -> h.getDataType().equals(uuidType));
if (renderUuidConversionImpl) {
additionalProperties.put("renderUuidConversionImpl", "true");
}
return op;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{convert::TryFrom, fmt, ops::Deref};
use std::{convert::TryFrom, fmt, ops::Deref{{#renderUuidConversionImpl}}, str::FromStr{{/renderUuidConversionImpl}}};

use chrono::{DateTime, Utc};
use http::HeaderValue;
Expand Down Expand Up @@ -178,3 +178,39 @@ impl TryFrom<IntoHeaderValue<DateTime<Utc>>> for HeaderValue {
}
}
}

{{#renderUuidConversionImpl}}
// uuid::Uuid
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please run integration test.

./bin/generate-samples.sh ./bin/configs/manual/*.yaml
 mvn integration-test -f samples/server/petstore/rust-axum/pom.xml

You might need to create new api with uuid in the header if those specs do not have

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, done that. Should have done it in the first place.


impl TryFrom<HeaderValue> for IntoHeaderValue<uuid::Uuid> {
type Error = String;

fn try_from(hdr_value: HeaderValue) -> Result<Self, Self::Error> {
match hdr_value.to_str() {
Ok(hdr_value) => match uuid::Uuid::from_str(hdr_value) {
Ok(uuid) => Ok(IntoHeaderValue(uuid)),
Err(e) => Err(format!("Unable to parse: {} as uuid - {}", hdr_value, e)),
},
Err(e) => Err(format!(
"Unable to convert header {:?} to string {}",
hdr_value, e
)),
}
}
}

impl TryFrom<IntoHeaderValue<uuid::Uuid>> for HeaderValue {
type Error = String;

fn try_from(hdr_value: IntoHeaderValue<uuid::Uuid>) -> Result<Self, Self::Error> {
match HeaderValue::from_bytes(hdr_value.0.as_bytes()) {
Ok(hdr_value) => Ok(hdr_value),
Err(e) => Err(format!(
"Unable to convert {:?} to a header: {}",
hdr_value, e
)),
}
}
}

{{/renderUuidConversionImpl}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Test deserialization of uuid objects in headers
openapi: 3.0.0
info:
title: Sample API
version: 0.1.9
paths:
/users:
post:
description: Adds a user to the users database table.
parameters:
- $ref: "#/components/parameters/UuidHeaderParam"
responses:
"201": # status code
description: Added row to table!
content:
application/json:
schema:
type: string
components:
schemas:
HeaderUuid:
type: string
format: uuid
example: a9f5a638-728c-479d-af9b-016eb8049ab6
parameters:
UuidHeaderParam:
name: some_uid
in: header
required: true
description: A uuid transmitted in the header.
schema:
$ref: "#/components/schemas/HeaderUuid"
Original file line number Diff line number Diff line change
@@ -1 +1 @@
7.5.0-SNAPSHOT
7.6.0-SNAPSHOT
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ server, you can easily generate a server stub.
To see how to make this your own, look here: [README]((https://openapi-generator.tech))

- API version: 1.0.7
- Generator version: 7.5.0-SNAPSHOT
- Generator version: 7.6.0-SNAPSHOT



Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
7.5.0-SNAPSHOT
7.6.0-SNAPSHOT
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ server, you can easily generate a server stub.
To see how to make this your own, look here: [README]((https://openapi-generator.tech))

- API version: 1.0.7
- Generator version: 7.5.0-SNAPSHOT
- Generator version: 7.6.0-SNAPSHOT



Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
7.5.0-SNAPSHOT
7.6.0-SNAPSHOT
2 changes: 1 addition & 1 deletion samples/server/petstore/rust-axum/output/ops-v3/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ server, you can easily generate a server stub.
To see how to make this your own, look here: [README]((https://openapi-generator.tech))

- API version: 0.0.1
- Generator version: 7.5.0-SNAPSHOT
- Generator version: 7.6.0-SNAPSHOT



Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
7.5.0-SNAPSHOT
7.6.0-SNAPSHOT
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ server, you can easily generate a server stub.
To see how to make this your own, look here: [README]((https://openapi-generator.tech))

- API version: 1.0.0
- Generator version: 7.5.0-SNAPSHOT
- Generator version: 7.6.0-SNAPSHOT



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,7 @@ pub trait Api {
method: Method,
host: Host,
cookies: CookieJar,
body: models::TestEndpointParametersRequest,
) -> Result<TestEndpointParametersResponse, String>;

/// To test enum parameters.
Expand All @@ -457,6 +458,7 @@ pub trait Api {
cookies: CookieJar,
header_params: models::TestEnumParametersHeaderParams,
query_params: models::TestEnumParametersQueryParams,
body: Option<models::TestEnumParametersRequest>,
) -> Result<TestEnumParametersResponse, String>;

/// test inline additionalProperties.
Expand All @@ -478,6 +480,7 @@ pub trait Api {
method: Method,
host: Host,
cookies: CookieJar,
body: models::TestJsonFormDataRequest,
) -> Result<TestJsonFormDataResponse, String>;

/// To test class name in snake case.
Expand Down Expand Up @@ -567,6 +570,7 @@ pub trait Api {
host: Host,
cookies: CookieJar,
path_params: models::UpdatePetWithFormPathParams,
body: Option<models::UpdatePetWithFormRequest>,
) -> Result<UpdatePetWithFormResponse, String>;

/// uploads an image.
Expand Down
Loading
Loading