Skip to content

Conversation

@yelizhenden-mdb
Copy link
Collaborator

@yelizhenden-mdb yelizhenden-mdb commented Mar 7, 2025

Proposed changes

Jira ticket: CLOUDP-304053

Check if the request body content of the Create method and the response content of the Get method refer to the same resource.

Important Note: The request body schemas can be inline or referenced. For both, check the field equivalence. (Don't check for readOnly/writeOnly properties)

Example: The create and get methods for the /groups/{groupId}/clusters resource follow this rule

Decision 1: If a resource provides a Create method but not a Get method (post /resource and get /resource/{id} should exist at the same time), ignore it.

Decision 2: Check for the same versions of create.requestBody and get.responses. If there is no equivalent version in the get response, ignore it.

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 changed the title CLOUDP-271994: IPA-106: Validate Create methods accepts no query params CLOUDP-304053: IPA-106:Create - The resource must be the request body Mar 10, 2025
@yelizhenden-mdb yelizhenden-mdb marked this pull request as ready for review March 10, 2025 11:27
@yelizhenden-mdb yelizhenden-mdb requested a review from a team as a code owner March 10, 2025 11:27
@wtrocki
Copy link
Member

wtrocki commented Mar 10, 2025

Any way we can putthose somewhere in the repository:
Decision 1: If a resource provides a Create method but not a Get method (post /resource and get /resource/{id} should exist at the same time), ignore it.

Decision 2: Check for the same versions of create.requestBody and get.responses. If there is no equivalent version in the get response, ignore it.

Could be manual notes in readme or part of the ipa docs we generate.

sparse-checkout: |
openapi/
tools/spectral
- name: Setup Node
Copy link
Member

Choose a reason for hiding this comment

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

Driveby?

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 could not find the new libraries I included for the rule, so added these steps

Comment on lines +55 to +58
!isEqual(
omitDeep(postMethodRequestContentPerMediaType.schema, 'readOnly', 'writeOnly'),
omitDeep(getMethodResponseContentPerMediaType.schema, 'readOnly', 'writeOnly')
)
Copy link
Member

Choose a reason for hiding this comment

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

We need to be aware of circular dependencies: lodash/lodash#5764

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This will compare the resolved schemas. So I am expecting it will be all value comparison but not reference comparisons

Copy link
Member

@wtrocki wtrocki Mar 10, 2025

Choose a reason for hiding this comment

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

Yes. That was my point. I think we have >10 circular references in the models and that have been breaking postman for us: https://github.com/mongodb/openapi/blob/main/tools/postman/scripts/transform-for-api.sh#L49

Good to ensure that all current openapi violations in schema are valid. If not those can be extra unit tests.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Okay, let me add extra unit tests for this case. Do you have any example in hand, I can imitate it as unit test?

Copy link
Member

Choose a reason for hiding this comment

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

Looked into something simple but.. our schema is too complex so we can have simpler version for tests.
I think all unit tests should cover oneOf and then we can replace that across

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Included oneOf and circular dependency tests. Could you take another look if they match the cases in your mind?

Copy link
Member

@wtrocki wtrocki left a comment

Choose a reason for hiding this comment

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

LGTM with assumption that it handles complexities of our current schemas.
Could be good to use actual chunks of our openapi schema in tests.

  • oneOf with returned parent objects
  • Circular references

"apache-arrow": "^19.0.1",
"dotenv": "^16.4.7",
"eslint-plugin-jest": "^28.10.0",
"lodash": "^4.17.21",
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

FYI: lodash library is used for deep object comparison

Copy link
Member

Choose a reason for hiding this comment

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

Good idea to use it for now.
In the past experience I have always started with it and then implemented custom versions to cover corner cases :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fingers crossed hope we will not need custom one, but let's see :D

"eslint-plugin-jest": "^28.10.0",
"lodash": "^4.17.21",
"markdown-table": "^3.0.4",
"omit-deep-lodash": "^1.1.7",
Copy link
Collaborator Author

@yelizhenden-mdb yelizhenden-mdb Mar 10, 2025

Choose a reason for hiding this comment

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

FYI: omit-deep-lodash library is used for recursive omitting

@marcosuma
Copy link
Collaborator

I might have missed it somewhere else, but should also the response of the CREATE match the request of the CREATE? I guess that's probably another validation?

@yelizhenden-mdb
Copy link
Collaborator Author

@marcosuma Yes, that's correct! There will be another validation that will validate Create response. This is only for validating the Create request

@yelizhenden-mdb yelizhenden-mdb merged commit c34095e into main Mar 10, 2025
13 checks passed
@yelizhenden-mdb yelizhenden-mdb deleted the CLOUDP-304053 branch March 10, 2025 16:29
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.

4 participants