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

Throw error when an invalid key is present in a JSON Apollo configuration #3125

Merged
merged 9 commits into from
Jul 19, 2023

Conversation

Iron-Ham
Copy link
Contributor

@Iron-Ham Iron-Ham commented Jul 17, 2023

What are you trying to accomplish?

closes #2942

Notify users when they create an invalid apollo-codegen-config.json.

What approach did you choose and why?

I created an AnyCodingKey struct which cannot fail. I create sets of all the keys present in the input, and then make sure that the input keys are a subset of the CodingKeys defined for that object.

Anything you want to highlight for special attention from reviewers?

Is the error text here sufficient to explain to users that something is wrong?

@netlify
Copy link

netlify bot commented Jul 17, 2023

👷 Deploy request for apollo-ios-docs pending review.

Visit the deploys page to approve it

Name Link
🔨 Latest commit 9235769

@Iron-Ham Iron-Ham changed the title Error when an invalid key is present in the JSON apollo configuration Throw error when an invalid key is present in a JSON Apollo configuration Jul 17, 2023
Copy link
Member

@calvincestari calvincestari left a comment

Choose a reason for hiding this comment

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

Thanks @Iron-Ham!

It looks like the key validation has been implemented in:

  • FileOutput
  • OutputOptions
  • ExperimentalFeatures
  • base (ApolloCodegenConfiguration)

It's not implemented in these structs though:

  • FileInput
  • SchemaTypesFileOutput
  • ConversionStrategies
  • OperationDocumentFormat
  • SelectionSetInitializers

We should decide if we're going to enforce a 100% key-correct configuration file or only perform key validation on the most common places we've noticed incorrect configurations.

  • If we're going to enforce 100% key-correctness then some of the structs will need to get decode implementations because at the moment we've only done that where custom behaviour was needed.
  • If we're only going to validate at the most error-prone structs then we can likely remove validation of ExperimentalFeatures.

I'm not sure it's worth the effort to go the 100% key-correctness route and I think I'd instead choose to just validate the most error-prone. Thoughts?

guard allKeys.isSubset(of: validKeys) else {
throw DecodingError.typeMismatch(type, DecodingError.Context.init(
codingPath: container.codingPath,
debugDescription: "Unrecognized key found",
Copy link
Member

@calvincestari calvincestari Jul 18, 2023

Choose a reason for hiding this comment

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

We could maybe improve this by being more explicit in which keys are unrecognized; subtracting validKeys from allKeys?

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm wondering if this could be put into a protocol that automatically calls this function so we don't have to remember to call it in each of the decoder functions? We could call it StrictlyKeyedDecodable.

I'm not sure if that would cause more complexity with the CodingKeys?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm wondering if this could be put into a protocol that automatically calls this function so we don't have to remember to call it in each of the decoder functions?

That was my first thought, but I shied away from it when I realized that Codable (Encodable + Decodable) doesn't have any reference to CodingKeys. Adds a lot of complexity for a function that we'd otherwise call a handful of times in one file.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yupp, that makes sense. If we were using this all over might be worth it, but for this one place, totally fine.

@Iron-Ham
Copy link
Contributor Author

Agreed that we should really just tackle the most error prone ones. I'll remove experimental features and adjust the error String.

Copy link
Contributor

@AnthonyMDev AnthonyMDev left a comment

Choose a reason for hiding this comment

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

Thanks so much for putting this together @Iron-Ham . I think this could be improved by turning it into a protocol, and adding additional test cases, but this is great!

guard allKeys.isSubset(of: validKeys) else {
throw DecodingError.typeMismatch(type, DecodingError.Context.init(
codingPath: container.codingPath,
debugDescription: "Unrecognized key found",
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm wondering if this could be put into a protocol that automatically calls this function so we don't have to remember to call it in each of the decoder functions? We could call it StrictlyKeyedDecodable.

I'm not sure if that would cause more complexity with the CodingKeys?

@Iron-Ham Iron-Ham requested a review from calvincestari July 19, 2023 01:02
Copy link
Member

@calvincestari calvincestari left a comment

Choose a reason for hiding this comment

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

LGTM - @AnthonyMDev, thoughts on the protocol route?

Copy link
Contributor

@AnthonyMDev AnthonyMDev left a comment

Choose a reason for hiding this comment

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

@AnthonyMDev AnthonyMDev merged commit c338ee9 into apollographql:main Jul 19, 2023
AnthonyMDev pushed a commit that referenced this pull request Aug 2, 2023
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.

Generate error when codegen configuration keys are misplaced
3 participants