Skip to content

Feat: (Rust) Added chrono (date/date-time) support for Rust Generator [resolves #19319]#23451

Open
richy1623 wants to merge 8 commits intoOpenAPITools:masterfrom
richy1623:fix_issue_19319
Open

Feat: (Rust) Added chrono (date/date-time) support for Rust Generator [resolves #19319]#23451
richy1623 wants to merge 8 commits intoOpenAPITools:masterfrom
richy1623:fix_issue_19319

Conversation

@richy1623
Copy link
Copy Markdown

@richy1623 richy1623 commented Apr 6, 2026

Current issue:

The rust generator does not use a typed Date/DateTime object, it uses a type String [resolves #19319]

Proposed solution

Use a common date time library chrono to represent these types

Implementation

  • Added a new CLI option (useChrono) which defaults to true
  • If this property is set use the new chrono types for the fields typeMapping
  • If at least one field is using a Date or DateTime then include chrono as a dependency in the Cargo.toml

Testing

  • Updated the tests to now expect a new dependency chrono where relevant
  • Added a new test config for the option of useChrono=false and the expected behaviour is using String types

Requesting a review from one of the Rust technical comittee:

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • Run the following to build the project and update samples:
    ./mvnw clean package || exit
    ./bin/generate-samples.sh ./bin/configs/*.yaml || exit
    ./bin/utils/export_docs_generators.sh || exit
    
    (For Windows users, please run the script in WSL)
    Commit all changed files.
    This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
    These must match the expectations made by your contribution.
    You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*.
    IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
  • File the PR against the correct branch: master (upcoming 7.x.0 minor release - breaking changes with fallbacks), 8.0.x (breaking changes without fallbacks)
  • If your PR solves a reported issue, reference it using GitHub's linking syntax (e.g., having "fixes #123" present in the PR description)
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

Summary by cubic

Adds typed date/time support in the Rust generator using chrono. By default, date maps to chrono::NaiveDate and date-time maps to chrono::DateTime<chrono::FixedOffset> to improve type safety and RFC3339 serialization. Resolves #19319.

  • New Features

    • Map date -> chrono::NaiveDate, date-time -> chrono::DateTime<chrono::FixedOffset> when useChrono=true.
    • New option useChrono (CLI/config), default true; false keeps String.
    • Add chrono in generated Cargo.toml only when date fields are present.
    • Documented useChrono in Rust generator docs; added rust-reqwest-petstore-no-chrono config and sample.
  • Migration

    • No changes for most users; chrono is added automatically when models use dates.
    • To keep legacy String dates, set useChrono=false (e.g., -p useChrono=false).

Written for commit a522e3e. Summary will update on new commits.

Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

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

8 issues found across 111 files

Note: This PR contains a large number of files. cubic only reviews up to 75 files per PR, so some files may not have been reviewed.

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="samples/client/petstore/rust/hyper/petstore/src/models/order.rs">

<violation number="1" location="samples/client/petstore/rust/hyper/petstore/src/models/order.rs:24">
P2: `ship_date` uses timezone-naive `NaiveDateTime` for an OpenAPI `date-time` field, risking RFC3339 offset incompatibility and loss of timezone semantics.</violation>
</file>

<file name="samples/client/petstore/rust/reqwest/petstore-avoid-box/docs/Order.md">

<violation number="1" location="samples/client/petstore/rust/reqwest/petstore-avoid-box/docs/Order.md:10">
P1: `date-time` is mapped to timezone-naive `chrono::NaiveDateTime`, which can drop or reject RFC3339 offset information for `ship_date`.</violation>
</file>

<file name="samples/client/petstore/rust/reqwest/petstore-no-chrono/git_push.sh">

<violation number="1" location="samples/client/petstore/rust/reqwest/petstore-no-chrono/git_push.sh:57">
P2: `git push` is piped through `grep`, which can hide the real push exit code and return incorrect script success/failure.</violation>
</file>

<file name="samples/client/petstore/rust/reqwest/petstore-no-chrono/src/apis/testing_api.rs">

<violation number="1" location="samples/client/petstore/rust/reqwest/petstore-no-chrono/src/apis/testing_api.rs:81">
P2: `Result<String, ...>` success handling incorrectly rejects `text/plain` responses instead of returning the response text.</violation>
</file>

<file name="samples/client/petstore/rust/reqwest/petstore-no-chrono/docs/TestingApi.md">

<violation number="1" location="samples/client/petstore/rust/reqwest/petstore-no-chrono/docs/TestingApi.md:54">
P2: Dead markdown link: `std::path::PathBuf` is linked to `std::path::PathBuf.md`, but that file is not present in the docs directory.</violation>
</file>

<file name="samples/client/petstore/rust/hyper/petstore/docs/Order.md">

<violation number="1" location="samples/client/petstore/rust/hyper/petstore/docs/Order.md:10">
P2: `date-time` is documented and modeled as `chrono::NaiveDateTime`, which is timezone-naive and can break or degrade RFC3339 offset semantics.</violation>
</file>

<file name="samples/client/petstore/rust/reqwest/petstore-no-chrono/src/apis/mod.rs">

<violation number="1" location="samples/client/petstore/rust/reqwest/petstore-no-chrono/src/apis/mod.rs:104">
P2: Content-Type detection is case-sensitive, so valid mixed-case JSON/text media types can be misclassified as unsupported and break response parsing.</violation>
</file>

<file name="samples/client/petstore/rust/reqwest/petstore-async-middleware/src/models/order.rs">

<violation number="1" location="samples/client/petstore/rust/reqwest/petstore-async-middleware/src/models/order.rs:24">
P2: `shipDate` uses `NaiveDateTime`, which is timezone-naive and can break RFC3339 `date-time` compatibility (offset parsing/round-tripping).</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

let content = resp.text()?;
match content_type {
ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `String`"))),
Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai bot Apr 6, 2026

Choose a reason for hiding this comment

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

P2: Result<String, ...> success handling incorrectly rejects text/plain responses instead of returning the response text.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At samples/client/petstore/rust/reqwest/petstore-no-chrono/src/apis/testing_api.rs, line 81:

<comment>`Result<String, ...>` success handling incorrectly rejects `text/plain` responses instead of returning the response text.</comment>

<file context>
@@ -0,0 +1,225 @@
+        let content = resp.text()?;
+        match content_type {
+            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
+            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `String`"))),
+            ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `String`")))),
+        }
</file context>
Fix with Cubic

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

this is generated by the generate-samples.sh script


impl From<&str> for ContentType {
fn from(content_type: &str) -> Self {
if content_type.starts_with("application") && content_type.contains("json") {
Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai bot Apr 6, 2026

Choose a reason for hiding this comment

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

P2: Content-Type detection is case-sensitive, so valid mixed-case JSON/text media types can be misclassified as unsupported and break response parsing.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At samples/client/petstore/rust/reqwest/petstore-no-chrono/src/apis/mod.rs, line 104:

<comment>Content-Type detection is case-sensitive, so valid mixed-case JSON/text media types can be misclassified as unsupported and break response parsing.</comment>

<file context>
@@ -0,0 +1,120 @@
+
+impl From<&str> for ContentType {
+    fn from(content_type: &str) -> Self {
+        if content_type.starts_with("application") && content_type.contains("json") {
+            return Self::Json;
+        } else if content_type.starts_with("text/plain") {
</file context>
Fix with Cubic

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

this is generated by the generate-samples.sh script

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[REQ](rust, client) Use a proper Date and DateTime type for deserializing dates

1 participant