Skip to content

Conversation

yelizhenden-mdb
Copy link
Collaborator

@yelizhenden-mdb yelizhenden-mdb commented Dec 17, 2024

Proposed changes

Jira ticket: CLOUDP-287245

Implements a rule that the HTTP method for custom methods must be either GET or POST (https://docs.devprod.prod.corp.mongodb.com/ipa/109).

Checklist

  • I have signed the MongoDB CLA
  • I have added tests that prove my fix is effective or that my feature works

Changes to Spectral

  • I have read the README file for Spectral Updates

Further comments

@yelizhenden-mdb yelizhenden-mdb marked this pull request as ready for review December 17, 2024 17:24
@yelizhenden-mdb yelizhenden-mdb requested a review from a team as a code owner December 17, 2024 17:24
const errors = [];

// Iterate through each path key and its corresponding path item
for (const [pathKey, pathItem] of Object.entries(paths)) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Ah didn't realize that paths is not an array, I thought that the change would only make the input contain a single path and it's pathitems, not the whole paths object. Sorry about that! This makes the function a bit harder to read IMO, feel free to revert back to the previous approach if you want. WDYT?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Or perhaps try $.paths[*]

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It worked with $.paths[*] and using path from the context. Could you take another look and let me know how it looks?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

$.paths[*] gives you the object for each path, but it doesn't include the path keys themselves, so it's not quite enough on its own

Copy link
Collaborator

Choose a reason for hiding this comment

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

Thanks! Looks good! Just one question on the logic

Comment on lines +16 to +18
if (httpMethods.some((method) => !VALID_METHODS.includes(method))) {
return ERROR_RESULT;
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Will this return error if the path has an exception extension?

Suggestion: use $.path[get,put,post,delete,options,head,patch,trace] to ignore any other objects than the methods, kinda like we do here:

- "#PathItem[get,put,post,delete,options,head,patch,trace]"

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, it would return error with an exception extension. Good catch!

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

"#PathItem[get,put,post,delete,options,head,patch,trace]" gives the object inside HTTP method. I will exclude x-xgen-IPA-exception


//Exclude exception extension key
let keys = Object.keys(input);
const httpMethods = keys.filter((key) => key !== EXCEPTION_EXTENSION_KEY);
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@lovisaberggren I excluded the exception extension key here, but in the opposite I can only check the list of predefined HTTP methods as well

Copy link
Collaborator

Choose a reason for hiding this comment

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

Thanks! Yeah perhaps the opposite is more stable, as there may be other extensions for example added to paths, and HTTP methods probably won't change a lot 😄

Copy link
Collaborator

@lovisaberggren lovisaberggren left a comment

Choose a reason for hiding this comment

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

Thanks! 🚀

const ERROR_MESSAGE = 'The HTTP method for custom methods must be GET or POST.';
const ERROR_RESULT = [{ message: ERROR_MESSAGE }];
const VALID_METHODS = ['get', 'post'];
const HTTP_METHODS = ['get', 'put', 'post', 'delete', 'options', 'head', 'patch', 'trace'];
Copy link
Collaborator

Choose a reason for hiding this comment

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

[Just a note for future rules]: I expect we will have other similar rules coming up, at that point we can consider refactoring out a common method to check for valid/invalid methods to the utils package

@yelizhenden-mdb yelizhenden-mdb merged commit 2fd8098 into main Dec 18, 2024
13 checks passed
@yelizhenden-mdb yelizhenden-mdb deleted the CLOUDP-287245 branch December 18, 2024 17:15
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.

2 participants