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

Modified logic for request condition validation to throw after determining all invalid conditions #22532

Merged
merged 4 commits into from
Jun 25, 2021

Conversation

gapra-msft
Copy link
Member

No description provided.

Comment on lines 512 to 535
if (invalidConditions.contains(BlobRequestConditionProperty.LEASE_ID)
&& requestConditions.getLeaseId() != null) {
throw LOGGER.logExceptionAsError(new IllegalArgumentException("'leaseId' is not applicable to this API."));
invalidConditionsFound.add(BlobRequestConditionProperty.LEASE_ID.toString());
}
if (invalidConditions.contains(BlobRequestConditionProperty.TAGS_CONDITIONS)
&& requestConditions.getTagsConditions() != null) {
throw LOGGER.logExceptionAsError(new IllegalArgumentException("'tagsConditions' is not applicable to "
+ "this API."));
invalidConditionsFound.add(BlobRequestConditionProperty.TAGS_CONDITIONS.toString());
}
if (invalidConditions.contains(BlobRequestConditionProperty.IF_MODIFIED_SINCE)
&& requestConditions.getIfModifiedSince() != null) {
throw LOGGER.logExceptionAsError(new IllegalArgumentException("'ifModifiedSince' is not applicable to "
+ "this API."));
invalidConditionsFound.add(BlobRequestConditionProperty.IF_MODIFIED_SINCE.toString());
}
if (invalidConditions.contains(BlobRequestConditionProperty.IF_UNMODIFIED_SINCE)
&& requestConditions.getIfUnmodifiedSince() != null) {
throw LOGGER.logExceptionAsError(new IllegalArgumentException("'ifUnmodifiedSince' is not applicable to "
+ "this API."));
invalidConditionsFound.add(BlobRequestConditionProperty.IF_UNMODIFIED_SINCE.toString());
}
if (invalidConditions.contains(BlobRequestConditionProperty.IF_MATCH)
&& requestConditions.getIfMatch() != null) {
throw LOGGER.logExceptionAsError(new IllegalArgumentException("'ifMatch' is not applicable to this API."));
invalidConditionsFound.add(BlobRequestConditionProperty.IF_MATCH.toString());
}
if (invalidConditions.contains(BlobRequestConditionProperty.IF_NONE_MATCH)
&& requestConditions.getIfNoneMatch() != null) {
throw LOGGER.logExceptionAsError(new IllegalArgumentException("'ifNoneMatch' is not applicable to this "
+ "API."));
invalidConditionsFound.add(BlobRequestConditionProperty.IF_NONE_MATCH.toString());
}
Copy link
Member

@jaschrep-msft jaschrep-msft Jun 24, 2021

Choose a reason for hiding this comment

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

How hot of a code path is this? Do we expect the general case for invalidConditions to be empty or very full? We may be able to make this a little more readable and get some perf depending on the answer to those questions. Avoiding calling contains() so often for a likely empty list is probably useful. IDK if this is more readable to you but it is to me. also we can flag missing enum cases as warnings if this expands later. (gihhub won't let me give a suggested edit here)

for (BlobRequestConditionProperty condition : invalidConditions) {
    switch (condition) {
        case LEASE_ID:
            if (requestConditions.getLeaseId() != null) {
                invalidConditionsFound.add(BlobRequestConditionProperty.LEASE_ID.toString());
            }
            break;
        // other cases
    }
}

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah that does look a lot better.

Copy link
Member Author

Choose a reason for hiding this comment

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

Changed

Copy link
Contributor

Choose a reason for hiding this comment

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

If we care about perf then bitflags could be an option.

if (invalidConditions.contains(BlobRequestConditionProperty.IF_MATCH)
&& requestConditions.getIfMatch() != null) {
throw LOGGER.logExceptionAsError(new IllegalArgumentException("'ifMatch' is not applicable to this API."));
List<String> invalidConditionsFound = new ArrayList<>();
Copy link
Contributor

Choose a reason for hiding this comment

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

most people will get it right, i.e. there won't be invalid conditions in API call. eventually they have to - otherwise API won't work.
Might be good to initialize this list in if statements like we did in .net to avoid extra allocations.

@gapra-msft gapra-msft enabled auto-merge (squash) June 25, 2021 15:45
@gapra-msft gapra-msft merged commit 0644e56 into Azure:main Jun 25, 2021
@gapra-msft gapra-msft deleted the storage/requestConditionValidation branch June 25, 2021 18:44
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.

5 participants