Skip to content

Conversation

@lovisaberggren
Copy link
Collaborator

@lovisaberggren lovisaberggren commented Feb 28, 2025

Proposed changes

Adds rule to check for Response suffix of responses from get methods. Additionally did a little refactor to add a common function as this rule is very similar to xgen-IPA-106-create-method-request-body-is-request-suffixed-object. Also added check so that we validate json content types only.

Testing:

  • Added unit test

Jira ticket: CLOUDP-302982

if (!schema.$ref.endsWith('Request')) {
return collectAndReturnViolation(path, RULE_NAME, ERROR_MESSAGE_SCHEMA_NAME);
}
const schemaRef = getSchemaRef(schema);
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Created a shared function since this logic is pretty much the same for the new rule as well

Copy link
Member

Choose a reason for hiding this comment

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

I was looking for that function as well :)
Nice!

const contentMediaType = path[path.length - 1];

if (isCustomMethodIdentifier(resourcePath)) {
if (isCustomMethodIdentifier(resourcePath) || !contentMediaType.endsWith('json')) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Added check to only apply rule to json content types, since csv and gzip don't really have a schema, it's alright if they don't reference one

Copy link
Member

@wtrocki wtrocki Mar 6, 2025

Choose a reason for hiding this comment

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

Good call as this might cause failures for new content types. Good to have short spec for it?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Sure! I'll add to the spec tab

@lovisaberggren lovisaberggren marked this pull request as ready for review March 6, 2025 15:39
@lovisaberggren lovisaberggren requested a review from a team as a code owner March 6, 2025 15:39
* @param {object} schema the schema object
* @returns {string} the schema ref
*/
export function getSchemaRef(schema) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

❤️

},
'application/vnd.atlas.2024-01-01+json': {
schema: {
$ref: '#/components/schemas/SchemaResponse',
Copy link
Member

Choose a reason for hiding this comment

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

What would be our approach for inline schemas (properties defined here).
IMHO we should allow them and skip reporting success

Copy link
Collaborator Author

@lovisaberggren lovisaberggren Mar 6, 2025

Choose a reason for hiding this comment

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

Currently the rule will fail and tell the API producer to implement a schema ref, same approach as we did in #473


const componentSchemas = {
schemas: {
SchemaResponse: {
Copy link
Member

Choose a reason for hiding this comment

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

Technical note to add:

  • We going to cover only top response object.
    • No oneOf support
    • No inline schemas
    • No checks for children
    • No checks for allOf

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yup, I'm good with that, can document when merged

schema: {
type: 'array',
items: {
$ref: '#/components/schemas/Schema',
Copy link
Member

@wtrocki wtrocki Mar 6, 2025

Choose a reason for hiding this comment

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

example to ignore

Suggested change
$ref: '#/components/schemas/Schema',
oneOf: [ {$ref: '#/components/schemas/Schema'}, {$ref: '#/components/schemas/Schema'}}

'/resource/{id}/singleton': {
get: {
responses: {
200: {
Copy link
Member

Choose a reason for hiding this comment

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

Optional: Test for two 2xx response codes.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

That would be an invalid json since we cannot have two keys of the same value

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

@lovisaberggren lovisaberggren merged commit ee01c85 into main Mar 6, 2025
13 checks passed
@lovisaberggren lovisaberggren deleted the CLOUDP-302982 branch March 6, 2025 16:05
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.

3 participants