Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/cute-words-melt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"strapi-plugin-webtools": patch
---

Bad URL when pattern refers to empty singular relationship
1 change: 0 additions & 1 deletion packages/core/server/controllers/url-pattern.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@


import get from 'lodash/get';
import { Context } from 'koa';
import { factories, Schema, UID } from '@strapi/strapi';
import { KoaContext } from '../types/koa';
Expand Down
16 changes: 16 additions & 0 deletions packages/core/server/services/__tests__/url-pattern.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,21 @@ describe('URL Pattern Service', () => {
// Current implementation replaces with empty string if missing.
expect(resolved).toBe('/articles/my-article');
});

it('test', () => {
const uid = 'api::category.category';
const entity = {
title: 'My Category',
test: undefined,
};
const pattern = '/category/[test.title]/[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('/category/my-category');
});
});

describe('validatePattern', () => {
Expand Down Expand Up @@ -182,6 +197,7 @@ describe('URL Pattern Service', () => {

expect(result.valid).toBe(true);
});

it('should invalidate pattern with forbidden fields', () => {
const pattern = '/articles/[forbidden]/[title]';
const allowedFields = ['title'];
Expand Down
31 changes: 15 additions & 16 deletions packages/core/server/services/url-pattern.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,27 +194,26 @@ const customServices = () => ({

const relationEntity = entity[relationName];

if (Array.isArray(relationEntity) && relationIndex !== null) {
const subEntity = relationEntity[relationIndex] as
| Record<string, unknown>
| undefined;
const value = subEntity?.[relationalField[1]];
resolvedPattern = resolvedPattern.replace(
`[${field}]`,
value ? slugify(String(value)) : '',
);
} else if (
typeof relationEntity === 'object'
&& relationEntity !== null
&& !Array.isArray(relationEntity)
) {
if (Array.isArray(relationEntity)) {
if (relationIndex === null) {
strapi.log.error(`Pattern ${pattern} refers to plural relationship ${relationName}, but does not contain an index.`);
resolvedPattern = resolvedPattern.replace(`[${field}]`, '');
} else {
const subEntity = relationEntity[relationIndex] as
| Record<string, unknown>
| undefined;
const value = subEntity?.[relationalField[1]];
resolvedPattern = resolvedPattern.replace(
`[${field}]`,
value ? slugify(String(value)) : '',
);
}
} else {
const value = (relationEntity as Record<string, unknown>)?.[relationalField[1]];
resolvedPattern = resolvedPattern.replace(
`[${field}]`,
value ? slugify(String(value)) : '',
);
} else {
strapi.log.error('Something went wrong whilst resolving the pattern.');
}
}
});
Expand Down
Loading