Skip to content

[Swift] Add optional parameters to init when unwrapRequired is true#3813

Merged
wing328 merged 1 commit into
swagger-api:2.3.0from
mwgray:unwrapRequired-init-optional-parameters
Sep 22, 2016
Merged

[Swift] Add optional parameters to init when unwrapRequired is true#3813
wing328 merged 1 commit into
swagger-api:2.3.0from
mwgray:unwrapRequired-init-optional-parameters

Conversation

@mwgray

@mwgray mwgray commented Sep 16, 2016

Copy link
Copy Markdown
Contributor

PR checklist

  • Read the contribution guildelines.
  • Ran the shell/batch script under ./bin/ to update Petstore sample so that CIs can verify the change. (For instance, only need to run./bin/{LANG}-petstore.sh if updating the {LANG} (e.g. php, ruby, python, etc) code generator or {LANG} client's mustache templates)
  • Filed the PR against the correct branch: master for non-breaking changes and 2.3.0 branch for breaking (non-backward compatible) changes.

Description of the PR

Previously when running with unwrapRequired=true, the init function would only list required parameters. I added the optional ones with default values to nil.

@wing328

wing328 commented Sep 19, 2016

Copy link
Copy Markdown
Contributor

cc @Edubits @jaz-ah

@wing328

wing328 commented Sep 21, 2016

Copy link
Copy Markdown
Contributor

@mwgray thanks for the PR. Would it be possible to keep the existing init (which only initializes required properties) so that the enhancement is backward compatible?

{{#hasOptionalParams}} mustache tag may be helpful to avoid duplicated init when there's no optional parameter.

@mwgray

mwgray commented Sep 21, 2016

Copy link
Copy Markdown
Contributor Author

It's possible, but having both generated at the same time creates ambiguity for the compiler. I'm using Swift 2.3, and it causes a compiler error if both are generated(not sure about 2.2, or 3.0):

Ambiguous use of 'init(username:password:)'
public var username: String
public var password: String?

public init(username: String) {
    self.username = username
}

public init(username: String, password: String?=nil) {
    self.username = username
    self.password = password
}

It's not clear to me what this change does to prevent backwards compatibility. The signature is the same as the previously generated code(hence the ambiguity)

If you don't set optionals in a init, they are set to the default values, which in this case is nil. In both init functions, all values are being set, it's just the compiler sets password for you in init(username:).

@wing328

wing328 commented Sep 22, 2016

Copy link
Copy Markdown
Contributor

OK. Thanks for the explanation.

@wing328 wing328 merged commit 9f75f8d into swagger-api:2.3.0 Sep 22, 2016
@wing328

wing328 commented Sep 22, 2016

Copy link
Copy Markdown
Contributor

Upgrade Note

When unwrapRequired is set to true, the model constructor init signature has been updated to include both required and optional parameters (as opposed to just required parameters)

@wing328 wing328 changed the title add optional parameters to init when unwrapRequired is true [Swift] Add optional parameters to init when unwrapRequired is true Mar 23, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants