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

Filtering resource models returned only by post #2365

Merged
merged 10 commits into from
Jun 19, 2017
Merged

Conversation

dsgouda
Copy link
Contributor

@dsgouda dsgouda commented Jun 14, 2017

Addressing #2316


var unfilteredResourceCandidates = resourceModels.Union(modelsAllOfOnXmsAzureResources);

// Now filter all the resource models that are returned from a POST operation only
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't see the "only" part reflected in the code but maybe I'm missing it.
Also, the indentation is misleading: the .SelectMany calls are perfectly aligned but are not operating on the same "level". I'd expect the second SelectMany to be on the same height as the inner Where, just break pathObj => pathObj<HERE>.Where and lines won't be that long.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

d'oh (I went everywhere looking for a Homer emoji!)

var postOpResourceModels = serviceDefinition.Paths.Values.SelectMany(pathObj => pathObj.Where(opObj => opObj.Key.EqualsIgnoreCase("post"))
.SelectMany(opObj => opObj.Value.Responses?.Select(resp => resp.Value?.Schema?.Reference?.StripDefinitionPath())))
.Where(model => !string.IsNullOrWhiteSpace(model))
.Except(putOperationsResponseModels)
Copy link
Contributor

Choose a reason for hiding this comment

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

don't implement the "only" by relying on other sets and excepting them.

  1. you already missed some HTTP methods, what about PATCH?
  2. it's not obvious that putOperationsResponseModels contains all models of PUT methods. Next month, someone applies a fix to something by changing that set, not realizing that it breaks this line. Locality 😉

Copy link
Contributor

Choose a reason for hiding this comment

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

how about looking into the models returned by operations and selecting the ones that are only returned by posts? then removing those from the previous set? I would probably not call it "unfiltered" set, since it's already quite filtered, maybe "resourceCandidates".

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I literally called it resourceCandidates before changing my mind 😆

Copy link
Contributor Author

@dsgouda dsgouda Jun 15, 2017

Choose a reason for hiding this comment

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

@veronicagg that is what I am precisely trying to do
@olydis from what @ravbhatnagar indicated on the issue, I should really be looking at anything returned by a PUT/GET operation only.
Also, models being populated in the putOperationsResponseModels are basically resource model candidates and this particular filtering is applicable only to the resource models, would I still miss out on anything here?

Copy link
Contributor

Choose a reason for hiding this comment

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

sounds good, how does the rule perform on the specs repo?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well, this is not a rule per se, more like pre cursor step to running any arm related validation rules.
This is a one off operation and the results are stored in the context object passed to every single rule.
This makes me wonder if we should run this logic even if we do not have any arm specific rules to run, given how expensive these operations are its worth a look. what say?

var postOpResourceModels = serviceDefinition.Paths.Values.SelectMany(pathObj => pathObj.Where(opObj => opObj.Key.EqualsIgnoreCase("post"))
.SelectMany(opObj => opObj.Value.Responses?.Select(resp => resp.Value?.Schema?.Reference?.StripDefinitionPath())))
.Where(model => !string.IsNullOrWhiteSpace(model))
.Except(putOperationsResponseModels)
Copy link
Contributor

Choose a reason for hiding this comment

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

how about looking into the models returned by operations and selecting the ones that are only returned by posts? then removing those from the previous set? I would probably not call it "unfiltered" set, since it's already quite filtered, maybe "resourceCandidates".

@vishrutshah
Copy link
Contributor

I do not have any other comments apart from what's already been commented. Feel free to discuss with them and I'd be fine on the decision. I am not posting "Approve" / "Request Changes" as I'll be OOF tomorrow so if it gets resolved, go ahead :)

@veronicagg
Copy link
Contributor

@dsgouda apart from updating the name to "resourceCandidates" which I'm not seeing, rest looks good to me. I'd be good to run some of the resource related rules on the repo and make sure we're accurate. Since the rule won't get released right away (not as "latest"), if you'd done validation already you could run on the repo afterwards with the nightly build. thanks!

Copy link
Contributor

@veronicagg veronicagg left a comment

Choose a reason for hiding this comment

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

See my prior comment, pending CI results, I'm good with the rest.

@dsgouda
Copy link
Contributor Author

dsgouda commented Jun 16, 2017

Thanks @veronicagg I will run the changes against the rest spec repo

@dsgouda
Copy link
Contributor Author

dsgouda commented Jun 17, 2017

Ran this against a few specs in the rest spec repo, and I still see the resources being picked as expected, we can investigate this further if required.

{
this.Root = root;
this.Value = root;
this.File = file;
PopulateResourceTypes(root);
if (metadata.ServiceDefinitionDocumentType == ServiceDefinitionDocumentType.ARM)
Copy link
Contributor

Choose a reason for hiding this comment

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

this change breaks CI:
https://github.com/Azure/autorest/blob/master/src/modeler/AutoRest.Swagger/Validation/BodyTopLevelProperties.cs#L52 fails because resModels is null. Since that didn't happen before, I bet that it is this guard that causes the context.ResourceModels to not be populated...
Now, I see that both this guard and the rule say that they are ARM, but still, this is kind of the only potential point of failure I could make out.

Copy link
Contributor

Choose a reason for hiding this comment

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

further observation: this == will fail if metadata.ServiceDefinitionDocumentType are multiple types at once!
Instead of duplicating the logic for document type filtering, I'd honestly just make ResourceModels a lazily initialized property! If someone asks for it, populate it, otherwise don't. That way, you automatically inherit the correct behavior from the fact that the rules are classified correctly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

hmm... starting to think if there is any value in adding the check after all.

Copy link
Contributor

Choose a reason for hiding this comment

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

fair point, don't think anyone severely suffers because of this... main customer is still ARM, for data plane I also assume this goes faster since there is actually not much that looks like a resource. And even then, what's the actual overhead of this? Would be surprised if it took half a second on most Swaggers.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agreed, even if it does warrant a performance investigation, I think we can revisit this once we move validator to a different repo,

@dsgouda dsgouda closed this Jun 19, 2017
@dsgouda dsgouda reopened this Jun 19, 2017
@olydis olydis merged commit 061461d into Azure:master Jun 19, 2017
@dsgouda dsgouda deleted the i2316 branch June 29, 2017 22:10
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