Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Hashable for Struct #9166

Merged
merged 3 commits into from
Apr 6, 2021
Merged

Add Hashable for Struct #9166

merged 3 commits into from
Apr 6, 2021

Conversation

maoyama
Copy link
Contributor

@maoyama maoyama commented Apr 4, 2021

Resolves #9030

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • Run the following to build the project and update samples:
    ./mvnw clean package 
    ./bin/generate-samples.sh
    ./bin/utils/export_docs_generators.sh
    
    Commit all changed files.
    This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
    These must match the expectations made by your contribution.
    You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*.
    For Windows users, please run the script in Git BASH.
  • File the PR against the correct branch: master, 5.1.x, 6.0.x
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.
    @jgavris (2017/07) @ehyche (2017/08) @Edubits (2017/09) @jaz-ah (2017/09) @4brunu (2019/11)

@wing328
Copy link
Member

wing328 commented Apr 4, 2021

@maoyama thanks for the PR. What about adding an option (e.g. hashableStruct) to add x-swift-hashable to all models in postProcessModels if x-swift-hashable is not yet defined in the model?

Ref: https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenConstants.java#L133

@maoyama
Copy link
Contributor Author

maoyama commented Apr 4, 2021

@wing328 I didn't want to add more options and make it more complicated for the user. I like the simple specs by this PR.

@wing328
Copy link
Member

wing328 commented Apr 4, 2021

Understood. Let's see if the Swift technical committee has a different option on this as I do not know if everyone likes hashable struct.

@wing328
Copy link
Member

wing328 commented Apr 5, 2021

Another use case worth considering is that a user wants all models except a few models to extends hashable. An option I described above can achieve that.

@4brunu
Copy link
Contributor

4brunu commented Apr 5, 2021

I don't see any downside in making all the models conform to Hashable.
For me we can go with all models conform to Hashable and if an issue appears to someone we can introduce a flag to disable some cases.

@wing328
Copy link
Member

wing328 commented Apr 6, 2021

OK. Let's go with this and add an option if needed later.

@wing328 wing328 merged commit c3b7ecf into OpenAPITools:master Apr 6, 2021
@fl034
Copy link
Contributor

fl034 commented May 12, 2021

Hey guys @4brunu . I've experienced a downside of this feature. I have many custom implementations of Hashable that seem to be ignored now, since the model has a synthesized implementation.
In my custom implementations I exclude for example timestamps like "updatedAt".

So for me it will be crucial to be able to control this feature. For me it would be best to be able to control this at model-level. Like with x-swift-hashable or x-swift-disable-hashable.

We could also have a configuration flag to set the default behavior.

@4brunu
Copy link
Contributor

4brunu commented May 12, 2021

A configuration flag to control the behaviour seems a good option to me.
@fl034 could you please open a PR with this?

@fl034
Copy link
Contributor

fl034 commented May 13, 2021

Sorry @4brunu I don't have resources for it this time. My workaround was to downgrade to 5.1.0.
I added an issue to track it. #9473

@maoyama
Copy link
Contributor Author

maoyama commented May 13, 2021

@fl034 @4brunu

I have many custom implementations of Hashable that seem to be ignored now, since the model has a synthesized implementation.

I want to know more about this custom implementation of Hashable that seem to be ignored.
I think that we can exclude the specified property by custom implementation of Hashable like below:

// Custom implementations of Hashable
extension Animal {
    static public func == (lhs: Animal, rhs: Animal) -> Bool {
        return lhs.className == rhs.className
    }

    public func hash(into hasher: inout Hasher) {
        hasher.combine(className)
    }
}

// Generated Model
public struct Animal: Codable, Hashable {
    public var className: String
    public var color: String? = "red"

    public init(className: String, color: String? = "red") {
        self.className = className
        self.color = color
    }
}

(If this feature has a downside, we need an option to control its behavior.)

@fl034
Copy link
Contributor

fl034 commented May 13, 2021

@maoyama I suppose when the extension is in the same file, the custom implementation is used. But this is not applicable for generated code files, of course.

In my case I even get a compiler warning, that my implementations will be ignored.

Bildschirmfoto 2021-05-12 um 11 54 11

@maoyama
Copy link
Contributor Author

maoyama commented May 14, 2021

@fl034 Thank you for your reply.
I understood that we need an option to control its behavior.

Here are the results of the custom implementation I've tried:

  • Hashable can be custom implementation in the same module, not limited to the defined file.
  • Hashable cannot be custom implementation with different modules. (Equatable is possible)

I can open a PR with adding options.

@4brunu
Copy link
Contributor

4brunu commented May 14, 2021

@maoyama a PR is welcome 👍

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.

[REQ] [Swift5] Hashable struct models
4 participants