OpenAPITools / OpenAPITools/openapi-generator

[BUG] [swift5] for nullable and required field default nil is not generated

Open
#19,844 1 comment 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
  • [ x] Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • [ x] Have you tested with the latest master to confirm the issue still exists?
  • [x ] Have you searched for related issues/PRs?
  • [ x] What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

When properly is nullable in Swift we'd like to get a setup for default value to nil to it, like this below

public struct MyStruct: Codable, Hashable {

    public var value: Decimal?

    public init(value: Decimal? = nil) {
        self.value = value
    }
}

But when a field in json scheme set as nullable and required then I get such result

public struct MyStruct: Codable, Hashable {

    public var value: Decimal?

    public init(value: Decimal?) {
        self.value = value
    }
}
openapi-generator version

openapi-generator-cli-7.9.0-20241004.072436-107.jar

OpenAPI declaration file content or url
      "MyStruct": {
        "properties": {
          "value": {
            "anyOf": [
              { "type": "string", "format": "decimal" },
              { "type": "null" }
            ],
            "title": "Value"
          },
        },
        "type": "object",
        "required": ["value"],
        "title": "MyStruct"
      },
Generation Details
Steps to reproduce
Related issues/PRs
Suggest a fix

I fixed it by custom template (just updated existed modelObject.mustache template), but it would be nice to have it from out the box.
Thank you.

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 with the Swift generator's modelObject.mustache template and the supplied MyStruct schema, focusing on how a required nullable property is rendered in the initializer. Generate the model and compare the output with the expected initializer defaulting value to nil; done means the generated Swift initializer includes that default without requiring a custom template.

Written by the indexing model from the issue text.

Assessment

Tech stack
swift
Domain
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.