From 674c44fbf1746141f8c11dcf0867b99c3c810c81 Mon Sep 17 00:00:00 2001 From: Yeliz Henden Date: Wed, 19 Mar 2025 16:27:03 +0000 Subject: [PATCH 1/3] CLOUDP-272001: Disallow usages of project in the api (fix) --- .../IPA112AvoidProjectFieldNames.test.js | 33 ++----------------- tools/spectral/ipa/rulesets/IPA-112.yaml | 2 ++ .../functions/IPA112AvoidProjectFieldNames.js | 9 +++-- .../rulesets/functions/utils/schemaUtils.js | 10 ++++++ 4 files changed, 20 insertions(+), 34 deletions(-) diff --git a/tools/spectral/ipa/__tests__/IPA112AvoidProjectFieldNames.test.js b/tools/spectral/ipa/__tests__/IPA112AvoidProjectFieldNames.test.js index 3e6e8f4741..1bc90d7c09 100644 --- a/tools/spectral/ipa/__tests__/IPA112AvoidProjectFieldNames.test.js +++ b/tools/spectral/ipa/__tests__/IPA112AvoidProjectFieldNames.test.js @@ -11,7 +11,7 @@ testRule('xgen-IPA-112-avoid-project-field-names', [ properties: { group: { type: 'string' }, groupId: { type: 'string' }, - otherField: { type: 'number' }, + projection: { type: 'number' }, }, }, }, @@ -57,7 +57,7 @@ testRule('xgen-IPA-112-avoid-project-field-names', [ errors: [ { code: 'xgen-IPA-112-avoid-project-field-names', - message: 'Field name "projects" should be avoided. Consider using "group" instead.', + message: 'Field name "projects" should be avoided. Consider using "groups" instead.', path: ['components', 'schemas', 'SchemaName', 'properties', 'projects'], severity: DiagnosticSeverity.Warning, }, @@ -85,35 +85,6 @@ testRule('xgen-IPA-112-avoid-project-field-names', [ }, ], }, - { - name: 'invalid schema - with different case variants', - document: { - components: { - schemas: { - SchemaName: { - properties: { - Project: { type: 'string' }, - PROJECTID: { type: 'string' }, - }, - }, - }, - }, - }, - errors: [ - { - code: 'xgen-IPA-112-avoid-project-field-names', - message: 'Field name "Project" should be avoided. Consider using "group" instead.', - path: ['components', 'schemas', 'SchemaName', 'properties', 'Project'], - severity: DiagnosticSeverity.Warning, - }, - { - code: 'xgen-IPA-112-avoid-project-field-names', - message: 'Field name "PROJECTID" should be avoided. Consider using "group" instead.', - path: ['components', 'schemas', 'SchemaName', 'properties', 'PROJECTID'], - severity: DiagnosticSeverity.Warning, - }, - ], - }, { name: 'invalid schema with exception - project field name with exception', document: { diff --git a/tools/spectral/ipa/rulesets/IPA-112.yaml b/tools/spectral/ipa/rulesets/IPA-112.yaml index 6ebe32c58e..45de97d64b 100644 --- a/tools/spectral/ipa/rulesets/IPA-112.yaml +++ b/tools/spectral/ipa/rulesets/IPA-112.yaml @@ -24,3 +24,5 @@ rules: prohibitedFieldNames: - name: 'project' alternative: ['group'] + - name: 'projects' + alternative: ['groups'] diff --git a/tools/spectral/ipa/rulesets/functions/IPA112AvoidProjectFieldNames.js b/tools/spectral/ipa/rulesets/functions/IPA112AvoidProjectFieldNames.js index c68ded5b82..19dc563631 100644 --- a/tools/spectral/ipa/rulesets/functions/IPA112AvoidProjectFieldNames.js +++ b/tools/spectral/ipa/rulesets/functions/IPA112AvoidProjectFieldNames.js @@ -6,6 +6,7 @@ import { handleInternalError, } from './utils/collectionUtils.js'; import { resolveObject } from './utils/componentUtils.js'; +import { splitCamelCase } from './utils/schemaUtils.js'; const RULE_NAME = 'xgen-IPA-112-avoid-project-field-names'; @@ -28,18 +29,20 @@ export default (input, options, { path, documentInventory }) => { function checkViolationsAndReturnErrors(input, options, path) { try { const prohibitedFieldNames = options?.prohibitedFieldNames || []; - const lowerPropertyName = input.toLowerCase(); + + // Split the field name into words assuming camelCase + const words = splitCamelCase(input); // Check if the property name includes any of the prohibited terms for (const prohibitedItem of prohibitedFieldNames) { const prohibitedName = prohibitedItem.name || ''; const alternative = prohibitedItem.alternative || ''; - if (lowerPropertyName.includes(prohibitedName.toLowerCase())) { + if (words.some((word) => word.toLowerCase() === prohibitedName)) { return [ { path, - message: `Field name "${input}" should be avoided. Consider using ${alternative.map((alt) => `"${alt}"`)} instead.`, + message: `Field name "${input}" should be avoided. Consider using "${alternative}" instead.`, }, ]; } diff --git a/tools/spectral/ipa/rulesets/functions/utils/schemaUtils.js b/tools/spectral/ipa/rulesets/functions/utils/schemaUtils.js index d70d323ba0..43fe55f0e3 100644 --- a/tools/spectral/ipa/rulesets/functions/utils/schemaUtils.js +++ b/tools/spectral/ipa/rulesets/functions/utils/schemaUtils.js @@ -30,3 +30,13 @@ export function getSchemaPathFromEnumPath(path) { } return path.slice(0, enumIndex); } + +/** + * Split camelCase string into words + * Example: "myProjectId" becomes ["my", "Project", "Id"] + * @param str {string} camelCase string + * @returns {*} + */ +export function splitCamelCase(str) { + return str.split(/(?=[A-Z])/); +} From 2dd03c27c5183cc397c7e2720a65196008cbd987 Mon Sep 17 00:00:00 2001 From: Yeliz Henden Date: Wed, 19 Mar 2025 16:36:51 +0000 Subject: [PATCH 2/3] unit test fix --- .../ipa/__tests__/utils/schemaUtils.test.js | 30 +++++++++++++++++++ .../functions/IPA112AvoidProjectFieldNames.js | 2 +- .../rulesets/functions/utils/schemaUtils.js | 7 ++++- 3 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 tools/spectral/ipa/__tests__/utils/schemaUtils.test.js diff --git a/tools/spectral/ipa/__tests__/utils/schemaUtils.test.js b/tools/spectral/ipa/__tests__/utils/schemaUtils.test.js new file mode 100644 index 0000000000..8bad523f67 --- /dev/null +++ b/tools/spectral/ipa/__tests__/utils/schemaUtils.test.js @@ -0,0 +1,30 @@ +import { splitCamelCase } from '../../rulesets/functions/utils/schemaUtils.js'; +import { describe, expect, it } from '@jest/globals'; + +describe('splitCamelCase', () => { + it('should split basic camelCase strings', () => { + expect(splitCamelCase('camelCase')).toEqual(['camel', 'case']); + expect(splitCamelCase('thisIsCamelCase')).toEqual(['this', 'is', 'camel', 'case']); + }); + + it('should handle single-word strings', () => { + expect(splitCamelCase('word')).toEqual(['word']); + expect(splitCamelCase('Word')).toEqual(['word']); + }); + + it('should handle strings with numbers', () => { + expect(splitCamelCase('user123Name')).toEqual(['user123', 'name']); + expect(splitCamelCase('user123name')).toEqual(['user123name']); + }); + + it('should handle empty strings', () => { + expect(splitCamelCase('')).toEqual(['']); + }); + + it('should handle edge cases from the project', () => { + expect(splitCamelCase('project')).toEqual(['project']); + expect(splitCamelCase('projectId')).toEqual(['project', 'id']); + expect(splitCamelCase('myProjectDetails')).toEqual(['my', 'project', 'details']); + expect(splitCamelCase('projection')).toEqual(['projection']); + }); +}); diff --git a/tools/spectral/ipa/rulesets/functions/IPA112AvoidProjectFieldNames.js b/tools/spectral/ipa/rulesets/functions/IPA112AvoidProjectFieldNames.js index 19dc563631..c24f531b02 100644 --- a/tools/spectral/ipa/rulesets/functions/IPA112AvoidProjectFieldNames.js +++ b/tools/spectral/ipa/rulesets/functions/IPA112AvoidProjectFieldNames.js @@ -38,7 +38,7 @@ function checkViolationsAndReturnErrors(input, options, path) { const prohibitedName = prohibitedItem.name || ''; const alternative = prohibitedItem.alternative || ''; - if (words.some((word) => word.toLowerCase() === prohibitedName)) { + if (words.some((word) => word === prohibitedName)) { return [ { path, diff --git a/tools/spectral/ipa/rulesets/functions/utils/schemaUtils.js b/tools/spectral/ipa/rulesets/functions/utils/schemaUtils.js index 43fe55f0e3..fb798c1568 100644 --- a/tools/spectral/ipa/rulesets/functions/utils/schemaUtils.js +++ b/tools/spectral/ipa/rulesets/functions/utils/schemaUtils.js @@ -38,5 +38,10 @@ export function getSchemaPathFromEnumPath(path) { * @returns {*} */ export function splitCamelCase(str) { - return str.split(/(?=[A-Z])/); + if (!str) return ['']; + + // Special handling for single words + if (!/[A-Z]/.test(str)) return [str]; + + return str.split(/(?=[A-Z])/).map((word) => word.toLowerCase()); } From 4202d532fbe15c27fe5e26ab05aa13fa8c7b7e38 Mon Sep 17 00:00:00 2001 From: Yeliz Henden Date: Wed, 19 Mar 2025 17:51:03 +0000 Subject: [PATCH 3/3] address comments --- tools/spectral/ipa/rulesets/functions/utils/schemaUtils.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/spectral/ipa/rulesets/functions/utils/schemaUtils.js b/tools/spectral/ipa/rulesets/functions/utils/schemaUtils.js index fb798c1568..2c15b69e28 100644 --- a/tools/spectral/ipa/rulesets/functions/utils/schemaUtils.js +++ b/tools/spectral/ipa/rulesets/functions/utils/schemaUtils.js @@ -35,7 +35,7 @@ export function getSchemaPathFromEnumPath(path) { * Split camelCase string into words * Example: "myProjectId" becomes ["my", "Project", "Id"] * @param str {string} camelCase string - * @returns {*} + * @returns {string[]} */ export function splitCamelCase(str) { if (!str) return [''];