Skip to content

[BUG] Nullable properties generated as not null in Rust #24108

Description

@C0D3-M4513R

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

#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 }
]

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions