Skip to content

fix(api): handle enum casting in GraphQLSelectionSet snapshot#4164

Open
Take111 wants to merge 1 commit intoaws-amplify:mainfrom
Take111:fix/graphql-enum-casting-in-selection-set
Open

fix(api): handle enum casting in GraphQLSelectionSet snapshot#4164
Take111 wants to merge 1 commit intoaws-amplify:mainfrom
Take111:fix/graphql-enum-casting-in-selection-set

Conversation

@Take111
Copy link
Copy Markdown

@Take111 Take111 commented Mar 4, 2026

Issue #

#3953

Related PR in amplify-codegen: aws-amplify/amplify-codegen#991

Description

GraphQL responses with enum fields store raw String values in the snapshot dictionary, but codegen-generated property accessors cast directly (e.g. snapshot["status"]! as! PostStatus), causing a crash.

Add convertFieldValues/convertValue helpers to GraphQLSelectionSet that use GraphQLField.type metadata to convert scalar values via JSONDecodable.init(jsonValue:), leveraging the existing RawRepresentable extension for String-to-Enum conversion.

General Checklist

  • Added new tests to cover change, if needed
  • Build succeeds with all target using Swift Package Manager
  • All unit tests pass
  • All integration tests pass
  • Security oriented best practices and standards are followed (e.g. using input sanitization, principle of least privilege, etc)
  • Documentation update for the change if required
  • PR title conforms to conventional commit style
  • New or updated tests include Given When Then inline code documentation and are named accordingly testThing_condition_expectation()
  • If breaking change, documentation/changelog update with migration instructions

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

GraphQL responses with enum fields store raw String values in the snapshot dictionary, but codegen-generated property accessors cast directly (e.g. `snapshot["status"]! as! PostStatus`), causing a crash.

Add convertFieldValues/convertValue helpers to GraphQLSelectionSet that use GraphQLField.type metadata to convert scalar values via JSONDecodable.init(jsonValue:), leveraging the existing RawRepresentable extension for String-to-Enum conversion.
@Take111 Take111 marked this pull request as ready for review March 4, 2026 12:11
@Take111 Take111 requested a review from a team as a code owner March 4, 2026 12:11
Copilot AI review requested due to automatic review settings March 4, 2026 12:11
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Updates the APISwift compatibility fixtures/tests to ensure GraphQL response decoding converts enum raw String values into the expected generated enum types (avoiding runtime as! Enum cast crashes), aligned with the referenced amplify-codegen change.

Changes:

  • Add convertFieldValues / convertValue helpers to GraphQLSelectionSet.init(from:) to coerce scalar snapshot values using GraphQLField.type metadata (including enums via JSONDecodable).
  • Add unit tests validating enum decoding for non-optional, optional, null, nested-list, and unknown enum cases.
  • Apply the same GraphQLSelectionSet snapshot conversion behavior across the APIHostApp generated API.swift fixtures.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.

Show a summary per file
File Description
AmplifyPlugins/API/Tests/AWSAPIPluginTests/APISwiftCompatibility/APISwiftTests.swift Adds coverage for enum decoding behavior (including unknown/null/nested list cases).
AmplifyPlugins/API/Tests/AWSAPIPluginTests/APISwiftCompatibility/API.swift Enhances GraphQLSelectionSet decoding to convert scalar values (incl. enums) based on selection metadata; adds test-only enum/query types used by the new tests.
AmplifyPlugins/API/Tests/APIHostApp/AWSAPIPluginGraphQLUserPoolTests/API.swift Mirrors the updated GraphQLSelectionSet decoding conversion logic in host app fixture.
AmplifyPlugins/API/Tests/APIHostApp/AWSAPIPluginGraphQLIAMTests/API.swift Mirrors the updated GraphQLSelectionSet decoding conversion logic in host app fixture.
AmplifyPlugins/API/Tests/APIHostApp/AWSAPIPluginFunctionalTests/API.swift Mirrors the updated GraphQLSelectionSet decoding conversion logic in host app fixture.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@cadivus
Copy link
Copy Markdown
Member

cadivus commented Mar 6, 2026

Hi @Take111,

Thank you for your contribution to the AWS Amplify Library! Our team will review your PR and provide feedback soon.

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