OpenAPITools / OpenAPITools/openapi-generator

[BUG][Crystal] For an array of nullable items, the nullability is not respected

Open
#22,070 3 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

Description

I have an array of nullable integers in my specification. But the Crystal code generator produces Array(Int32) whereas I would expect Array(Int32?).

I also confirmed that the JSON parsing fails if the array actually contains a null.

openapi-generator version

openapi-generator-cli 7.16.0

OpenAPI declaration file content or url
openapi: 3.0.4
info:
  title: ""
  description: ""
  version: ""
paths:
  /api/v3/items/:
    get:
      operationId: get_items
      responses:
        "200":
          description: ""
          content:
            application/json:
              schema:
                type: object
                required: [ids]
                properties:
                  ids:
                    type: array
                    items:
                      type: integer
                      nullable: true
                      
Generation Details
openapi-generator-cli generate -i swagger.yaml -g crystal -o api
Steps to reproduce

Generate the code based on the above.

The content of the file api/src/openapi_client/models/get_items200_response.cr contains the following:

module OpenAPIClient
  class GetItems200Response
    include JSON::Serializable
    include YAML::Serializable

    # Required properties
    @[JSON::Field(key: "ids", type: Array(Int32), nillable: false, emit_null: false)]
    property ids : Array(Int32)

    # Initializes the object
    # @param [Hash] attributes Model attributes in the form of hash
    def initialize(@ids : Array(Int32))
    end

What I expect to see instead:

module OpenAPIClient
  class GetItems200Response
    include JSON::Serializable
    include YAML::Serializable

    # Required properties
    @[JSON::Field(key: "ids", type: Array(Int32?), nillable: false, emit_null: false)]
    property ids : Array(Int32?)

    # Initializes the object
    # @param [Hash] attributes Model attributes in the form of hash
    def initialize(@ids : Array(Int32?))
    end
Related issues/PRs

None. https://github.com/openapitools/openapi-generator/issues?q=is%3Aissue%20crystal%20nullable

Suggest a fix

cc @wing328

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 reproducing the issue with the provided OpenAPI declaration and openapi-generator-cli generate -i swagger.yaml -g crystal -o api. Inspect the generated api/src/openapi_client/models/get_items200_response.cr and the Crystal generator’s handling of nullable array items. Done means the generated property and JSON field use Array(Int32?), and parsing an array containing null succeeds.

Written by the indexing model from the issue text.

Assessment

Tech stack
crystal, openapi
Domain
api, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.