postgres-nio icon indicating copy to clipboard operation
postgres-nio copied to clipboard

Decoding JSONB_AGG_STRICT does not work currently

Open thoven87 opened this issue 1 year ago • 0 comments

Describe the issue

Try decoding JSONB_AGG_STRICT fails

Vapor version

N/A

Operating system and version

macOS 15.0

Swift version

Swift Package Manager - Swift 6.0.0-dev

Steps to reproduce

say you one has the followings:


struct Person: Codable {
    let name: String,
    let id: Int,
    let createdAt: Date
 }

struct Team: Codable, Equatable, PostgresCodable {
    let id: Int
    let peers: [Person]
 }

Decoding logic

let stream = try await self.client.query(
"""SELECT
  team.id,
  JSONB_AGG_STRICT( -- DROP NULL VALUES
    JSON_BUILD_OBJECT(
      'name', people.name,
      'id', people.id,
      'createdAt', people.created_at
    )
  ) AS team_mates
FROM profiles.team t
JOIN profiles.people p
ON t.id = p.team_id
GROUP BY 1"""
)
let team = [Team]
for try await (teamID, teamMates) in stream.decode((Int, [Person]).self) {
 team.append(
    .init(
        id: teamID,
        peers: teamMates
    )
  )
 }


### Outcome

The program will throw Runtime exception 

message=Swift.DecodingError.typeMismatch(Swift.Dictionary<Swift.String, Any>, Swift.DecodingError.Context(codingPath: [], debugDescription: "Expected to decode Dictionary<String, Any> but found an array instead.", underlyingError: nil))

Instead of outputting 

and array of Team

### Additional notes

_No response_

thoven87 avatar Jul 22 '24 19:07 thoven87