-
-
Notifications
You must be signed in to change notification settings - Fork 73
WIP: Serialization handling for cacheability #44
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
Open
olivermack
wants to merge
4
commits into
api-platform:main
Choose a base branch
from
olivermack:cacheability
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
// @flow | ||
|
||
import Api from "./Api"; | ||
import Resource from "./Resource"; | ||
import Field from "./Field"; | ||
import Operation from "./Operation"; | ||
import Parameter from "./Parameter"; | ||
|
||
export default class ApiSerializer { | ||
/** | ||
* @param {Api} api | ||
* @return {object} a POJO | ||
*/ | ||
serialize(api: Api) { | ||
const { resources, ...rest } = api; | ||
|
||
return { | ||
...rest, | ||
resources: resources.map(this.serializeResource) | ||
}; | ||
} | ||
|
||
serializeResource = (resource: Resource) => { | ||
const { | ||
readableFields, | ||
writableFields, | ||
operations, | ||
parameters, | ||
...rest | ||
} = resource; | ||
const result = { | ||
...rest | ||
}; | ||
|
||
if (readableFields) { | ||
result.readableFields = readableFields.map(this.serializeField); | ||
} | ||
if (writableFields) { | ||
result.writableFields = writableFields.map(this.serializeField); | ||
} | ||
if (operations) { | ||
result.operations = operations.map(this.serializeOperation); | ||
} | ||
if (parameters) { | ||
result.parameters = parameters.map(this.serializeParameter); | ||
} | ||
|
||
return result; | ||
}; | ||
|
||
serializeField = (field: Field) => { | ||
const { reference, ...rest } = field; | ||
return { | ||
...rest, | ||
reference: | ||
reference && reference instanceof Resource ? reference.id : null | ||
}; | ||
}; | ||
|
||
serializeOperation = (operation: Operation) => { | ||
return { | ||
...operation | ||
}; | ||
}; | ||
|
||
serializeParameter = (parameter: Parameter) => { | ||
return { | ||
...parameter | ||
}; | ||
}; | ||
|
||
/** | ||
* @param {object} data the serialized POJO | ||
* @return {Api} | ||
*/ | ||
deserialize(data) { | ||
const { resources = [], entrypointUrl, ...rest } = data; | ||
const preparedResources = []; | ||
const allFields = []; | ||
const allOperations = []; | ||
const allParameters = []; | ||
|
||
if (resources) { | ||
olivermack marked this conversation as resolved.
Show resolved
Hide resolved
|
||
for (const resourceData of resources) { | ||
const { | ||
name, | ||
url, | ||
readableFields, | ||
writableFields, | ||
operations, | ||
parameters, | ||
...resourceRest | ||
} = resourceData; | ||
const resourceOptions = { ...resourceRest }; | ||
const resourceReadableFields = []; | ||
const resourceWritableFields = []; | ||
const resourceOperations = []; | ||
const resourceParameters = []; | ||
|
||
if (readableFields) { | ||
for (const { fieldName, ...fieldOptions } of readableFields) { | ||
const field = new Field(fieldName, fieldOptions); | ||
resourceReadableFields.push(field); | ||
allFields.push(field); | ||
} | ||
|
||
resourceOptions.readableFields = resourceReadableFields; | ||
} | ||
|
||
if (writableFields) { | ||
for (const { fieldName, ...fieldOptions } of writableFields) { | ||
const field = new Field(fieldName, fieldOptions); | ||
resourceWritableFields.push(field); | ||
allFields.push(field); | ||
} | ||
|
||
resourceOptions.writableFields = resourceWritableFields; | ||
} | ||
|
||
if (operations) { | ||
for (const { operationName, ...operationOptions } of operations) { | ||
const operation = new Operation(operationName, operationOptions); | ||
resourceOperations.push(operation); | ||
allOperations.push(operation); | ||
} | ||
|
||
resourceOptions.operations = resourceOperations; | ||
} | ||
|
||
if (parameters) { | ||
for (const { variable, range, required, description } of parameters) { | ||
const parameter = new Parameter( | ||
variable, | ||
range, | ||
required, | ||
description | ||
); | ||
resourceParameters.push(parameter); | ||
allParameters.push(parameter); | ||
} | ||
|
||
resourceOptions.parameters = resourceParameters; | ||
} | ||
|
||
preparedResources.push(new Resource(name, url, resourceOptions)); | ||
} | ||
|
||
// Resolve references | ||
for (const field of allFields) { | ||
if (null !== field.reference) { | ||
field.reference = | ||
preparedResources.find( | ||
resource => resource.id === field.reference | ||
) || null; | ||
} | ||
} | ||
} | ||
|
||
return new Api(entrypointUrl, { | ||
...rest, | ||
resources: preparedResources | ||
}); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
import Api from "./Api"; | ||
import ApiSerializer from "./ApiSerializer"; | ||
import Resource from "./Resource"; | ||
import Field from "./Field"; | ||
import Operation from "./Operation"; | ||
import Parameter from "./Parameter"; | ||
|
||
test("can serialize and deserialize Api", () => { | ||
const book = new Resource("Books", "http://localhost/books", { | ||
id: "http://schema.org/Book", | ||
title: "Book", | ||
readableFields: [ | ||
new Field("isbn", { | ||
id: "http://schema.org/isbn", | ||
range: "http://www.w3.org/2001/XMLSchema#string", | ||
reference: null, | ||
required: true, | ||
description: "The ISBN of the book", | ||
maxCardinality: null, | ||
deprecated: false | ||
}), | ||
new Field("name", { | ||
id: "http://schema.org/name", | ||
range: "http://www.w3.org/2001/XMLSchema#string", | ||
reference: null, | ||
required: true, | ||
description: "The name of the item", | ||
maxCardinality: null, | ||
deprecated: false | ||
}) | ||
], | ||
writableFields: [ | ||
new Field("isbn", { | ||
id: "http://schema.org/isbn", | ||
range: "http://www.w3.org/2001/XMLSchema#string", | ||
reference: null, | ||
required: true, | ||
description: "The ISBN of the book", | ||
maxCardinality: null, | ||
deprecated: false | ||
}), | ||
new Field("name", { | ||
id: "http://schema.org/name", | ||
range: "http://www.w3.org/2001/XMLSchema#string", | ||
reference: null, | ||
required: true, | ||
description: "The name of the item", | ||
maxCardinality: null, | ||
deprecated: false | ||
}) | ||
], | ||
operations: [ | ||
new Operation("Retrieves Book resource.", { | ||
method: "GET", | ||
returns: "http://schema.org/Book", | ||
types: ["http://www.w3.org/ns/hydra/core#Operation"], | ||
deprecated: false | ||
}), | ||
new Operation("Replaces the Book resource.", { | ||
method: "PUT", | ||
expects: "http://schema.org/Book", | ||
returns: "http://schema.org/Book", | ||
types: ["http://www.w3.org/ns/hydra/core#ReplaceResourceOperation"], | ||
deprecated: false | ||
}), | ||
new Operation("Deletes the Book resource.", { | ||
method: "DELETE", | ||
returns: "http://www.w3.org/2002/07/owl#Nothing", | ||
types: ["http://www.w3.org/ns/hydra/core#Operation"], | ||
deprecated: false | ||
}) | ||
], | ||
parameters: [ | ||
new Parameter( | ||
"isbn", | ||
"http://www.w3.org/2001/XMLSchema#string", | ||
false, | ||
"" | ||
) | ||
] | ||
}); | ||
|
||
const review = new Resource("Reviews", "http://localhost/reviews", { | ||
id: "http://schema.org/Review", | ||
title: "Book", | ||
readableFields: [ | ||
new Field("reviewBody", { | ||
id: "http://schema.org/reviewBody", | ||
range: "http://www.w3.org/2001/XMLSchema#string", | ||
reference: null, | ||
required: false, | ||
description: "The actual body of the review", | ||
maxCardinality: null, | ||
deprecated: false | ||
}), | ||
new Field("itemReviewed", { | ||
id: "http://schema.org/itemReviewed", | ||
range: "http://schema.org/Book", | ||
reference: book, | ||
required: true, | ||
description: "The name of the item", | ||
maxCardinality: null, | ||
deprecated: false | ||
}) | ||
], | ||
writableFields: [ | ||
new Field("reviewBody", { | ||
id: "http://schema.org/reviewBody", | ||
range: "http://www.w3.org/2001/XMLSchema#string", | ||
reference: null, | ||
required: true, | ||
description: "The actual body of the review", | ||
maxCardinality: null, | ||
deprecated: false | ||
}), | ||
new Field("itemReviewed", { | ||
id: "http://schema.org/itemReviewed", | ||
range: "http://schema.org/Book", | ||
reference: book, | ||
required: true, | ||
description: "The item that is being reviewed/rated", | ||
maxCardinality: null, | ||
deprecated: false | ||
}) | ||
] | ||
}); | ||
|
||
const api = new Api("http://localhost", { | ||
title: "API Platform's demo", | ||
resources: [book, review] | ||
}); | ||
|
||
const serializer = new ApiSerializer(); | ||
const serialized = serializer.serialize(api); | ||
|
||
// verify that the serialized and stringified versions don't differ | ||
// this ensures that the serializer does not return stuff that cannot be | ||
// expressed in json-string notation properly | ||
expect(JSON.parse(JSON.stringify(serialized))).toEqual(serialized); | ||
|
||
const deserialized = serializer.deserialize(serialized); | ||
|
||
expect(deserialized).toEqual(api); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.