Added support for short format when attaching authors/tags in Admin API v2 (#10505)

closes #10504

- both formats are supported
This commit is contained in:
Katharina Irrgang 2019-02-22 13:01:04 +01:00 committed by GitHub
parent 55289d04c8
commit a361a8d7c0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 344 additions and 109 deletions

View File

@ -84,7 +84,7 @@ module.exports = {
debug(frame.options);
},
add(apiConfig, frame) {
add(apiConfig, frame, options = {add: true}) {
debug('add');
if (_.get(frame,'options.source')) {
@ -98,11 +98,34 @@ module.exports = {
frame.data.pages[0] = url.forPost(Object.assign({}, frame.data.pages[0]), frame.options);
// @NOTE: force storing page
frame.data.pages[0].page = true;
if (options.add) {
frame.data.pages[0].page = true;
}
// CASE: Transform short to long format
if (frame.data.pages[0].authors) {
frame.data.pages[0].authors.forEach((author, index) => {
if (_.isString(author)) {
frame.data.pages[0].authors[index] = {
email: author
};
}
});
}
if (frame.data.pages[0].tags) {
frame.data.pages[0].tags.forEach((tag, index) => {
if (_.isString(tag)) {
frame.data.pages[0].tags[index] = {
name: tag
};
}
});
}
},
edit(apiConfig, frame) {
this.add(...arguments);
this.add(...arguments, {add: false});
debug('edit');

View File

@ -136,6 +136,27 @@ module.exports = {
if (options.add) {
frame.data.posts[0].page = false;
}
// CASE: Transform short to long format
if (frame.data.posts[0].authors) {
frame.data.posts[0].authors.forEach((author, index) => {
if (_.isString(author)) {
frame.data.posts[0].authors[index] = {
email: author
};
}
});
}
if (frame.data.posts[0].tags) {
frame.data.posts[0].tags.forEach((tag, index) => {
if (_.isString(tag)) {
frame.data.posts[0].tags[index] = {
name: tag
};
}
});
}
},
edit(apiConfig, frame) {

View File

@ -139,67 +139,92 @@
"description": "Authors of the page",
"type": "array",
"items": {
"type": "object",
"properties": {
"id": {
"type": "string",
"maxLength": 24
"anyOf": [{
"type": "object",
"properties": {
"id": {
"type": "string",
"maxLength": 24
},
"slug": {
"type": "string",
"maxLength": 191
},
"email": {
"type": "string",
"maxLength": 191
},
"roles": {
"strip": true
},
"permissions": {
"strip": true
}
},
"slug": {
"type": "string",
"maxLength": 191
},
"email": {
"type": "string",
"maxLength": 191
},
"roles": {
"strip": true
},
"permissions": {
"strip": true
}
},
"anyOf": [
{"required": ["id"]},
{"required": ["slug"]},
{"required": ["email"]}
]
"anyOf": [
{"required": ["id"]},
{"required": ["slug"]},
{"required": ["email"]}
]
}, {
"type": "string",
"maxLength": 191
}]
}
},
"page-tags": {
"description": "Tags of the page",
"type": "array",
"items": {
"type": "object",
"properties": {
"id": {
"type": "string",
"maxLength": 24
"anyOf": [{
"type": "object",
"properties": {
"id": {
"type": "string",
"maxLength": 24
},
"name": {
"type": "string",
"maxLength": 191
},
"slug": {
"type": [
"string",
"null"
],
"maxLength": 191
},
"parent": {
"strip": true
},
"parent_id": {
"strip": true
},
"pages": {
"strip": true
}
},
"name": {
"type": "string",
"maxLength": 191
},
"slug": {
"type": ["string", "null"],
"maxLength": 191
},
"parent": {
"strip": true
},
"parent_id": {
"strip": true
},
"pages": {
"strip": true
}
},
"anyOf": [
{"required": ["id"]},
{"required": ["name"]},
{"required": ["slug"]}
]
"anyOf": [
{
"required": [
"id"
]
},
{
"required": [
"name"
]
},
{
"required": [
"slug"
]
}
]
}, {
"type": "string",
"maxLength": 191
}]
}
}
}

View File

@ -139,67 +139,77 @@
"description": "Authors of the post",
"type": "array",
"items": {
"type": "object",
"properties": {
"id": {
"type": "string",
"maxLength": 24
"anyOf": [{
"type": "object",
"properties": {
"id": {
"type": "string",
"maxLength": 24
},
"slug": {
"type": "string",
"maxLength": 191
},
"email": {
"type": "string",
"maxLength": 191
},
"roles": {
"strip": true
},
"permissions": {
"strip": true
}
},
"slug": {
"type": "string",
"maxLength": 191
},
"email": {
"type": "string",
"maxLength": 191
},
"roles": {
"strip": true
},
"permissions": {
"strip": true
}
},
"anyOf": [
{"required": ["id"]},
{"required": ["slug"]},
{"required": ["email"]}
]
"anyOf": [
{"required": ["id"]},
{"required": ["slug"]},
{"required": ["email"]}
]
}, {
"type": "string",
"maxLength": 191
}]
}
},
"post-tags": {
"description": "Tags of the post",
"type": "array",
"items": {
"type": "object",
"properties": {
"id": {
"type": "string",
"maxLength": 24
"anyOf": [{
"type": "object",
"properties": {
"id": {
"type": "string",
"maxLength": 24
},
"name": {
"type": "string",
"maxLength": 191
},
"slug": {
"type": ["string", "null"],
"maxLength": 191
},
"parent": {
"strip": true
},
"parent_id": {
"strip": true
},
"posts": {
"strip": true
}
},
"name": {
"type": "string",
"maxLength": 191
},
"slug": {
"type": ["string", "null"],
"maxLength": 191
},
"parent": {
"strip": true
},
"parent_id": {
"strip": true
},
"posts": {
"strip": true
}
},
"anyOf": [
{"required": ["id"]},
{"required": ["name"]},
{"required": ["slug"]}
]
"anyOf": [
{"required": ["id"]},
{"required": ["name"]},
{"required": ["slug"]}
]
}, {
"type": "string",
"maxLength": 191
}]
}
}
}

View File

@ -178,4 +178,50 @@ describe('Unit: v2/utils/serializers/input/pages', function () {
frame.options.formats.should.containEql('plaintext');
});
});
describe('Ensure relations format', function () {
it('relations is array of objects', function () {
const apiConfig = {};
const frame = {
options: {},
data: {
pages: [
{
id: 'id1',
authors: [{id: 'id'}],
tags: [{slug: 'slug1', name: 'hey'}, {slug: 'slug2'}]
}
]
}
};
serializers.input.pages.edit(apiConfig, frame);
frame.data.pages[0].authors.should.eql([{id: 'id'}]);
frame.data.pages[0].tags.should.eql([{slug: 'slug1', name: 'hey'}, {slug: 'slug2'}]);
});
it('authors is array of strings', function () {
const apiConfig = {};
const frame = {
options: {},
data: {
pages: [
{
id: 'id1',
authors: ['email1', 'email2'],
tags: ['name1', 'name2'],
}
]
}
};
serializers.input.pages.edit(apiConfig, frame);
frame.data.pages[0].authors.should.eql([{email: 'email1'}, {email: 'email2'}]);
frame.data.pages[0].tags.should.eql([{name: 'name1'}, {name: 'name2'}]);
});
});
});

View File

@ -447,5 +447,51 @@ describe('Unit: v2/utils/serializers/input/posts', function () {
postData.mobiledoc.should.equal('{"version":"0.3.1","atoms":[],"cards":[],"markups":[],"sections":[[1,"p",[[0,[],0,"this is great feature"]]]]}');
});
});
describe('Ensure relations format', function () {
it('relations is array of objects', function () {
const apiConfig = {};
const frame = {
options: {},
data: {
posts: [
{
id: 'id1',
authors: [{id: 'id'}],
tags: [{slug: 'slug1', name: 'hey'}, {slug: 'slug2'}]
}
]
}
};
serializers.input.posts.edit(apiConfig, frame);
frame.data.posts[0].authors.should.eql([{id: 'id'}]);
frame.data.posts[0].tags.should.eql([{slug: 'slug1', name: 'hey'}, {slug: 'slug2'}]);
});
it('authors is array of strings', function () {
const apiConfig = {};
const frame = {
options: {},
data: {
posts: [
{
id: 'id1',
authors: ['email1', 'email2'],
tags: ['name1', 'name2'],
}
]
}
};
serializers.input.posts.edit(apiConfig, frame);
frame.data.posts[0].authors.should.eql([{email: 'email1'}, {email: 'email2'}]);
frame.data.posts[0].tags.should.eql([{name: 'name1'}, {name: 'name2'}]);
});
});
});
});

View File

@ -388,6 +388,38 @@ describe('Unit: v2/utils/validators/input/pages', function () {
return validators.input.pages.edit(apiConfig, frame);
});
it('should pass with authors as array with strings', function () {
const frame = {
options: {},
data: {
pages: [
{
authors: ['email1', 'email2'],
updated_at: new Date().toISOString()
}
]
}
};
return validators.input.pages.edit(apiConfig, frame);
});
it('should pass with authors as array with strings & objects', function () {
const frame = {
options: {},
data: {
pages: [
{
authors: ['email1', {email: 'email'}],
updated_at: new Date().toISOString()
}
]
}
};
return validators.input.pages.edit(apiConfig, frame);
});
});
});
});

View File

@ -388,6 +388,38 @@ describe('Unit: v2/utils/validators/input/posts', function () {
return validators.input.posts.edit(apiConfig, frame);
});
it('should pass with authors as array with strings', function () {
const frame = {
options: {},
data: {
posts: [
{
authors: ['email1', 'email2'],
updated_at: new Date().toISOString()
}
]
}
};
return validators.input.posts.edit(apiConfig, frame);
});
it('should pass with authors as array with strings & objects', function () {
const frame = {
options: {},
data: {
posts: [
{
authors: ['email1', {email: 'email'}],
updated_at: new Date().toISOString()
}
]
}
};
return validators.input.posts.edit(apiConfig, frame);
});
});
});
});