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
2 changes: 1 addition & 1 deletion sdk/cosmos/azure-cosmos/azure/cosmos/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -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?

:keyword str session_token: Token for use with Session consistency.
:keyword dict[str,str] initial_headers: Initial headers to be sent as part of the request.
:keyword str etag: An ETag value, or the wildcard character (*). Used to check if the resource
Expand All @@ -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)

if populate_query_metrics:
request_options["populateQueryMetrics"] = populate_query_metrics
if pre_trigger_include is not None:
Expand Down
11 changes: 0 additions & 11 deletions sdk/cosmos/azure-cosmos/test/test_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -699,17 +699,6 @@ def test_partitioned_collection_partition_key_value_types(self):
# create document with float partitionKey
created_collection.create_item(body=document_definition)

document_definition = {'name': 'sample document',
'spam': 'eggs',
'pk': 'value'}

# Should throw an error because automatic id generation is disabled always.
self.__AssertHTTPFailureWithStatus(
StatusCodes.BAD_REQUEST,
created_collection.create_item,
document_definition
)

created_db.delete_container(created_collection)

def test_partitioned_collection_conflict_crud_and_query(self):
Expand Down