-
-
Notifications
You must be signed in to change notification settings - Fork 21
feat: Implement support for ToMany relation array syntax in URL patterns #355
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
Open
candidosales
wants to merge
7
commits into
pluginpal:master
Choose a base branch
from
candidosales:poc-many-to-many
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+331
−63
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
661a844
feat: Implement support for ToMany relation array syntax in URL patte…
candidosales aeabd36
feat: enhance URL pattern parsing to support hyphens and array syntax…
candidosales 10f90f0
feat: improve URL pattern relation extraction by stripping array indices
candidosales 2a6bbf6
feat: enable `ToMany` relations by removing commented restriction
candidosales 888d218
feat: Add validation for array index in ToMany relations within URL p…
candidosales a130e2b
fix: lint
candidosales c31162c
feat: refine type definitions for entity handling in URL pattern reso…
candidosales 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| { | ||
| "recommendations": [ | ||
| "editorconfig.editorconfig" | ||
| ] | ||
| } | ||
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
204 changes: 204 additions & 0 deletions
204
packages/core/server/services/__tests__/url-pattern.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,204 @@ | ||
| import urlPatternService from '../url-pattern'; | ||
|
|
||
| // Mock getPluginService to return the service itself | ||
| jest.mock('../../util/getPluginService', () => ({ | ||
| getPluginService: () => urlPatternService, | ||
| })); | ||
|
|
||
| jest.mock('@strapi/strapi', () => ({ | ||
| factories: { | ||
| createCoreService: (uid, cfg) => { | ||
| if (typeof cfg === 'function') return cfg(); | ||
| return cfg; | ||
| }, | ||
| }, | ||
| })); | ||
|
|
||
| // Mock Strapi global | ||
| global.strapi = { | ||
| config: { | ||
| get: jest.fn((key) => { | ||
| if (key === 'plugin::webtools') return { slugify: (str) => str.toLowerCase().replace(/\s+/g, '-') }; | ||
| if (key === 'plugin::webtools.default_pattern') return '/[id]'; | ||
| return null; | ||
| }), | ||
| }, | ||
| contentTypes: { | ||
| 'api::article.article': { | ||
| attributes: { | ||
| title: { type: 'string' }, | ||
| categories: { | ||
| type: 'relation', | ||
| relation: 'manyToMany', | ||
| target: 'api::category.category', | ||
| }, | ||
| author: { | ||
| type: 'relation', | ||
| relation: 'oneToOne', | ||
| target: 'api::author.author', | ||
| } | ||
| }, | ||
| info: { pluralName: 'articles' }, | ||
| }, | ||
| 'api::category.category': { | ||
| attributes: { | ||
| slug: { type: 'string' }, | ||
| name: { type: 'string' }, | ||
| }, | ||
| }, | ||
| 'api::author.author': { | ||
| attributes: { | ||
| name: { type: 'string' }, | ||
| } | ||
| } | ||
| }, | ||
| log: { | ||
| error: jest.fn(), | ||
| }, | ||
| } as any; | ||
|
|
||
|
|
||
| describe('URL Pattern Service', () => { | ||
| const service = urlPatternService as any; | ||
|
|
||
| describe('getAllowedFields', () => { | ||
| it('should return allowed fields including ToMany relations', () => { | ||
| const contentType = strapi.contentTypes['api::article.article']; | ||
| const allowedFields = ['string', 'uid']; | ||
| const fields = service.getAllowedFields(contentType, allowedFields); | ||
|
|
||
| expect(fields).toContain('title'); | ||
| expect(fields).toContain('author.name'); | ||
| // This is the new feature we want to support | ||
| expect(fields).toContain('categories.slug'); | ||
| }); | ||
|
|
||
| it('should return allowed fields for underscored relation name', () => { | ||
| const contentType = { | ||
| attributes: { | ||
| private_categories: { | ||
| type: 'relation', | ||
| relation: 'manyToMany', | ||
| target: 'api::category.category', | ||
| }, | ||
| }, | ||
| } as any; | ||
|
|
||
| // Mock strapi.contentTypes for the target | ||
| strapi.contentTypes['api::category.category'] = { | ||
| attributes: { | ||
| slug: { type: 'uid' }, | ||
| }, | ||
| } as any; | ||
|
|
||
| const allowedFields = ['uid']; | ||
| const fields = service.getAllowedFields(contentType, allowedFields); | ||
|
|
||
| expect(fields).toContain('private_categories.slug'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('resolvePattern', () => { | ||
| it('should resolve pattern with ToMany relation array syntax', () => { | ||
| const uid = 'api::article.article'; | ||
| const entity = { | ||
| title: 'My Article', | ||
| categories: [ | ||
| { slug: 'tech', name: 'Technology' }, | ||
| { slug: 'news', name: 'News' }, | ||
| ], | ||
| }; | ||
| const pattern = '/articles/[categories[0].slug]/[title]'; | ||
|
|
||
| const resolved = service.resolvePattern(uid, entity, pattern); | ||
|
|
||
| expect(resolved).toBe('/articles/tech/my-article'); | ||
| }); | ||
|
|
||
| it('should resolve pattern with dashed relation name', () => { | ||
| const uid = 'api::article.article'; | ||
| const entity = { | ||
| 'private-categories': [ | ||
| { slug: 'tech' }, | ||
| ], | ||
| }; | ||
| const pattern = '/articles/[private-categories[0].slug]'; | ||
|
|
||
| const resolved = service.resolvePattern(uid, entity, pattern); | ||
|
|
||
| expect(resolved).toBe('/articles/tech'); | ||
| }); | ||
|
|
||
| it('should handle missing array index gracefully', () => { | ||
| const uid = 'api::article.article'; | ||
| const entity = { | ||
| title: 'My Article', | ||
| categories: [], | ||
| }; | ||
| const pattern = '/articles/[categories[0].slug]/[title]'; | ||
|
|
||
| const resolved = service.resolvePattern(uid, entity, pattern); | ||
|
|
||
| // Should probably result in empty string for that part or handle it? | ||
| // Current implementation replaces with empty string if missing. | ||
| expect(resolved).toBe('/articles/my-article'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('validatePattern', () => { | ||
| it('should invalidate pattern with ToMany relation missing array index', () => { | ||
| const pattern = '/test/[private_categories.slug]/1'; | ||
| const allowedFields = ['private_categories.slug']; | ||
| const contentType = { | ||
| attributes: { | ||
| private_categories: { | ||
| type: 'relation', | ||
| relation: 'manyToMany', | ||
| target: 'api::category.category', | ||
| }, | ||
| }, | ||
| } as any; | ||
|
|
||
| const result = service.validatePattern(pattern, allowedFields, contentType); | ||
|
|
||
| expect(result.valid).toBe(false); | ||
| expect(result.message).toContain('must include an array index'); | ||
| }); | ||
|
|
||
| it('should validate pattern with underscored relation name', () => { | ||
| const pattern = '/test/[private_categories[0].slug]/1'; | ||
| const allowedFields = ['private_categories.slug']; | ||
|
|
||
| const result = service.validatePattern(pattern, allowedFields); | ||
|
|
||
| expect(result.valid).toBe(true); | ||
| }); | ||
|
|
||
| it('should validate pattern with dashed relation name', () => { | ||
| const pattern = '/test/[private-categories[0].slug]/1'; | ||
| const allowedFields = ['private-categories.slug']; | ||
|
|
||
| const result = service.validatePattern(pattern, allowedFields); | ||
|
|
||
| expect(result.valid).toBe(true); | ||
| }); | ||
| it('should invalidate pattern with forbidden fields', () => { | ||
| const pattern = '/articles/[forbidden]/[title]'; | ||
| const allowedFields = ['title']; | ||
|
|
||
| const result = service.validatePattern(pattern, allowedFields); | ||
|
|
||
| expect(result.valid).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| describe('getRelationsFromPattern', () => { | ||
| it('should return relation name without array index', () => { | ||
| const pattern = '/articles/[categories[0].slug]/[title]'; | ||
| const relations = service.getRelationsFromPattern(pattern); | ||
|
|
||
| expect(relations).toContain('categories'); | ||
| expect(relations).not.toContain('categories[0]'); | ||
| }); | ||
| }); | ||
| }); |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added this recommendation file to remind the developer to install EditorConfig and set up tab-size indentation.