swagger-api / swagger-api/swagger-codegen

[Swift4] Model initializer with required properties only

Open
#8,365 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

To illustrate my problem I'm going to use the Client model available below. In our Client model, there are 26 properties, where only 3 are required (non-optional).

Client.swift.zip

In swagger-codegen-2.4.0 models are generated with no initializers, when a model has only optional properties, I'm able to initialize it with no arguments (so Client() is possible), however, when there's at least one required property the only initalizer available forces me to pass values to the required arguments and nil to the rest, as follows:

let client = Client(cpf: clientCPF, name: clientName, email: clientEmail, alternateEmail: nil, motherName: nil, birthDate: nil, maritalStatus: nil, maritalPartner: nil, identityDocuments: nil, personalPhones: nil, residentialAddress: nil, professionalData: nil, birthCity: nil, birthState: nil, graduation: nil, fatherName: nil, nationality: nil, residenceType: nil, gender: nil, clientReferences: nil, clientAssets: nil, residenceLiveYears: nil, consultCreditDataAuthorization: nil, proxy: nil, paymentOption: nil, debitAccount: nil)

I'd like to have an initializer with only required properties, like:

let client = Client(cpf: clientCPF, name: clientName, email: clientEmail)

Being forced to pass nil to all optional properties when initializing a model when I only need a couple of properties looks really bad.

Is it possible to have another initializer that takes only required properties?

swagger-codegen-2.4.0

java -jar ~/Documents/projects/swagger/swagger-codegen-2.4.0/modules/swagger-codegen-cli/target/swagger-codegen-cli.jar generate -i /Users/me/Downloads/swagger.json -l swift4 -o /Users/me/Downloads/swagger-output

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 locating the swift4 generator template used by the shown swagger-codegen CLI command and inspect how the generated Client.swift initializer is formed. Generate a model containing both required and optional properties, then verify that the output supports initialization with required properties only while preserving the existing behavior for optional properties.

Written by the indexing model from the issue text.

Assessment

Tech stack
swift
Domain
tooling
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.