Skip to content

Commit

Permalink
Infer default rdfContext from rdfsClass
Browse files Browse the repository at this point in the history
  • Loading branch information
NoelDeMartin committed Mar 1, 2024
1 parent 161e468 commit 7283e11
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
### Changed

- `SolidTypeRegistration.forClass` is now declared as an array.
- Default `rdfContext` resolution changed to prioritize the vocab used in `rdfsClass` if present.

## [v0.5.2](https://github.com/NoelDeMartin/soukai-solid/releases/tag/v0.5.2) - 2023-11-03

Expand Down
12 changes: 12 additions & 0 deletions src/models/SolidModel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,18 @@ describe('SolidModel', () => {
expect(fields.title?.rdfPropertyAliases).toEqual([]);
});

it('defaults to rdfsClass context if default rdfContext is missing', () => {
class StubModel extends SolidModel {

public static rdfsClass = 'http://xmlns.com/foaf/0.1/Stub';

}

bootModels({ StubModel });

expect(StubModel.rdfContexts.default).toEqual('http://xmlns.com/foaf/0.1/');
});

it('defaults to first context if rdfProperty is missing', () => {
class StubModel extends SolidModel {

Expand Down
26 changes: 19 additions & 7 deletions src/models/SolidModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
shortId,
stringToSlug,
tap,
toString,
urlClean,
urlParentDirectory,
urlParse,
Expand Down Expand Up @@ -483,19 +484,30 @@ export class SolidModel extends SolidModelBase {
...initialRdfContexts,
...Object.getOwnPropertyDescriptor(modelClass, 'rdfContexts')?.value ?? {} as Record<string, string>,
};
const buildInRdfContexts = {
crdt: 'https://vocab.noeldemartin.com/crdt/',
foaf: 'http://xmlns.com/foaf/0.1/',
ldp: 'http://www.w3.org/ns/ldp#',
pim: 'http://www.w3.org/ns/pim/space#',
posix: 'http://www.w3.org/ns/posix/stat#',
purl: 'http://purl.org/dc/terms/',
rdf: 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
rdfs: 'http://www.w3.org/2000/01/rdf-schema#',
schema: 'https://schema.org/',
solid: 'http://www.w3.org/ns/solid/terms#',
xsd: 'http://www.w3.org/2001/XMLSchema#',
};
const rdfContextFromClass = () => Object.values({ ...rdfContexts, ...buildInRdfContexts }).find(
rdfContext => modelClass.rdfsClass?.startsWith(toString(rdfContext)),
);

rdfContexts.default ??= rdfContext ?? Object.values(rdfContexts).find(url => !!url);
rdfContexts.default ??= rdfContext ?? rdfContextFromClass() ?? Object.values(rdfContexts).find(url => !!url);

if (!parentModelClass) {
return {
...rdfContexts,
default: rdfContexts.default ?? 'http://www.w3.org/ns/solid/terms#',
solid: 'http://www.w3.org/ns/solid/terms#',
crdt: 'https://vocab.noeldemartin.com/crdt/',
ldp: 'http://www.w3.org/ns/ldp#',
purl: 'http://purl.org/dc/terms/',
rdf: 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
rdfs: 'http://www.w3.org/2000/01/rdf-schema#',
...buildInRdfContexts,
};
}

Expand Down

0 comments on commit 7283e11

Please sign in to comment.