Repository navigation
fix(concerto-linter): lint reserved system concept declarations #1237
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
Closed
Rishabh060105
wants to merge
6
commits into
accordproject:main
from
Rishabh060105:Rishabh060105/issue-1216-reserved-system-concept-lint
Closed
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
dcdb9d1
feat(concerto-linter): lint reserved system concept declarations
6d68dfc
fix(concerto-linter): address copilot review feedback
daf3d99
fix(concerto-linter): address reserved system review feedback
ed13497
Merge upstream/main into issue-1216 linter follow-up
7d144a8
test(concerto-core): avoid brittle external status assertion
a2bf765
chore: merge upstream main
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
...oncerto-linter/default-ruleset/src/functions/find-reserved-system-concept-declarations.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| /* | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| import type { IFunction, IFunctionResult } from '@stoplight/spectral-core'; | ||
| import semver from 'semver'; | ||
|
|
||
| const RESERVED_SYSTEM_CONCEPT_NAMES = new Set([ | ||
| 'Concept', | ||
| 'Asset', | ||
| 'Transaction', | ||
| 'Participant', | ||
| 'Event', | ||
| ]); | ||
|
|
||
| interface Declaration { | ||
| name?: string; | ||
| } | ||
|
|
||
| interface Model { | ||
| namespace?: string; | ||
| declarations?: Declaration[]; | ||
| concertoVersion?: string; | ||
| } | ||
|
|
||
| interface ReservedSystemConceptOptions { | ||
| dangerouslyAllowReservedSystemTypeNamesInUserModels?: boolean; | ||
| } | ||
|
|
||
| function isLegacyOrV3Model(model: Model): boolean { | ||
|
Rishabh060105 marked this conversation as resolved.
Resolved
|
||
| if (typeof model.concertoVersion === 'string') { | ||
| const minimumVersion = semver.minVersion(model.concertoVersion); | ||
| if (minimumVersion) { | ||
| return minimumVersion.major === 3; | ||
| } | ||
| } | ||
|
|
||
| return typeof model.namespace === 'string' && !model.namespace.includes('@'); | ||
| } | ||
|
|
||
| /** | ||
| * Finds declaration names that collide with reserved Concerto system concepts | ||
| * in risky compatibility contexts. | ||
| * | ||
| * @param {unknown} targetVal The AST node to check, expected to be a Concerto model. | ||
| * @param {ReservedSystemConceptOptions} options Rule options controlling v4 dangerous mode behavior. | ||
| * @returns {IFunctionResult[]} A result for each matching declaration. | ||
| */ | ||
| export const findReservedSystemConceptDeclarations: IFunction = ( | ||
| targetVal, | ||
| options?: unknown, | ||
| ): IFunctionResult[] => { | ||
| if (typeof targetVal !== 'object' || targetVal === null) { | ||
| throw new Error('Value must be a valid AST object for a Concerto model.'); | ||
| } | ||
|
|
||
| const model = targetVal as Model; | ||
| if (!Array.isArray(model.declarations)) { | ||
| return []; | ||
| } | ||
|
|
||
| const functionOptions = options as ReservedSystemConceptOptions | undefined; | ||
| const dangerousModeEnabled = Boolean(functionOptions?.dangerouslyAllowReservedSystemTypeNamesInUserModels); | ||
| const shouldReport = isLegacyOrV3Model(model) || dangerousModeEnabled; | ||
|
|
||
| if (!shouldReport) { | ||
| return []; | ||
| } | ||
|
|
||
| return model.declarations.reduce<IFunctionResult[]>((results, declaration) => { | ||
| if (!declaration?.name || !RESERVED_SYSTEM_CONCEPT_NAMES.has(declaration.name)) { | ||
| return results; | ||
| } | ||
|
|
||
| results.push({ | ||
| message: `Declaration '${declaration.name}' collides with a reserved Concerto system concept. Rename the declaration, disable dangerouslyAllowReservedSystemTypeNamesInUserModels, or migrate safely before relying on this model.`, | ||
| }); | ||
|
Rishabh060105 marked this conversation as resolved.
Resolved
|
||
|
|
||
| return results; | ||
| }, []); | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
packages/concerto-linter/default-ruleset/src/reserved-system-concept-declarations.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| /* | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| import { findReservedSystemConceptDeclarations } from './functions/find-reserved-system-concept-declarations'; | ||
|
|
||
| /** | ||
| * Rule: Reserved System Concept Declarations | ||
| * ------------------------------------------ | ||
| * Flags declaration names that collide with reserved Concerto system concepts | ||
| * in legacy/v3 models, or when dangerous reserved system type names are | ||
| * explicitly allowed in v4 compatibility mode. | ||
| */ | ||
| export default { | ||
| given: '$.models[*]', | ||
| severity: 0, // 0 = error, 1 = warning, 2 = info, 3 = hint | ||
| then: { | ||
| function: findReservedSystemConceptDeclarations, | ||
| }, | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
...certo-linter/default-ruleset/test/fixtures/no-reserved-keywords-system-concepts-valid.cto
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| namespace org.example@1.0.0 | ||
|
|
||
| asset Asset identified by assetId { | ||
| o String assetId | ||
| } | ||
|
|
||
| transaction Transaction { | ||
| o String transactionId optional | ||
| } |
5 changes: 5 additions & 0 deletions
5
...ter/default-ruleset/test/fixtures/reserved-system-concept-declarations-legacy-invalid.cto
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| namespace org.example | ||
|
|
||
| participant Participant identified by participantId { | ||
| o String participantId | ||
| } |
11 changes: 11 additions & 0 deletions
11
...-linter/default-ruleset/test/fixtures/reserved-system-concept-declarations-v3-invalid.cto
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| concerto version "^3.0.0" | ||
|
|
||
| namespace org.example@1.0.0 | ||
|
|
||
| asset Asset identified by assetId { | ||
| o String assetId | ||
| } | ||
|
|
||
| concept Concept { | ||
| o String name | ||
| } |
9 changes: 9 additions & 0 deletions
9
...-linter/default-ruleset/test/fixtures/reserved-system-concept-declarations-v4-invalid.cto
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| namespace org.example@1.0.0 | ||
|
|
||
| asset Asset identified by assetId { | ||
| o String assetId | ||
| } | ||
|
|
||
| event Event { | ||
| o String eventId optional | ||
| } |
9 changes: 9 additions & 0 deletions
9
...to-linter/default-ruleset/test/fixtures/reserved-system-concept-declarations-v4-valid.cto
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| namespace org.example@1.0.0 | ||
|
|
||
| asset Vehicle identified by vehicleId { | ||
| o String vehicleId | ||
| } | ||
|
|
||
| concept Shipment { | ||
| o String shipmentId | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
...s/concerto-linter/default-ruleset/test/rules/reserved-system-concept-declarations.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| import reservedSystemConceptDeclarations from '../../src/reserved-system-concept-declarations'; | ||
| import { testRules } from '../test-rule'; | ||
|
|
||
| function createRule(dangerouslyAllowReservedSystemTypeNamesInUserModels = false) { | ||
| const ruleFunction = reservedSystemConceptDeclarations.then.function; | ||
| return { | ||
| rules: { | ||
| 'reserved-system-concept-declarations': { | ||
| ...reservedSystemConceptDeclarations, | ||
| then: { | ||
| function: (targetVal: unknown, _options: unknown, context: unknown) => ruleFunction( | ||
| targetVal, | ||
| { dangerouslyAllowReservedSystemTypeNamesInUserModels }, | ||
| context as never | ||
| ), | ||
| }, | ||
| }, | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| describe('Reserved System Concept Declarations Rule', () => { | ||
| test('should report violations for v3 models with reserved system concept declarations', async () => { | ||
| const results = await testRules( | ||
| createRule(), | ||
| 'reserved-system-concept-declarations-v3-invalid.cto' | ||
| ); | ||
|
|
||
| expect(results).toHaveLength(2); | ||
| results.forEach(result => { | ||
| expect(result.code).toBe('reserved-system-concept-declarations'); | ||
| expect(result.message).toContain('collides with a reserved Concerto system concept'); | ||
| }); | ||
| }); | ||
|
|
||
| test('should report violations for legacy models when concertoVersion metadata is absent', async () => { | ||
| const results = await testRules( | ||
| createRule(), | ||
| 'reserved-system-concept-declarations-legacy-invalid.cto' | ||
| ); | ||
|
|
||
| expect(results).toHaveLength(1); | ||
| expect(results[0].code).toBe('reserved-system-concept-declarations'); | ||
| }); | ||
|
|
||
| test('should stay silent for normal v4 models when dangerous mode is disabled', async () => { | ||
| const results = await testRules( | ||
| createRule(), | ||
| 'reserved-system-concept-declarations-v4-invalid.cto' | ||
| ); | ||
|
|
||
| expect(results).toHaveLength(0); | ||
| }); | ||
|
|
||
| test('should report one violation per reserved declaration when dangerous mode is enabled in v4', async () => { | ||
| const results = await testRules( | ||
| createRule(true), | ||
| 'reserved-system-concept-declarations-v4-invalid.cto' | ||
| ); | ||
|
|
||
| expect(results).toHaveLength(2); | ||
| results.forEach(result => { | ||
| expect(result.code).toBe('reserved-system-concept-declarations'); | ||
| }); | ||
| }); | ||
|
|
||
| test('should not report non-reserved declarations', async () => { | ||
| const results = await testRules( | ||
| createRule(true), | ||
| 'reserved-system-concept-declarations-v4-valid.cto' | ||
| ); | ||
|
|
||
| expect(results).toHaveLength(0); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.