mirror of
https://github.com/strapi/strapi.git
synced 2025-12-25 22:23:10 +00:00
fix(content-types): remove getter for private attributes
+ Fixes a flaky test (transactions.test.api.js)
This commit is contained in:
parent
4f63cb517f
commit
c8995460ac
@ -284,7 +284,7 @@ describe('transactions', () => {
|
||||
.where({ id: 1 })
|
||||
.execute();
|
||||
|
||||
expect(end[0].key).toEqual('strapi_content_types_schema');
|
||||
expect(end[0].key).toEqual(original[0].key);
|
||||
});
|
||||
|
||||
test('rollback successfully', async () => {
|
||||
@ -323,7 +323,7 @@ describe('transactions', () => {
|
||||
.where({ id: 1 })
|
||||
.execute();
|
||||
|
||||
expect(end[0].key).toEqual('strapi_content_types_schema');
|
||||
expect(end[0].key).toEqual(original[0].key);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@ -143,7 +143,6 @@ describe('Permissions Manager', () => {
|
||||
global.strapi = {
|
||||
getModel() {
|
||||
return {
|
||||
privateAttributes: [],
|
||||
attributes: {
|
||||
title: {
|
||||
type: 'text',
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
|
||||
const { cloneDeep } = require('lodash/fp');
|
||||
const _ = require('lodash');
|
||||
const { hasDraftAndPublish, getPrivateAttributes } = require('@strapi/utils').contentTypes;
|
||||
const { hasDraftAndPublish } = require('@strapi/utils').contentTypes;
|
||||
const {
|
||||
CREATED_AT_ATTRIBUTE,
|
||||
UPDATED_AT_ATTRIBUTE,
|
||||
@ -58,12 +58,6 @@ const createContentType = (uid, definition) => {
|
||||
);
|
||||
}
|
||||
|
||||
Object.defineProperty(schema, 'privateAttributes', {
|
||||
get() {
|
||||
return getPrivateAttributes(schema);
|
||||
},
|
||||
});
|
||||
|
||||
// attributes
|
||||
Object.assign(schema.attributes, {
|
||||
[CREATED_AT_ATTRIBUTE]: {
|
||||
|
||||
@ -13,7 +13,6 @@ describe('Entity service triggers webhooks', () => {
|
||||
uid: 'api::test.test',
|
||||
kind: 'singleType',
|
||||
modelName: 'test-model',
|
||||
privateAttributes: [],
|
||||
attributes: {
|
||||
attr: { type: 'string' },
|
||||
},
|
||||
|
||||
@ -62,7 +62,7 @@ describe('Entity service', () => {
|
||||
|
||||
const fakeStrapi = {
|
||||
getModel: jest.fn(() => {
|
||||
return { kind: 'singleType', privateAttributes: [] };
|
||||
return { kind: 'singleType' };
|
||||
}),
|
||||
};
|
||||
|
||||
@ -133,7 +133,6 @@ describe('Entity service', () => {
|
||||
uid: entityUID,
|
||||
kind: 'contentType',
|
||||
modelName: 'test-model',
|
||||
privateAttributes: [],
|
||||
options: {},
|
||||
attributes: {
|
||||
attrStringDefaultRequired: {
|
||||
@ -467,7 +466,6 @@ describe('Entity service', () => {
|
||||
modelName: 'entity',
|
||||
collectionName: 'entity',
|
||||
uid: entityUID,
|
||||
privateAttributes: [],
|
||||
options: {},
|
||||
info: {
|
||||
singularName: 'entity',
|
||||
|
||||
@ -27,7 +27,6 @@ describe('BigInteger validator', () => {
|
||||
kind: 'contentType',
|
||||
modelName: 'test-model',
|
||||
uid: 'test-uid',
|
||||
privateAttributes: [],
|
||||
options: {},
|
||||
attributes: {
|
||||
attrBigIntegerUnique: { type: 'biginteger', unique: true },
|
||||
|
||||
@ -27,7 +27,6 @@ describe('Date validator', () => {
|
||||
kind: 'contentType',
|
||||
modelName: 'test-model',
|
||||
uid: 'test-uid',
|
||||
privateAttributes: [],
|
||||
options: {},
|
||||
attributes: {
|
||||
attrDateUnique: { type: 'date', unique: true },
|
||||
|
||||
@ -27,7 +27,6 @@ describe('Datetime validator', () => {
|
||||
kind: 'contentType',
|
||||
modelName: 'test-model',
|
||||
uid: 'test-uid',
|
||||
privateAttributes: [],
|
||||
options: {},
|
||||
attributes: {
|
||||
attrDateTimeUnique: { type: 'datetime', unique: true },
|
||||
|
||||
@ -27,7 +27,6 @@ describe('Float validator', () => {
|
||||
kind: 'contentType',
|
||||
uid: 'test-uid',
|
||||
modelName: 'test-model',
|
||||
privateAttributes: [],
|
||||
options: {},
|
||||
attributes: {
|
||||
attrFloatUnique: { type: 'float', unique: true },
|
||||
|
||||
@ -27,7 +27,6 @@ describe('Integer validator', () => {
|
||||
kind: 'contentType',
|
||||
uid: 'test-uid',
|
||||
modelName: 'test-model',
|
||||
privateAttributes: [],
|
||||
options: {},
|
||||
attributes: {
|
||||
attrIntegerUnique: { type: 'integer', unique: true },
|
||||
|
||||
@ -27,7 +27,6 @@ describe('String validator', () => {
|
||||
kind: 'contentType',
|
||||
modelName: 'test-model',
|
||||
uid: 'test-uid',
|
||||
privateAttributes: [],
|
||||
options: {},
|
||||
attributes: {
|
||||
attrStringUnique: { type: 'string', unique: true },
|
||||
|
||||
@ -27,7 +27,6 @@ describe('Time validator', () => {
|
||||
kind: 'contentType',
|
||||
modelName: 'test-model',
|
||||
uid: 'test-uid',
|
||||
privateAttributes: [],
|
||||
options: {},
|
||||
attributes: {
|
||||
attrTimeUnique: { type: 'time', unique: true },
|
||||
|
||||
@ -27,7 +27,6 @@ describe('Time validator', () => {
|
||||
kind: 'contentType',
|
||||
modelName: 'test-model',
|
||||
uid: 'test-uid',
|
||||
privateAttributes: [],
|
||||
options: {},
|
||||
attributes: {
|
||||
attrTimestampUnique: { type: 'timestamp', unique: true },
|
||||
|
||||
@ -26,7 +26,6 @@ describe('UID validator', () => {
|
||||
kind: 'contentType',
|
||||
modelName: 'test-model',
|
||||
uid: 'test-uid',
|
||||
privateAttributes: [],
|
||||
options: {},
|
||||
attributes: {
|
||||
attrUidUnique: { type: 'uid' },
|
||||
|
||||
@ -153,7 +153,6 @@ describe('Content types utils', () => {
|
||||
test('Attribute is private in the model attributes', () => {
|
||||
const model = createModelWithPrivates();
|
||||
global.strapi = { config: createConfig() };
|
||||
Object.assign(model, { privateAttributes: getPrivateAttributes(model) });
|
||||
|
||||
expect(isPrivateAttribute(model, 'foo')).toBeTruthy();
|
||||
expect(isPrivateAttribute(model, 'bar')).toBeFalsy();
|
||||
@ -164,7 +163,6 @@ describe('Content types utils', () => {
|
||||
test('Attribute is set to private in the app config', () => {
|
||||
const model = createModelWithPrivates();
|
||||
global.strapi = { config: createConfig(['bar']) };
|
||||
Object.assign(model, { privateAttributes: getPrivateAttributes(model) });
|
||||
|
||||
expect(isPrivateAttribute(model, 'foo')).toBeTruthy();
|
||||
expect(isPrivateAttribute(model, 'bar')).toBeTruthy();
|
||||
@ -175,7 +173,6 @@ describe('Content types utils', () => {
|
||||
test('Attribute is set to private in the model options', () => {
|
||||
const model = createModelWithPrivates(['foobar']);
|
||||
global.strapi = { config: createConfig() };
|
||||
Object.assign(model, { privateAttributes: getPrivateAttributes(model) });
|
||||
|
||||
expect(isPrivateAttribute(model, 'foo')).toBeTruthy();
|
||||
expect(isPrivateAttribute(model, 'bar')).toBeFalsy();
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
'use strict';
|
||||
|
||||
const _ = require('lodash');
|
||||
const { has } = require('lodash/fp');
|
||||
const { getOr, has, union } = require('lodash/fp');
|
||||
|
||||
const SINGLE_TYPE = 'singleType';
|
||||
const COLLECTION_TYPE = 'collectionType';
|
||||
@ -92,16 +92,23 @@ const isSingleType = ({ kind = COLLECTION_TYPE }) => kind === SINGLE_TYPE;
|
||||
const isCollectionType = ({ kind = COLLECTION_TYPE }) => kind === COLLECTION_TYPE;
|
||||
const isKind = (kind) => (model) => model.kind === kind;
|
||||
|
||||
const getStoredPrivateAttributes = (model) => union(
|
||||
strapi?.config?.get('api.responses.privateAttributes', []) ?? [],
|
||||
getOr([], 'options.privateAttributes', model)
|
||||
);
|
||||
|
||||
const getPrivateAttributes = (model = {}) => {
|
||||
return _.union(
|
||||
strapi.config.get('api.responses.privateAttributes', []),
|
||||
_.get(model, 'options.privateAttributes', []),
|
||||
getStoredPrivateAttributes(model),
|
||||
_.keys(_.pickBy(model.attributes, (attr) => !!attr.private))
|
||||
);
|
||||
};
|
||||
|
||||
const isPrivateAttribute = (model, attributeName) => {
|
||||
return model?.privateAttributes?.includes(attributeName) ?? false;
|
||||
if (model?.attributes?.[attributeName]?.private === true) {
|
||||
return true;
|
||||
}
|
||||
return getStoredPrivateAttributes(model).includes(attributeName);
|
||||
};
|
||||
|
||||
const isScalarAttribute = (attribute) => {
|
||||
|
||||
@ -7,7 +7,7 @@ module.exports = ({ schema, key, attribute }, { remove }) => {
|
||||
return;
|
||||
}
|
||||
|
||||
const isPrivate = isPrivateAttribute(schema, key) || attribute.private === true;
|
||||
const isPrivate = attribute.private === true || isPrivateAttribute(schema, key);
|
||||
|
||||
if (isPrivate) {
|
||||
remove(key);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user