yonaskolb / SwagGen

OpenAPI/Swagger 3.0 Parser and Swift code generator
MIT License
626 stars 146 forks source link

Type 'ModelWithAdditionalProperties' does not conform to protocol 'Equatable' #220

Open Cy-4AH opened 4 years ago

Cy-4AH commented 4 years ago

Petstore spec generates

Type 'ModelWithAdditionalProperties' does not conform to protocol 'Equatable'

if generated with options modelType:struct modelInheritance:false

yonaskolb commented 4 years ago

Happy to accept an easy PR for this

iainmerrick commented 2 years ago

I just hit what I think is a related issue, where an OpenAPI spec that includes a bare object field generates a model with [String: Any]? that isn't Equatable.

In our codebase we have an enum that looks like this:

enum JSONAny: Codable, Equatable {
    case bool(_ value: Bool)
    case int(_ value: Int)
    case float(_ value: Double)
    case string(_ value: String)
    case array(_ value: [JSONAny])
    case dict(_ value: [String: JSONAny])
    case null
    ...

Generating with --option anyType:JSONAny would fix it (and maybe ModelWithAdditionalProperties too) if I could get JSONAny in scope. Some possible ways of doing that:

  1. Make my own template and add JSONAny.swift (but I'd prefer to stick to the standard one!)
  2. Add JSONAny.swift to the standard template (happy to contribute this if there's interest)
  3. Have the generator include an extra file -- could this be added? e.g. --option addFile:JSONAny.swift?
  4. Have the generator add an import statement -- could this be added? e.g. --option addImport:JSONAny?

@yonaskolb let me know if you like any of those options, and I can put together a PR.

iainmerrick commented 2 years ago

@Cy-4AH --option anyType:Data may be another workaround