Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 42 additions & 62 deletions src/hydra/parseHydraDocumentation.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,76 +97,56 @@ export default function parseHydraDocumentation(entrypointUrl, options = {}) {
({ entrypoint, docs, response }) => {
const title = 'undefined' === typeof docs[0]['http://www.w3.org/ns/hydra/core#title'] ? 'API Platform' : docs[0]['http://www.w3.org/ns/hydra/core#title'][0]['@value'];
const entrypointSupportedClass = findSupportedClass(docs, entrypoint[0]['@type'][0]);

let resources = [];
let fields = [];
const resources = [];
const fields = [];

// Add resources
for (let properties of entrypointSupportedClass['http://www.w3.org/ns/hydra/core#supportedProperty']) {
const property = properties['http://www.w3.org/ns/hydra/core#property'][0];
const entrypointSupportedOperations = property['http://www.w3.org/ns/hydra/core#supportedOperation'];

let readableFields = [];
let resourceFields = [];
let writableFields = [];

// Add fields
for (let j = 0; j < entrypointSupportedOperations.length; j++) {
let className = entrypointSupportedOperations[j]['http://www.w3.org/ns/hydra/core#returns'];

// Skip operations not having a return type
if (!className) {
continue;
const property = properties['http://www.w3.org/ns/hydra/core#property'][0]
const className = property['http://www.w3.org/2000/01/rdf-schema#range'][1]['http://www.w3.org/2002/07/owl#equivalentClass'][0]['http://www.w3.org/2002/07/owl#allValuesFrom'][0]['@value']
const readableFields = [];
const resourceFields = [];
const writableFields = [];

const supportedClass = findSupportedClass(docs, className);
for (let supportedProperties of supportedClass['http://www.w3.org/ns/hydra/core#supportedProperty']) {
const supportedProperty = supportedProperties['http://www.w3.org/ns/hydra/core#property'][0];
const range = supportedProperty['http://www.w3.org/2000/01/rdf-schema#range'] ? supportedProperty['http://www.w3.org/2000/01/rdf-schema#range'][0]['@id'] : null;

const field = new Field(
supportedProperty['http://www.w3.org/2000/01/rdf-schema#label'][0]['@value'],
{
id: supportedProperty['@id'],
range,
reference: 'http://www.w3.org/ns/hydra/core#Link' === property['@type'][0] ? range : null, // Will be updated in a subsequent pass
required: supportedProperties['http://www.w3.org/ns/hydra/core#required'] ? supportedProperties['http://www.w3.org/ns/hydra/core#required'][0]['@value'] : false,
description: supportedProperties['http://www.w3.org/ns/hydra/core#description'] ? supportedProperties['http://www.w3.org/ns/hydra/core#description'][0]['@value'] : ''
},
);

fields.push(field);
resourceFields.push(field);

if (supportedProperties['http://www.w3.org/ns/hydra/core#readable'][0]['@value']) {
readableFields.push(field);
}

className = className[0]['@id'];

if (0 === className.indexOf('http://www.w3.org/ns/hydra/core')) {
continue;
if (supportedProperties['http://www.w3.org/ns/hydra/core#writable'][0]['@value']) {
writableFields.push(field);
}
}

const supportedClass = findSupportedClass(docs, className);
for (let supportedProperties of supportedClass['http://www.w3.org/ns/hydra/core#supportedProperty']) {
const supportedProperty = supportedProperties['http://www.w3.org/ns/hydra/core#property'][0];
const range = supportedProperty['http://www.w3.org/2000/01/rdf-schema#range'] ? supportedProperty['http://www.w3.org/2000/01/rdf-schema#range'][0]['@id'] : null;

const field = new Field(
supportedProperty['http://www.w3.org/2000/01/rdf-schema#label'][0]['@value'],
{
id: supportedProperty['@id'],
range,
reference: 'http://www.w3.org/ns/hydra/core#Link' === property['@type'][0] ? range : null, // Will be updated in a subsequent pass
required: supportedProperties['http://www.w3.org/ns/hydra/core#required'] ? supportedProperties['http://www.w3.org/ns/hydra/core#required'][0]['@value'] : false,
description: supportedProperties['http://www.w3.org/ns/hydra/core#description'] ? supportedProperties['http://www.w3.org/ns/hydra/core#description'][0]['@value'] : ''
},
);

fields.push(field);
resourceFields.push(field);

if (supportedProperties['http://www.w3.org/ns/hydra/core#readable'][0]['@value']) {
readableFields.push(field);
}

if (supportedProperties['http://www.w3.org/ns/hydra/core#writable'][0]['@value']) {
writableFields.push(field);
}
resources.push(new Resource(
guessNameFromUrl(entrypoint[0][property['@id']][0]['@id'], entrypointUrl),
entrypoint[0][property['@id']][0]['@id'],
{
id: supportedClass['@id'],
title: supportedClass['http://www.w3.org/ns/hydra/core#title'][0]['@value'],
fields: resourceFields,
readableFields,
writableFields
}

resources.push(new Resource(
guessNameFromUrl(entrypoint[0][property['@id']][0]['@id'], entrypointUrl),
entrypoint[0][property['@id']][0]['@id'],
{
id: supportedClass['@id'],
title: supportedClass['http://www.w3.org/ns/hydra/core#title'][0]['@value'],
fields: resourceFields,
readableFields,
writableFields
}
));

break;
}
));
}

// Resolve references
Expand Down
30 changes: 27 additions & 3 deletions src/hydra/parseHydraDocumentation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,15 @@ test('parse a Hydra documentation', () => {
"@type": "hydra:Link",
"domain": "#Entrypoint",
"rdfs:label": "The collection of Book resources",
"range": "hydra:PagedCollection",
"rdfs:range": [
"hydra:Collection",
{
"owl:equivalentClass": {
"owl:onProperty": "hydra:member",
"owl:allValuesFrom": "http://schema.org/Book"
}
}
],
"hydra:supportedOperation": [
{
"@type": "hydra:Operation",
Expand Down Expand Up @@ -355,7 +363,15 @@ test('parse a Hydra documentation', () => {
"@type": "hydra:Link",
"domain": "#Entrypoint",
"rdfs:label": "The collection of Review resources",
"range": "hydra:PagedCollection",
"rdfs:range": [
"hydra:Collection",
{
"owl:equivalentClass": {
"owl:onProperty": "hydra:member",
"owl:allValuesFrom": "http://schema.org/Review"
}
}
],
"hydra:supportedOperation": [
{
"@type": "hydra:Operation",
Expand Down Expand Up @@ -385,7 +401,15 @@ test('parse a Hydra documentation', () => {
"@type": "hydra:Link",
"domain": "#Entrypoint",
"rdfs:label": "The collection of custom resources",
"range": "hydra:PagedCollection",
"rdfs:range": [
"hydra:Collection",
{
"owl:equivalentClass": {
"owl:onProperty": "hydra:member",
"owl:allValuesFrom": "#CustomResource"
}
}
],
"hydra:supportedOperation": [
{
"@type": "hydra:Operation",
Expand Down