Skip to content
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

fix(database): add prefix to avoid join column name conflicts #20027

Merged
merged 17 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 7 additions & 2 deletions packages/core/database/src/query/helpers/populate/apply.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ const XtoOne = async (

const alias = qb.getAlias();
const joinColAlias = `${alias}.${joinColumnName}`;
// We must select the join column id, however whatever it is named will overwrite an attribute of the same name
// Therefore, we will use something unlikely to conflict (strapi_{content type name}_id)
// TODO: ...and completely restrict the strapi_ prefix for an attribute name in the future
const joinColRenameAs = `strapi_${joinColumnName}`;
innerdvations marked this conversation as resolved.
Show resolved Hide resolved
const joinColSelect = `${joinColAlias} as ${joinColRenameAs}`;

const referencedValues = _.uniq(
results.map((r) => r[referencedColumnName]).filter((value) => !_.isNil(value))
Expand Down Expand Up @@ -146,11 +151,11 @@ const XtoOne = async (
on: joinTable.on,
orderBy: joinTable.orderBy,
})
.addSelect(joinColAlias)
.addSelect(joinColSelect)
.where({ [joinColAlias]: referencedValues })
.execute<Row[]>({ mapResults: false });

const map = _.groupBy<Row>(joinColumnName)(rows);
const map = _.groupBy<Row>(joinColRenameAs)(rows);

results.forEach((result) => {
result[attributeName] = fromTargetRow(_.first(map[result[referencedColumnName] as string]));
Expand Down
108 changes: 108 additions & 0 deletions tests/api/core/strapi/api/populate/populate.test.api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
'use strict';

const { propEq, omit } = require('lodash/fp');

const { createTestBuilder } = require('api-tests/builder');
const { createStrapiInstance } = require('api-tests/strapi');
const { createContentAPIRequest } = require('api-tests/request');

const builder = createTestBuilder();

let strapi;
let data;
let rq;

const schemas = {
contentTypes: {
shirt: {
attributes: {
name: {
type: 'string',
},
shirtId: {
type: 'string',
},
variants: {
type: 'relation',
relation: 'oneToMany',
target: 'api::shirt.shirt',
mappedBy: 'variantOf',
},
variantOf: {
type: 'relation',
relation: 'manyToOne',
target: 'api::shirt.shirt',
inversedBy: 'variants',
},
},
displayName: 'Shirt',
singularName: 'shirt',
pluralName: 'shirts',
description: '',
collectionName: '',
},
},
};

const fixtures = {
shirtA: [
{
name: 'Shirt A',
shirtId: 'A',
},
],
shirtRelations: (fixtures) => {
return [
{ name: 'Shirt B', shirtId: 'B', variantOf: fixtures.shirt[0].id },
{ name: 'Shirt C', shirtId: 'C', variantOf: fixtures.shirt[0].id },
];
},
};

describe('Populate', () => {
beforeAll(async () => {
await builder
.addContentTypes(Object.values(schemas.contentTypes))
.addFixtures(schemas.contentTypes.shirt.singularName, fixtures.shirtA)
.addFixtures(schemas.contentTypes.shirt.singularName, fixtures.shirtRelations)
.build();

strapi = await createStrapiInstance();
rq = createContentAPIRequest({ strapi });
data = await builder.sanitizedFixtures(strapi);
});

afterAll(async () => {
await strapi.destroy();
await builder.cleanup();
});

test('Populate with attribute named {content_type}_id in parent', async () => {
const qs = {
filters: {
name: 'Shirt A',
},
populate: {
variants: true,
variantOf: true,
},
};
const { status, body } = await rq.get(`/${schemas.contentTypes.shirt.pluralName}`, { qs });

expect(status).toBe(200);
expect(body.data).toHaveLength(1);
const shirtA = body.data[0];

// Check that shirtA contains shirtB and shirtC as variants
expect(shirtA.attributes.variants.data).toMatchObject(
expect.arrayContaining([
expect.objectContaining({
attributes: expect.objectContaining({ shirtId: 'B', name: 'Shirt B' }),
}),
expect.objectContaining({
attributes: expect.objectContaining({ shirtId: 'C', name: 'Shirt C' }),
}),
])
);
});
});