Skip to content

Fix JSONDecoder.decode(_,from:) behavior #4807

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

Merged
merged 2 commits into from
Aug 11, 2023

Conversation

ypli-chn
Copy link
Contributor

@ypli-chn ypli-chn commented Aug 3, 2023

JSONDecoder.decode(_,from:) accepts a generic parameter, but it does not use any concrete type in practice.
This PR fixes that use parameter type instead of T.self.

This PR solves #4806

Example Code

import Foundation

class Base: Codable { }
class Child: Base { }

func dynamicType() -> Base.Type {
    return Child.self
}

let data = "{}".data(using: .utf8)!

let classType = dynamicType()
let instance = try JSONDecoder().decode(classType, from: data)
print("type: \(instance) => \(type(of: instance))")

on macOS:

type: JSONDecoderExample.Child => Child

on Linux:

type: JSONDecoderExample.Base => Base

@lxbndr
Copy link
Contributor

lxbndr commented Aug 5, 2023

Would you mind adding a test case to the TestJSONEncoder? This is a severe regression, and we would like to catch similar issues in future.

@ypli-chn
Copy link
Contributor Author

ypli-chn commented Aug 7, 2023

Sure. I added a method named test_childTypeDecoder to test this issue.

@stephentyrone stephentyrone requested a review from glessard August 7, 2023 14:36
Copy link
Contributor

@glessard glessard left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the contribution!

@glessard
Copy link
Contributor

glessard commented Aug 8, 2023

@swift-ci please test

@glessard
Copy link
Contributor

glessard commented Aug 8, 2023

The discrepancy you describe in #4806 has probably been present for a long time. It appears that it is fixed already in https://github.com/apple/swift-foundation, which will become the Foundation implementation for the Linux and Windows toolchains at a future point. I will make sure that a test like this exists in swift-foundation.

@glessard
Copy link
Contributor

glessard commented Aug 9, 2023

@swift-ci please test windows platform

@glessard
Copy link
Contributor

The macOS test failure is a known issue.

@glessard glessard merged commit 9f53cc5 into swiftlang:main Aug 11, 2023
@glessard
Copy link
Contributor

glessard commented Oct 9, 2023

Circling back, the behaviour fixed here is tested for in the following test of swift-foundation:
https://github.com/apple/swift-foundation/blob/62500a5d7921cc61703c2a1b4c961adb12dc80c3/Tests/FoundationEssentialsTests/JSONEncoderTests.swift#L680

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

Successfully merging this pull request may close these issues.

3 participants