OpenAPITools / OpenAPITools/openapi-generator
[BUG][Crystal] For an array of nullable items, the nullability is not respected
Nobody has claimed this yet.
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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