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

Accessing an unset deprecated field in input causes a crash #3506

Open
pixelmatrix opened this issue Jan 24, 2025 · 1 comment
Open

Accessing an unset deprecated field in input causes a crash #3506

pixelmatrix opened this issue Jan 24, 2025 · 1 comment
Labels
bug Generally incorrect behavior needs investigation

Comments

@pixelmatrix
Copy link

Summary

We recently deprecated a field in an input object.

As a result, codegen created a new default initializer that that excludes that field.

public init(newField: GraphQLNullable<MyType>) {
  __data = InputDict(["newField": newField])
}

@available(*, deprecated, message: "Argument 'oldField' is deprecated")
public init(oldField: GraphQLNullable<MyType>) {
  __data = InputDict(["oldField": oldField])
}

Now, when accessing the deprecated field through the input object, it crashes because that field is not actually present in the underlying InputDict.

Generated code for the property looks like:

@available(*, deprecated, message: "Use newField instead")
public var oldField: GraphQLNullable<MyType> {
  get { __data["oldField"] }
  set { __data["oldField"] = newValue }
}

…which calls:

public subscript<T: GraphQLOperationVariableValue>(key: String) -> T {
  get { data[key] as! T }
  set { data[key] = newValue }
}

Version

1.15.3

Steps to reproduce the behavior

  1. Deprecate a field of an input object in your schema
  2. Run codegen to pick up the field
  3. Create an instance of the input object using the new default initializer
let input = MyInput()
  1. Try accessing the deprecated field on the object
if input.oldField == .none { // crash!
  print("worked")
}

Logs

Could not cast value of type 'Swift.Optional<ApolloAPI.GraphQLOperationVariableValue>' (0x1f05300a0) to 'ApolloAPI.GraphQLNullable<Swift.String>' (0x1f052f388).

Anything else?

No response

@pixelmatrix pixelmatrix added bug Generally incorrect behavior needs investigation labels Jan 24, 2025
@calvincestari
Copy link
Member

Thanks for raising the issue @pixelmatrix. We'll take a look at the design intent here and get back to you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Generally incorrect behavior needs investigation
Projects
None yet
Development

No branches or pull requests

2 participants