Skip to content

Commit

Permalink
Fix shorthand default rdfContext inference
Browse files Browse the repository at this point in the history
  • Loading branch information
NoelDeMartin committed Mar 1, 2024
1 parent 7283e11 commit b98b087
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
15 changes: 11 additions & 4 deletions src/models/SolidModel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,15 +194,22 @@ describe('SolidModel', () => {
});

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

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

}

bootModels({ StubModel });
class B extends SolidModel {

public static rdfsClass = 'foaf:B';

}

bootModels({ A, B });

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

it('defaults to first context if rdfProperty is missing', () => {
Expand Down
10 changes: 6 additions & 4 deletions src/models/SolidModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -497,17 +497,19 @@ export class SolidModel extends SolidModelBase {
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)),
);
const rdfContextFromClass = () => Object.entries({ ...rdfContexts, ...buildInRdfContexts }).find(
([shorthand, url]) =>
modelClass.rdfsClass?.startsWith(`${shorthand}:`) ||
modelClass.rdfsClass?.startsWith(toString(url)),
)?.[1];

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

if (!parentModelClass) {
return {
...rdfContexts,
default: rdfContexts.default ?? 'http://www.w3.org/ns/solid/terms#',
...buildInRdfContexts,
default: rdfContexts.default ?? 'http://www.w3.org/ns/solid/terms#',
};
}

Expand Down

0 comments on commit b98b087

Please sign in to comment.