Skip to content

Stop including the discriminator in the JSON id by default#34208

Merged
ajcvickers merged 4 commits intomainfrom
NormalForNorfolk
Aug 2, 2024
Merged

Stop including the discriminator in the JSON id by default#34208
ajcvickers merged 4 commits intomainfrom
NormalForNorfolk

Conversation

@ajcvickers
Copy link
Copy Markdown
Contributor

Fixes #34179

There are also new APIs on the entity type and model builders to facilitate optional behaviors, including reverting to the pre-9 behavior:

  • Entity().IncludeDiscriminatorInJsonId();
  • Entity().IncludeRootDiscriminatorInJsonId();
  • Entity().AlwaysCreateShadowIdProperty();

Note that this change requires regeneration of the Northwind database.

@ajcvickers ajcvickers requested a review from a team July 11, 2024 13:58
Comment thread src/EFCore.Cosmos/Infrastructure/Internal/CosmosModelValidator.cs
Comment thread src/EFCore.Cosmos/Infrastructure/Internal/CosmosModelValidator.cs
Comment thread src/EFCore.Cosmos/Metadata/Conventions/CosmosJsonIdConvention.cs Outdated
Comment thread src/EFCore.Cosmos/Metadata/Conventions/CosmosJsonIdConvention.cs Outdated
Comment thread src/EFCore.Cosmos/Metadata/Conventions/CosmosJsonIdConvention.cs Outdated
Comment thread src/EFCore.Cosmos/Metadata/Conventions/CosmosJsonIdConvention.cs Outdated
Comment thread src/EFCore.Cosmos/Metadata/Conventions/CosmosJsonIdConvention.cs Outdated
Comment thread src/EFCore.Cosmos/Metadata/Conventions/CosmosJsonIdConvention.cs
Comment thread src/EFCore.Cosmos/Metadata/Conventions/CosmosJsonIdConvention.cs Outdated
Comment thread src/EFCore.Cosmos/Metadata/Conventions/CosmosJsonIdConvention.cs Outdated
Comment thread src/EFCore.Cosmos/Metadata/Internal/JsonIdDefinition.cs Outdated
Comment thread src/EFCore.Cosmos/Metadata/Internal/JsonIdDefinitionFactory.cs
Comment thread src/EFCore.Cosmos/Metadata/Conventions/CosmosJsonIdConvention.cs
Comment thread src/EFCore.Cosmos/Infrastructure/Internal/CosmosModelValidator.cs
Comment thread src/EFCore.Cosmos/Metadata/Conventions/CosmosKeyAugmenterConvention.cs Outdated
Comment thread src/EFCore.Cosmos/Metadata/Conventions/CosmosKeyAugmenterConvention.cs Outdated
Comment thread src/EFCore.Cosmos/Metadata/Conventions/CosmosJsonIdConvention.cs Outdated
Comment thread src/EFCore.Cosmos/Metadata/Conventions/CosmosJsonIdConvention.cs Outdated
Comment thread src/EFCore.Cosmos/Metadata/Conventions/CosmosJsonIdConvention.cs Outdated
Comment thread src/EFCore.Cosmos/Metadata/Conventions/CosmosJsonIdConvention.cs
Copy link
Copy Markdown
Member

@AndriySvyryd AndriySvyryd left a comment

Choose a reason for hiding this comment

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

How do we make sure that the Northwind database is recreated on the CI with the new values and is also correctly recreated with the old values when code from an older branch runs?

Comment thread src/EFCore.Cosmos/Extensions/CosmosEntityTypeBuilderExtensions.cs
Comment thread src/EFCore.Cosmos/Metadata/Conventions/CosmosJsonIdConvention.cs Outdated
@roji
Copy link
Copy Markdown
Member

roji commented Jul 13, 2024

How do we make sure that the Northwind database is recreated on the CI with the new values and is also correctly recreated with the old values when code from an older branch runs?

IMHO that's another good reason to not rely on a persistent database in CI, but rather to just use the emulator, where it's guaranteed to be recreated each time (I think we agreed we want to go in that direction anyway, possibly except for specific RBAC tests which can't be executed with the emulator?).

If we do want to stay with non-emulator tests, we could simply recreate the database each time, but on Ci only (to avoid making local runs slow), e.g. by checking the CI environment variable.

@AndriySvyryd
Copy link
Copy Markdown
Member

IMHO that's another good reason to not rely on a persistent database in CI, but rather to just use the emulator, where it's guaranteed to be recreated each time (I think we agreed we want to go in that direction anyway, possibly except for specific RBAC tests which can't be executed with the emulator?).

We need to do #19973 first

If we do want to stay with non-emulator tests, we could simply recreate the database each time, but on Ci only (to avoid making local runs slow), e.g. by checking the CI environment variable.

That would still not work for tests from old branches. We'd need to rename the database.

@ajcvickers
Copy link
Copy Markdown
Contributor Author

@roji @AndriySvyryd New version up attempting to follow Andriy's guidance.

@ajcvickers ajcvickers requested review from a team, AndriySvyryd and roji July 18, 2024 17:10
@ajcvickers ajcvickers force-pushed the NormalForNorfolk branch 4 times, most recently from bc508ac to 0c548b5 Compare July 23, 2024 17:00
@ajcvickers
Copy link
Copy Markdown
Contributor Author

@roji This is now updated with fixes for discriminator checking and more tests. Can you check the test baselines to make sure you think we are doing ReadItem or not as appropriate? I commented on each test case where we don't do ReadItem indicating why not for that case.

Copy link
Copy Markdown
Member

@roji roji left a comment

Choose a reason for hiding this comment

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

Hey, here's a review of the ReadItemPartitionKey tests only. The actual behavior seems correct to me (i.e. I haven't noticed a case where we do ReadItem when we shouldn't or vice versa). So below see mainly some comments about the structure/factoring of these tests.

As a general comment, there's maybe a bit more systematic duplication of tests across various scenarios than I'd have personally done... This is definitely a matter of personal style/taste on how to test: more black box-style "cover all permutations" vs. more white-box "think about specific scenarios you want/need to cover" (I lean much more in the latter direction). For example, do we think we need all the duplication along the only/non-only axis, as well as everything on the leaf. I'm also noting that there seem to be relatively few tests which actually differ across the different implementations (e.g. HierarchicalPartitionKeyEntity is practically identical across all versions).

But I think it's all OK, hopefully it doesn't cost us in terms of maintainability/complexity.

Regardless FYI it took me a while to understand all the test factoring here and realize what extends from what and why :) It makes sense to me now, but maybe a comment on each test class to explain what it's there for could be useful for our future selves.

{
base.OnModelCreating(modelBuilder, context);

modelBuilder.IncludeDiscriminatorInJsonId();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Any specific reason to do this on this specific test suite (not pushing back)?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Because otherwise the test data results in conflicts in the JSON id values.

Comment thread test/EFCore.Cosmos.FunctionalTests/EndToEndCosmosTest.cs
Comment thread test/EFCore.Cosmos.FunctionalTests/FindCosmosTest.cs
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Given we're gonna have so many ReadItemPartitionKey query tests, maybe they deserve their own sub-folder...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Are you asking me to put them in a subfolder? Happy to do so, if that's what you want.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yeah that was the idea

.Where(e => e.Payload.Contains("3") && e.PartitionKey1 == "PK1" && e.PartitionKey2 == 1 && e.PartitionKey3));
await base.Predicate_with_hierarchical_partition_key_and_additional_things_in_predicate();

// Not ReadItem because no primary key value
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Also just additional things in predicate

@ajcvickers
Copy link
Copy Markdown
Contributor Author

As a general comment, there's maybe a bit more systematic duplication of tests across various scenarios than I'd have personally done... This is definitely a matter of personal style/taste on how to test: more black box-style "cover all permutations" vs. more white-box "think about specific scenarios you want/need to cover" (I lean much more in the latter direction).

I very much believe that if I only do "white box" testing, then I will ship significantly more bugs. This is based on my experience of writing tests in this way and frequently finding a bug in a scenario I had never even thought of. So, at least for people of my ability, this will be a trade off for the advantages of having fewer tests against us shipping more bugs.

@ajcvickers ajcvickers requested a review from roji July 28, 2024 11:26
@ajcvickers ajcvickers force-pushed the NormalForNorfolk branch 2 times, most recently from f813158 to ff8e1e7 Compare July 28, 2024 13:27
Comment thread src/EFCore.Cosmos/Metadata/Conventions/CosmosJsonIdConvention.cs Outdated
Comment thread src/EFCore.Cosmos/Metadata/Conventions/CosmosJsonIdConvention.cs Outdated
Comment thread src/EFCore.Cosmos/Metadata/Conventions/CosmosJsonIdConvention.cs Outdated
Comment thread src/EFCore.Cosmos/Metadata/Conventions/CosmosJsonIdConvention.cs Outdated
Comment thread test/EFCore.Cosmos.FunctionalTests/ModelBuilding/CosmosModelBuilderGenericTest.cs Outdated
Comment thread test/EFCore.Cosmos.FunctionalTests/ReadItemTest.cs Outdated
Copy link
Copy Markdown
Member

@AndriySvyryd AndriySvyryd left a comment

Choose a reason for hiding this comment

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

There aren't any blocking issues

roji and others added 3 commits August 2, 2024 12:30
Fixes #34179

There are also new APIs on the entity type and model builders to facilitate optional behaviors, including reverting to the pre-9 behavior:

- Entity<Blog>().IncludeDiscriminatorInJsonId();
- Entity<Blog>().IncludeRootDiscriminatorInJsonId();
- Entity<Blog>().AlwaysCreateShadowIdProperty();

Note that this change requires regeneration of the Northwind database.
@ajcvickers ajcvickers merged commit 41e511f into main Aug 2, 2024
@ajcvickers ajcvickers deleted the NormalForNorfolk branch August 2, 2024 15:36
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.

Cosmos: Use 1:1 mapping for .NET Id to JSON id unless explicitly configured otherwise

3 participants