[ICU-15403] Trim whitespace on storage buckets and host sets json attrs serializers (#2636)

* trim whitespace on json attrs in storage bucket serializer

* add test for storage bucket serializer

* move trimWhitespace check to application level

* add trimWhitespace to preferred_endpoints attr

* add conditional for string arr

* trim whitespace on str arr

* add check for null val

* refactor, move logic

* relocate comment

* add unit test for application serializer
This commit is contained in:
Lauren authored and GitHub committed 2025-02-11 18:31:01 -05:00
1 parent b287683a61
commit e8cc22879f
7 files changed
+46 -5

No files matched your search

@@ -88,6 +88,7 @@ export default class GeneratedHostSetModel extends BaseModel {
name: ['aws', 'azure', 'gcp'],
},
emptyArrayIfMissing: true,
trimWhitespace: true,
})
preferred_endpoints;
@@ -70,6 +70,7 @@ export default class GeneratedStorageBucketModel extends BaseModel {
@attr('string', {
isNestedAttribute: true,
description: 'The AWS region to use.',
trimWhitespace: true,
})
region;
@@ -109,6 +110,7 @@ export default class GeneratedStorageBucketModel extends BaseModel {
@attr('string', {
isNestedAttribute: true,
description: 'The role ARN to use.',
trimWhitespace: true,
})
role_arn;
@@ -60,6 +60,18 @@ export default class ApplicationSerializer extends RESTSerializer {
// Do not serialize `disabled` fields.
// TODO: disabled is temporarily disabled
if (key === 'disabled') delete json[key];
// Before we transform our json
// Let's trim the values in the json obj
// Currently, json values will only ever be a string or an array
if (options.trimWhitespace && json[key]) {
if (typeOf(json[key]) === 'array') {
json[key] = json[key].map((str) => str.trim());
} else if (typeOf(json[key] === 'string')) {
json[key] = json[key].trim();
}
}
// Push nested attributes down into the attributes key
if (options.isNestedAttribute && json[key] !== undefined) {
if (!json.attributes) json.attributes = {};
@@ -72,6 +72,7 @@ export default class StorageBucketSerializer extends ApplicationSerializer {
const value = super.serializeAttribute(...arguments);
const { credentialType } = snapshot.record;
const { options } = attribute;
// This deletes any fields that don't belong to the record's credential type
if (options.isNestedAttribute && json.attributes) {
// The key must be included in the fieldsByType list above
@@ -381,4 +381,29 @@ module('Unit | Serializer | application', function (hooks) {
},
});
});
// JSON values will only be trimmed if the model attr
// includes the trimWhitespace property set to true
// See preferred_endpoints attr on host-set model
test('it trims whitespace on json values', function (assert) {
const store = this.owner.lookup('service:store');
const record = store.createRecord('host-set', {
name: 'Host Set 1',
compositeType: 'azure',
description: 'Description',
host_catalog_id: '123',
version: 1,
preferred_endpoints: [{ value: ' option 1' }, { value: 'option 2 ' }],
sync_interval_seconds: 1,
});
assert.deepEqual(record.serialize(), {
name: 'Host Set 1',
description: 'Description',
host_catalog_id: '123',
version: 1,
preferred_endpoints: ['option 1', 'option 2'],
sync_interval_seconds: 1,
attributes: {},
});
});
});
@@ -54,7 +54,7 @@ module('Unit | Serializer | host set', function (hooks) {
description: 'Description',
host_catalog_id: '123',
version: 1,
preferred_endpoints: [{ value: 'option 1' }, { value: 'option 2' }],
preferred_endpoints: [{ value: ' option 1' }, { value: 'option 2 ' }],
sync_interval_seconds: 1,
filters: [{ value: 'filter 1' }, { value: 'filter 2' }],
});
@@ -26,7 +26,7 @@ module('Unit | Serializer | storage bucket', function (hooks) {
bucket_name: 'bucketname',
bucket_prefix: 'bucketprefix',
worker_filter: 'workerfilter',
region: 'eu-west-1',
region: 'eu-west-1 ',
access_key_id: 'foobars',
secret_access_key: 'testing',
disable_credential_rotation: true,
@@ -117,7 +117,7 @@ module('Unit | Serializer | storage bucket', function (hooks) {
access_key_id: '',
secret_access_key: '',
disable_credential_rotation: true,
role_arn: 'arn',
role_arn: 'arn ',
role_external_id: 'Example987',
role_session_name: 'my-session',
role_tags: [
@@ -160,12 +160,12 @@ module('Unit | Serializer | storage bucket', function (hooks) {
bucket_prefix: 'bucketprefix',
worker_filter: 'workerfilter',
plugin: { name: 'aws' },
region: 'eu-west-1',
region: ' eu-west-1 ',
access_key_id: 'foobars',
secret_access_key: 'testing',
disable_credential_rotation: true,
version: 1,
role_arn: 'role_arn_test',
role_arn: ' role_arn_test ',
role_external_id: 'role_external_id_test',
role_session_name: 'role_session_test',
role_tags: [