SwiftyLab / MetaCodable

Supercharge Swift's Codable implementations with macros meta-programming.
https://swiftpackageindex.com/SwiftyLab/MetaCodable/main/documentation/metacodable
MIT License
604 stars 22 forks source link

No value associated with key CodingKeys #83

Closed ljd1bg closed 4 months ago

ljd1bg commented 4 months ago

Describe the bug Fails to decode a key that is present (but only after the first time).

DecodingError
      ▿ keyNotFound : 2 elements
        - .0 : CodingKeys(stringValue: "time_zone", intValue: nil)
        ▿ .1 : Context
          - codingPath : 0 elements
          - debugDescription : "No value associated with key CodingKeys(stringValue: \"time_zone\", intValue: nil) (\"time_zone\")."
          - underlyingError : nil

But this is the response (always the same of course):

{
  "id" : "12345-6789-0",
  "opening_times" : {
    "twentyfourseven" : true
  },
  "coordinates" : {
    "longitude" : "9.028339",
    "latitude" : "48.926502"
  },
  "postal_code" : "71739",
  "time_zone" : "Europe\/Berlin"
}

Here is my model:

@Codable
@CodingKeys(.snake_case)
public struct LocationDetails {
    public let id: String
    public let timeZone: String
    public let postalCode: String

    @CodedIn("coordinates")
    @CodedBy(ValueCoder<Double>())
    public let latitude: Double

    @CodedIn("coordinates")
    @CodedBy(ValueCoder<Double>())
    public let longitude: Double

    @CodedIn("opening_times")
    @Default([])
    public let regularHours: [RegularHours]

}

@Codable
@CodingKeys(.snake_case)
public struct RegularHours {
    public let weekday: Int
    public let periodBegin: String
    public let periodEnd: String
}

To Reproduce Steps to reproduce the behavior:

  1. Fetch data from an endpoint first time
  2. Everything parses correctly, everything is populated:
    LocationDetails(id: "12345-6789-0", timeZone: "Europe/Berlin", postalCode: "71739", latitude: 48.926502, longitude: 9.028339, regularHours: [])
  3. Fetch data from the same endpoint again and I get the error mentioned. "No value associated with key CodingKeys(stringValue: \"time_zone\", intValue: nil) (\"time_zone\")."

Expected behavior I expect to correctly parse the model everytime.

Screenshots

Expanded macro:

Screenshot 2024-05-12 at 14 50 39

Environment (please complete the following information, remove ones not applicable):

Additional context The macro looks fine, and I thought it might have something to do with time_zone string, but I commented it out and it gave me the same error (only after the first time) for postal_code as well.

ljd1bg commented 4 months ago

I've tried this:

public struct LocationDetails: Decodable {
    public let id: String
    public let time_zone: String
    public let postal_code: String
}

And it still fails after the first time... I'm very confused 😕

soumyamahunt commented 4 months ago

Have you tried just decoding the JSON string without introducing the endpoint? This seems more like a datapoint issue.

ljd1bg commented 4 months ago

Seems that was the case... strange, thanks! :)