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

Assign to properties with explicit self in init(from decoder:) #696

Merged

Conversation

simonjbeaumont
Copy link
Collaborator

Motivation

Our generated init(from decoder:) assigns to properties without using explicit self. This is a problem because it can produce conflicts with the local variables created in the initializer, e.g. the decoding container, which will then fail to compile for schemas with properties named container, as found by an adopter in #695.

We should probably take a broader pass with regard to the use of explicit self in our generated code, but for now we can resolve this by focussing on this problematic generation, where a schema with named and additional properties.

Modifications

  • Assign to properties with explicit self in init(from decoder:)

Result

Test Plan

  • Add a snippet test.
  • Update the reference tests.

Copy link
Contributor

@czechboy0 czechboy0 left a comment

Choose a reason for hiding this comment

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

Thanks for the quick fix! 🙏

@simonjbeaumont simonjbeaumont added the 🔨 semver/patch No public API change. label Dec 10, 2024
@simonjbeaumont simonjbeaumont enabled auto-merge (squash) December 10, 2024 17:19
@simonjbeaumont simonjbeaumont merged commit 8a3dd4d into apple:main Dec 10, 2024
32 checks passed
simonjbeaumont added a commit that referenced this pull request Dec 11, 2024
### Motivation

In #696 we avoided clashing with a property called `container` in
certain schemas by making use of explicit self in `init(from decoder:)`.
We didn't have access to the OpenAPI document and hadn't caught the case
where this was used in a schema that supported additional properties,
where we also need to avoid the clash in `encode(to encoder:)`.

### Modifications

- Access properties with explicit self in `encode(to encoder:)`.

### Result

- Fixes #695 

### Test Plan

More snippet tests and reference tests.
simonjbeaumont added a commit that referenced this pull request Dec 11, 2024
…and encoders (#700)

### Motivation

In order to address #695 we made some targeted fixes (#696, #699) to use
explicit-self to avoid clashing with schema property names. This
addressed a clash with a specific variable with properties of only
certain schemas, but there are more places where we might encounter
future issues. Specifically, we have other variable names which might
cause a clash (e.g. `storage` and `discriminator`), and there are other
flavours of initializers, encoders, and decoders that were not updated
in the targeted fix.

In this PR, we update all access and assignment in the generated code
dealing with schemas to use explicit-self.


### Modifications

- Use self for member access and assignment in all initializers,
decoders, and encoders.
- Update the reference tests and snippet tests.

### Result

Removed some cases where valid OpenAPI docs would produce non-compiling
code.

### Test Plan

Snippet and reference tests. The latter is actually compiled during the
CI.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🔨 semver/patch No public API change.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Can't use container as a property name of a model
2 participants