Skip to content

Commit 097b0bf

Browse files
committed
refactor: address issues in the comments
1 parent 045fe31 commit 097b0bf

File tree

5 files changed

+435
-321
lines changed

5 files changed

+435
-321
lines changed

src/code-gen-process.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -445,11 +445,11 @@ export class CodeGenProcess {
445445
const jsonldOutputFiles: TranslatorIO[] = [];
446446

447447
// Check if we have JSON-LD schemas and options enabled
448-
const hasJsonLdSchemas = configuration.components?.some?.(
449-
(component) =>
450-
component.schemaType === "jsonld-context" ||
451-
component.schemaType === "jsonld-entity" ||
452-
component.schemaType === "jsonld-type",
448+
const hasJsonLdSchemas = configuration.modelTypes?.some?.(
449+
(modelType) =>
450+
modelType.typeData?.schemaType === "jsonld-context" ||
451+
modelType.typeData?.schemaType === "jsonld-entity" ||
452+
modelType.typeData?.schemaType === "jsonld-type",
453453
);
454454

455455
if (hasJsonLdSchemas) {
@@ -533,11 +533,11 @@ export class CodeGenProcess {
533533
configuration.config;
534534

535535
// Check if we have JSON-LD schemas
536-
const hasJsonLdSchemas = configuration.components?.some?.(
537-
(component) =>
538-
component.schemaType === "jsonld-context" ||
539-
component.schemaType === "jsonld-entity" ||
540-
component.schemaType === "jsonld-type",
536+
const hasJsonLdSchemas = configuration.modelTypes?.some?.(
537+
(modelType) =>
538+
modelType.typeData?.schemaType === "jsonld-context" ||
539+
modelType.typeData?.schemaType === "jsonld-entity" ||
540+
modelType.typeData?.schemaType === "jsonld-type",
541541
);
542542

543543
return await this.createOutputFileInfo(

src/jsonld-schema-resolver.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -114,16 +114,24 @@ export class JsonLdSchemaResolver {
114114
};
115115
}
116116

117-
// Process other properties
118-
Object.entries(schema).forEach(([key, value]) => {
119-
if (!key.startsWith("@")) {
120-
resolvedSchema.properties[key] = this.resolveProperty(
121-
key,
122-
value,
123-
schema["@context"],
124-
);
125-
}
126-
});
117+
// Process properties from the properties object if it exists
118+
if (schema.properties && typeof schema.properties === "object") {
119+
Object.entries(schema.properties).forEach(([key, value]) => {
120+
// Skip @ properties and x-jsonld properties as they're handled elsewhere
121+
if (!key.startsWith("@") && !key.startsWith("x-jsonld")) {
122+
resolvedSchema.properties[key] = this.resolveProperty(
123+
key,
124+
value,
125+
schema["@context"],
126+
);
127+
}
128+
});
129+
}
130+
131+
// Copy required array if it exists
132+
if (Array.isArray(schema.required)) {
133+
resolvedSchema.required.push(...schema.required);
134+
}
127135

128136
return resolvedSchema;
129137
}

templates/base/jsonld-context-data-contract.ejs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
<%
2-
const { contract, utils } = it;
2+
const { modelTypes, utils } = it;
33
const { formatDescription, require, _ } = utils;
4+
5+
// Filter for JSON-LD context types
6+
const jsonldContexts = modelTypes.filter(contract =>
7+
contract.typeData?.schemaType === "jsonld-context"
8+
);
49
%>
10+
<% for (const contract of jsonldContexts) { %>
511
<% if (contract.description) { %>
612
/**
713
* <%~ formatDescription(contract.description) %>
@@ -12,4 +18,6 @@ export interface <%~ contract.name %> {
1218
<%~ includeFile('@base/object-field-jsdoc.ejs', { ...it, field }) %>
1319
<%~ field.name %><%~ field.isRequired ? '' : '?' %>: <%~ field.value %>;
1420
<% } %>
15-
}
21+
}
22+
23+
<% } %>

templates/base/jsonld-entity-data-contract.ejs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
<%
2-
const { contract, utils } = it;
2+
const { modelTypes, utils } = it;
33
const { formatDescription, require, _ } = utils;
4+
5+
// Filter for JSON-LD entity types
6+
const jsonldEntities = modelTypes.filter(contract =>
7+
contract.typeData?.schemaType === "jsonld-entity"
8+
);
49
%>
10+
<% for (const contract of jsonldEntities) { %>
511
<% if (contract.description) { %>
612
/**
713
* <%~ formatDescription(contract.description) %>
@@ -17,4 +23,6 @@ export interface <%~ contract.name %> extends JsonLdEntity {
1723
<%~ includeFile('@base/object-field-jsdoc.ejs', { ...it, field }) %>
1824
<%~ field.name %><%~ field.isRequired ? '' : '?' %>: <%~ field.value %><%~ field.isNullable ? ' | null' : ''%>;
1925
<% } %>
20-
}
26+
}
27+
28+
<% } %>

0 commit comments

Comments
 (0)