From b1761f64e8ec589f27713bf2bc2d843e5dfa7c29 Mon Sep 17 00:00:00 2001 From: abluchet Date: Tue, 19 Sep 2017 18:05:08 +0200 Subject: [PATCH] attempt at fix #15 --- src/hydra/parseHydraDocumentation.js | 104 +++++++++------------- src/hydra/parseHydraDocumentation.test.js | 30 ++++++- 2 files changed, 69 insertions(+), 65 deletions(-) diff --git a/src/hydra/parseHydraDocumentation.js b/src/hydra/parseHydraDocumentation.js index 4445f9c..664a667 100644 --- a/src/hydra/parseHydraDocumentation.js +++ b/src/hydra/parseHydraDocumentation.js @@ -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 diff --git a/src/hydra/parseHydraDocumentation.test.js b/src/hydra/parseHydraDocumentation.test.js index c0399b2..e108852 100644 --- a/src/hydra/parseHydraDocumentation.test.js +++ b/src/hydra/parseHydraDocumentation.test.js @@ -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", @@ -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", @@ -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",