google / schema-dts

JSON-LD TypeScript types for Schema.org vocabulary
Apache License 2.0
860 stars 32 forks source link

BUG: schema-dts-gen and custom context - prefixes other than "schema" are ignored during generation of code for types #146

Closed eamon-otuathail closed 3 years ago

eamon-otuathail commented 3 years ago

schema-dts-gen accepts a --context parameter, which sets the value of the @context property.

Imagine we have a custom schema for an external extension which we will call custom-schema.nt, which is a mix of all the schema.org types along with custom types, which we prefix with the demo namespace. We can auto-generate TypeScript code as follows:

npx schema-dts-gen --ontology=https://somelocation.com/custom-schema.nt 
--context=schema:https://schema.org/,demo:https://example.com/demo/ > my-generated-code.ts

This nicely creates WithContext and Graph:

export type WithContext<T extends Thing> = Graph | (T & {
    "@context": {
        "schema": "https://schema.org/";
        "demo": "https://example.com/demo/";
    };
});
export interface Graph {
    "@context": {
        "schema": "https://schema.org/";
        "demo": "https://example.com/demo/";
    };
    "@graph": readonly Thing[];
}

However, the custom demo prefix is ignored during code generation - currently we get:

interface DemoBase extends ThingBase {
    /** Indicates whether this resource is a template. */
    "https://example.com/demo/isTemplate"?: SchemaValue<Boolean>;
}
interface DemoLeaf extends DemoBase {
    "@type": "https://example.com/demo/Demo";
}
/** A Demo */
export type Demo = DemoLeaf;

whereas what would be nice would be:

interface DemoBase extends ThingBase {
    /** Indicates whether this resource is a template. */
    "demo:isTemplate"?: SchemaValue<Boolean>;
}
interface DemoLeaf extends DemoBase {
    "@type": "demo:Demo";
}
/** A Demo */
export type Demo = DemoLeaf;
Eyas commented 3 years ago

Thanks! I agree schema-dts-gen should take advantage of the demo: prefix here.

Eyas commented 3 years ago

I'm looking at my test cases and this is supposed to work already. Can you provide a repro?

Eyas commented 3 years ago

Ah -- FWIW, if you change your --context to:

--context=schema:https://schema.org/,demo:https://example.com/demo

(i.e. without the final "/" for demo)

then it works as expected.

I'll make sure to include a fix for this in the near future.

eamon-otuathail commented 3 years ago

Hi Eyas,

Yes, I tried this and it works very nicely, thanks - Eamon

FWIW, if you change your --context to: --context=schema:https://schema.org/,demo:https://example.com/demo (i.e. without the final "/" for demo) then it works as expected.