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

[SWIFT4] Request does not conform to protocol 'Decodable' #925

Open
Parilar opened this issue Aug 29, 2018 · 15 comments
Open

[SWIFT4] Request does not conform to protocol 'Decodable' #925

Parilar opened this issue Aug 29, 2018 · 15 comments

Comments

@Parilar
Copy link

Parilar commented Aug 29, 2018

Description

Every request got the error "Type 'MyRequestRequest' does not conform to protocol 'Decodable'"

openapi-generator version

3.2.2

Command line used for generation

openapi-generator generate - swagger.json -g swift4 -o api

Using Xcode 10 and Newest Swift version

@Parilar
Copy link
Author

Parilar commented Aug 31, 2018

Problem solved. The solution was that the API got some mistakes.
When an Request defines his params as object :
"properties": { "params": { "type": "object" },
the API generates code which is not Codeable.
public var params: Any

defining an object and referencing it in the request solved the problem

@jacobweber
Copy link
Contributor

I think this should be re-opened. My understanding is that Swagger spec allows free-form objects using type: object.

@allezxandre
Copy link
Contributor

Any pointers to how we should handle type: objects instances?

I haven't tried it yet but I was thinking maybe the generator could use Codable instead of Any?

@cerupcat
Copy link
Contributor

Any updates on this? It looks like using Any anywhere on an object breaks building since it's not Codable.

@lou-lan
Copy link

lou-lan commented Oct 27, 2020

Any updates on this? I have the same problem.

@wing328 wing328 reopened this Oct 27, 2020
@wing328
Copy link
Member

wing328 commented Oct 27, 2020

Instead of Any, what should be a proper type in Swift to map type: object?

@lou-lan
Copy link

lou-lan commented Oct 27, 2020

Hi. This is the Kubernetes api document definition. Value can be int-or-string.

"v1.RollingUpdateDaemonSet": {
    "description": "Spec to control the desired behavior of daemon set rolling update.",
    "properties": {
    "maxUnavailable": {
        "description": "",
        "format": "int-or-string",
        "type": "object"
    }
    },
    "type": "object"
},

@wing328
Copy link
Member

wing328 commented Oct 27, 2020

I'm aware of such definition in Kubernetes as I was involved in the client generation for the Kubernetes API before.

If I remember correctly, it's using OpenAPI v2 to describe the API. In the long term, better upgrade to OpenAPI v3 and use oneOf to describe such property, e.g.

oneOf:
  - type: string
  - type: integer

In Swift, what type would be the best to store something that can be either a string or integer?

@lou-lan
Copy link

lou-lan commented Oct 27, 2020

So that's it, thank you very much for your answer. Kubernetes is written in the go language, and there is no mature implementation of openapi v3 in go, so upgrading from v2 to v3 would be a big project. But it does need to be addressed from the Kubernetes side.

@wing328
Copy link
Member

wing328 commented Oct 27, 2020

What about mapping type: object as a String in Swift for the time being as everything can be represented as a string and developers and convert the string into whatever type that's appropriate and the auto-generated Swift will at least compile?

You can test this workaround/solution using the typeMapping option for the time being: https://openapi-generator.tech/docs/usage/#type-mappings-and-import-mappings

@robmaceachern
Copy link

I'm hitting this issue too.

FYI @wing328 swagger-codegen maps object to a custom JSONObject type. I think that makes sense.

Another option would be to erase the type information with a wrapper type (e.g. AnyCodable) but I prefer a JSONObject type for this situation.

@wing328
Copy link
Member

wing328 commented Feb 9, 2021

FYI @wing328 swagger-codegen maps object to a custom JSONObject type. I think that makes sense.

That's one way to do it (but what if the payload is not JSON (technically it can be XML or something else but from what I know the Swift generator only supports JSON payload at the moment)

@wing328
Copy link
Member

wing328 commented Feb 9, 2021

cc Swift technical committee to see if they've a different opinion on this.

@jgavris (2017/07) @ehyche (2017/08) @Edubits (2017/09) @jaz-ah (2017/09) @4brunu (2019/11)

@4brunu
Copy link
Contributor

4brunu commented Feb 9, 2021

I think using the JSONObject would do it, and it's a better solution than the one we have now.
Looking into the future, supporting oneOf with an enum generated for each response, would be nice.

@robmaceachern
Copy link

technically it can be XML or something else but from what I know the Swift generator only supports JSON payload at the moment

Yeah I guess technically we'd need to look at the contentType of the object and only map to JSONObject if it is application/json.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants