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

Compile error: "Reference to member 'value' cannot be resolved without a contextual type" #1

Closed
broadwaylamb opened this issue Apr 20, 2018 · 1 comment

Comments

@broadwaylamb
Copy link

broadwaylamb commented Apr 20, 2018

The library cannot be compiled with Swift 4.1 because of the error in OpenAPIExample.swift, line 47:

try container.encode(value, forKey: .value) // ERROR: Reference to member 'value' cannot be resolved without a contextual type

That's because the value property is of type Optional<Any>. Prior to Swift 4.1 Optional conformed to the Encodable protocol unconditionally at compile time, but attempting to encode its payload if the payload is not itself Encodable would crash at runtime.

Swift 4.1 introduced conditional conformances, and now this error is caught at compile time, which results in the inability to compile Swiftgger.

My suggestion would be to make the OpenAPIExample class generic:

-public class OpenAPIExample: Encodable
+public class OpenAPIExample<Value: Encodable>: Encodable {

     public private(set) var ref: String?
     public private(set) var summary: String?
     public private(set) var description: String?
-    public private(set) var value: Any?
+    public private(set) var value: Value?
     public private(set) var externalValue: String?

However, I haven't tried this and not sure if it'll work with the rest of the project.

I'm looking forward to be able to use Swiftgger in my Vapor project 😊

@mczachurski
Copy link
Owner

Fixed in commit db3140b.

It's not perfect solution, however now Swiftgger didn't provide API to set up examples objects in OpenAPI JSON.

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

No branches or pull requests

2 participants