OpenAPITools / OpenAPITools/openapi-generator

[BUG] Nullable properties generated as not null in Rust

Open
#24,108 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

The generated types have missing Option, causing api responses to fail to parse.

Changing the openapi spec version of the api to 3.0.0 from 3.1.0 seems to resolve all of the issues (which aren't due the the spec's fault)

openapi-generator version

7.23.0

OpenAPI declaration file content or url

From jinxxy: https://api.creators.jinxxy.com/v1/docs#description/introduction
api.yaml

Generation Details

Generation script:

# Generate Client
rm src/apis src/models docs -rf

./node_modules/\@openapitools/openapi-generator-cli/main.js generate \
-g rust \
'--additional-properties=packageName=jinxxy,supportAsync=true,avoidBoxedModels=true,library=reqwest,supportMiddleware=false,description="",infoEmail="",licenseInfo=""' \
-o . \
-i "api.yaml"
#--global-property debugOperations=true

# Update entire description (replace entire line, match the random data there) line in Cargo.toml
sed -i 's/^description = ".*"//' Cargo.toml
sed -i 's/^license = ""//' Cargo.toml
sed -i 's/^authors = \[""\]//' Cargo.toml

# Remove empty doc comments
find src -type f -exec sed -i '/^\s*\/\/\/\s*$/d' {} \;

echo '#![allow(non_camel_case_types)]'$'\n''#![allow(non_snake_case)]'$'\n'"$(cat src/lib.rs)" > src/lib.rs
Steps to reproduce
Example Api response: from https://api.creators.jinxxy.com/v1/orders/{id}
{
  "object": "StoreOrder",
  "id": "redacted",
  "email": "redacted",
  "paid_at": "2026-06-23T12:02:30.385Z",
  "user": {
    "object": "User",
    "id": "redacted",
    "name": "redacted",
    "username": "redacted",
    "profile_image": {
      "object": "ProfileImageFile",
      "id": "redacted",
      "url": "redacted",
      "updated_at": "2024-04-09T22:33:35.925Z"
    },
    "updated_at": "2024-06-23T12:02:30.548Z"
  },
  "payment_status": "PAID",
  "checkout_fields": [
    {
      "object": "CheckoutAnswer",
      "answer": "what",
      "label": "redacted"
    }
  ],
  "order_items": [
    {
      "object": "StoreOrderItem",
      "id": "redacted",
      "name": "Discount: redacted",
      "target_id": "redacted",
      "target_type": "DISCOUNT",
      "target_version_id": "redacted",
      "seller": null,
      "license_id": null,
      "license": null
    },
    {
      "object": "StoreOrderItem",
      "id": "redacted",
      "name": "Platform Fee",
      "target_id": "redacted",
      "target_type": "PLATFORM_FEE",
      "target_version_id": null,
      "seller": {
        "object": "User",
        "id": "-1",
        "name": null,
        "username": null,
        "profile_image": null,
        "updated_at": "2024-01-14T08:21:50.137Z"
      },
      "license_id": null,
      "license": null
    },
    {
      "object": "StoreOrderItem",
      "id": "redacted",
      "name": "redacted",
      "target_id": "redacted",
      "target_type": "DIGITAL_PRODUCT",
      "target_version_id": "redacted",
      "seller": {
        "object": "User",
        "id": "redacted",
        "name": "redacted",
        "username": "redacted",
        "profile_image": {
          "object": "ProfileImageFile",
          "id": "redacted",
          "url": "redacted",
          "updated_at": "2024-04-09T22:33:35.925Z"
        },
        "updated_at": "2024-06-23T12:02:30.548Z"
      },
      "license_id": "redacted",
      "license": {
        "object": "UserLicense",
        "id": "redacted",
        "key": "redacted",
        "short_key": "redacted"
      }
    }
  ],
  "payout_total": 0
}

order_items[0].seller is null and is marked as nullable in the spec, however the in generated return type OrdersIdGet200ResponseOrderItemsInner seller is not wrapped in an option.

(note that some of the the properties (e.g. name & username) in seller for order_items[0].seller are also null, but the spec doesn't mark those as nullable. That seems more like the spec being wrong than the generator being wrong.)

Generated code of OrdersIdGet200ResponseOrderItemsInner:

#[derive(Clone, Default, Debug, PartialEq, Serialize, Deserialize)]
pub struct OrdersIdGet200ResponseOrderItemsInner {
    #[serde(rename = "id")]
    pub id: String,
    #[serde(rename = "object")]
    pub object: String,
    #[serde(rename = "name")]
    pub name: String,
    #[serde(rename = "target_id")]
    pub target_id: String,
    /// Type of item (DIGITAL_PRODUCT, PLATFORM_FEE, DISCOUNT, etc.)
    #[serde(rename = "target_type")]
    pub target_type: String,
    #[serde(rename = "target_version_id")]
    pub target_version_id: String,
    #[serde(rename = "seller")]
    pub seller: models::OrdersIdGet200ResponseOrderItemsInnerSeller,
    /// Associated license ID. Only present for digital products, null for fees/discounts.
    #[serde(rename = "license_id")]
    pub license_id: String,
    #[serde(rename = "license")]
    pub license: models::OrdersIdGet200ResponseOrderItemsInnerLicense,
}
Related issues/PRs

https://github.com/OpenAPITools/openapi-generator/issues/2337 seems like it talks about missing options, but it addresses template edits

Suggest a fix

One workaround is to apply this jsonpatch to the spec (I swapped over from the yaml to the json version):

[
  { "op": "replace", "path": "/openapi", "value": "3.0.0" },
  { "op": "add", "path": "/paths/~1orders~1{id}/get/responses/200/content/application~1json/schema/properties/order_items/items/properties/seller/properties/name/nullable", "value": true },
  { "op": "add", "path": "/paths/~1orders~1{id}/get/responses/200/content/application~1json/schema/properties/order_items/items/properties/seller/properties/username/nullable", "value": true }
]

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the provided Rust generation command with api.yaml and inspect OrdersIdGet200ResponseOrderItemsInner, especially seller and license. Trace the OpenAPI 3.1 nullable-property handling in the Rust generator and compare it with the 3.0.0 output. Done means nullable response properties generate Option types and a regression case covers the reported response.

Written by the indexing model from the issue text.

Assessment

Tech stack
openapi, rust
Domain
api, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.