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

[Cosmos] Add option for disabling auto id generation in create_item #15715

Merged
merged 12 commits into from
Jan 15, 2021

Conversation

zfoster
Copy link
Contributor

@zfoster zfoster commented Dec 8, 2020

Replaces hardcoded automaticIdGeneration flag with option in container create_item.

Does anyone familiar with this project know why this was flagged True?

cc @annatisch @Rodrigossz @southpolesteve

@ghost ghost added the Cosmos label Dec 8, 2020
@@ -462,6 +462,7 @@ def create_item(
pre_trigger_include=None, # type: Optional[str]
post_trigger_include=None, # type: Optional[str]
indexing_directive=None, # type: Optional[Any]
disable_automatic_id_generation=None, #type: Optional[bool]
Copy link
Member

@annatisch annatisch Dec 8, 2020

Choose a reason for hiding this comment

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

Could this be moved into kwargs?
(The :param label in the docstring would move to :keyword to match.)

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe it's true to make it easier for those that don't have an id to use.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@annatisch I see no reason why not, however all of the other similar args follow this pattern

Copy link
Member

Choose a reason for hiding this comment

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

Thanks @zfoster - we already have more positional optional parameters in the method signatures than we would generally advise, as making these parameters keyword only gives us more options for non-breaking changes in the future.
So going forward we should generally add new parameters to kwargs unless there's a very compelling reason not to.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

or by chance know who would know? :)

Copy link
Contributor

Choose a reason for hiding this comment

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

Spark Connector creates an id by default. Maybe it's for consistency.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's doing the opposite here I think? We have hardcoded to DISABLE ID generation

Copy link
Contributor

Choose a reason for hiding this comment

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

@zfoster I think that comment is just wrong. We should match whatever the JS SDK does here.

@@ -475,6 +475,7 @@ def create_item(
:param pre_trigger_include: trigger id to be used as pre operation trigger.
:param post_trigger_include: trigger id to be used as post operation trigger.
:param indexing_directive: Indicate whether the document should be omitted from indexing.
:keyword bool disable_automatic_id_generation: Enable automatic id generation if no id present.
Copy link
Member

Choose a reason for hiding this comment

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

The default value is True right? If we want non-breaking behaviour, we will probably need to keep automatic generation as disabled by default. So I think we could do that one of two ways:

  • Keep the parameter called disable_auto_id_gen, and document the default as being True (and set it as such if not present).
  • Rename the parameter to enable_auto_id_gen, document the default as being False, and set the request option to the opposite.

Copy link
Member

Choose a reason for hiding this comment

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

@zfoster - on thinking about this a bit more - I think we should use the latter option - as I believe it conveys the behaviour more intuitively. i.e. update the kwarg name to enable_auto_id_generation, and update the docstring to state explicitly that this is False by default.
It would mean also updating the implementation to the opposite:

request_options["disableAutomaticIdGeneration"] = not kwargs.pop('disable_auto_id_gen', True)

Could we please also add a test (or update an existing test) to make sure that this new flag is tested?

@@ -488,7 +489,6 @@ def create_item(
request_options = build_options(kwargs)
response_hook = kwargs.pop('response_hook', None)

request_options["disableAutomaticIdGeneration"] = True
Copy link
Member

Choose a reason for hiding this comment

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

As I mentioned above - we probably need to keep this here to ensure consistent behaviour for existing customers, allowing the parameter overwrite:

request_options["disableAutomaticIdGeneration"] = kwargs.pop('disable_auto_id_gen', True)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

that makes sense - I was thinking this wouldn't affect users since it requires them to use an ID already, but I can see people relying on getting an error when they do not pass an ID

Copy link
Member

Choose a reason for hiding this comment

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

Thanks @zfoster - just to clarify, when you say "requires them to use an ID already", whereabouts is that?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

request_options["disableAutomaticIdGeneration"] = True

having this hardcoded means you have to supply an ID in the args when creating items

Copy link
Contributor Author

Choose a reason for hiding this comment

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

if i'm reading it right

Copy link
Member

Choose a reason for hiding this comment

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

Interesting - in that case what is the keyword argument people would need to have been supplying? And is that what our tests are doing? (sorry I should have dug deeper into this earlier)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

we didn't have a kwarg for it afaict. honestly, I'm not sure why this is the way it is, it's unique to this sdk. Our tests do supply IDs and from this we can assume our users do too, however I can flesh out why I think your comment about ensuring consistent behavior is still important:

We may have users who generate IDs or create them programmatically, and rely on the fact that we don't ever automatically generate them. The way this is set up now, we keep this consistent and never generate IDs unless they explicitly pass the kwarg in (same behavior as before)

@zfoster
Copy link
Contributor Author

zfoster commented Dec 16, 2020

@annatisch how does this look now?

@zfoster
Copy link
Contributor Author

zfoster commented Jan 4, 2021

@annatisch can you take another look at this?

Copy link
Contributor

@southpolesteve southpolesteve left a comment

Choose a reason for hiding this comment

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

Approved. As I understand it, this is keeping the existing behavior and adding an option to disable it.

I get why this if confusing. I think this was just done backwards in the original SDK. If we ever do a 5.0 we should look at flipping the behavior to be in sync with the other Cosmos SDKs.

@@ -488,7 +489,7 @@ def create_item(
request_options = build_options(kwargs)
response_hook = kwargs.pop('response_hook', None)

request_options["disableAutomaticIdGeneration"] = True
request_options["enableAutomaticIdGeneration"] = kwargs.pop('enable_automatic_id_generation', False)
Copy link
Member

Choose a reason for hiding this comment

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

The request_options key here is a service value - not sure it can be changed like this?
I think you'll need something like:

        request_options["disableAutomaticIdGeneration"] = not kwargs.pop('enable_automatic_id_generation', False)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah definitely - I can't push at the moment, having issues authenticating to github. I slapped something together and then was testing pushing. I'll reping this when ready for review

Copy link
Member

Choose a reason for hiding this comment

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

Awesome, thanks! :)
Good luck with the github trouble - let me know if you need me to make any updates from my end.

@zfoster
Copy link
Contributor Author

zfoster commented Jan 12, 2021

@annatisch I think this is all set and good for another review

collection_id, document, path = self._GetContainerIdWithPathForItem(
database_or_container_link, document, options
)

if base.IsItemContainerLink(database_or_container_link):
print('checking link', document)
Copy link
Member

Choose a reason for hiding this comment

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

A print statement ;)
Additionally - why was this block moved down? Does the ordering change here affect the options?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oops, I thought that print statement was already there when I reordered them. Let me remove! The order changed because the ID is generated and added to the body in the statement above this one. This statement then adds the partitionKey to the options. In the case that partitionKey is /id, we won't have the partitionKey in the options unless the statements are in this order

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

@@ -508,8 +509,10 @@ def create_item(
if indexing_directive is not None:
request_options["indexingDirective"] = indexing_directive

passthrough_args = {k: v for k, v in kwargs.items() if k != "enable_automatic_id_generation"}
Copy link
Member

@annatisch annatisch Jan 12, 2021

Choose a reason for hiding this comment

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

This line shouldn't be necessary as enable_automatic_id_generation has already been popped from the kwargs above.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ah I think I saw this error before I understood what the difference between the whl and sdist tests were. I never rebuilt so I kept getting the error :) thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

@rakshith91 rakshith91 removed their request for review January 15, 2021 03:56
@zfoster zfoster enabled auto-merge (squash) January 15, 2021 17:17
@annatisch
Copy link
Member

/azp run python - cosmos - tests

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@zfoster zfoster merged commit 72afdd0 into Azure:master Jan 15, 2021
rakshith91 pushed a commit to rakshith91/azure-sdk-for-python that referenced this pull request Jan 15, 2021
…zure#15715)

* Add option for disabling auto id generation in create_item

* Adds ID generation to kwargs, removes bad test

* Default disableAutoID gen to true, add back test

* use correct kwarg

* Fix test typos

* Change flag to enable instead of disable

* Adds test for item create with no ID

* disable header flag

* Fixes auto ID generation in create_item

* Remove print and unnecessary passthrough_args
openapi-sdkautomation bot pushed a commit to AzureSDKAutomation/azure-sdk-for-python that referenced this pull request Aug 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants