Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Derived type constraints in Markdown #279

Merged
merged 4 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions lib/csdl2markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,9 @@ module.exports.csdl2markdown = function (filename, csdl, referenced = {}) {
}

if (type.$Kind == "TypeDefinition") {
lines.push("**Type:** " + typeLink({ $Type: type.$UnderlyingType }));
lines.push(
"**Type:** " + typeLink({ ...type, $Type: type.$UnderlyingType }),
);
lines.push("");
}

Expand Down Expand Up @@ -611,7 +613,8 @@ module.exports.csdl2markdown = function (filename, csdl, referenced = {}) {
*/
function applicableTermsList(applicableTerms) {
const text = [];
if (applicableTerms.length > 0) text.push("<br>Applicable Annotation Terms:<ul>");
if (applicableTerms.length > 0)
text.push("<br>Applicable Annotation Terms:<ul>");
applicableTerms.forEach((term) => {
text.push(`<li>${typeLink({ $Type: term })}</li>`);
});
Expand Down Expand Up @@ -729,7 +732,7 @@ module.exports.csdl2markdown = function (filename, csdl, referenced = {}) {
if (modelElement.$Type == "com.sap.vocabularies.Common.v1.Experimental")
customFile = "Common.md";

return (
let type =
(modelElement.$Collection ? "\\[" : "") +
(customType ? "[" : "") +
(customType
Expand All @@ -741,8 +744,20 @@ module.exports.csdl2markdown = function (filename, csdl, referenced = {}) {
: np.name) +
(modelElement.$Nullable ? "?" : "") +
(customType ? "](" + customFile + "#" + np.name + ")" : "") +
(modelElement.$Collection ? "\\]" : "")
);
(modelElement.$Collection ? "\\]" : "");
if (modelElement[voc.Validation.DerivedTypeConstraint]) {
type += "<br>Allowed derived types:<ul>";
for (let dt of modelElement[voc.Validation.DerivedTypeConstraint]) {
let link;
if (dt.startsWith("Collection(")) {
dt = dt.substring(11, dt.length - 1);
link = "[.]";
} else link = ".";
type += `<li>${link.replace(".", typeLink({ $Type: dt }))}</li>`;
}
type += "</ul>";
}
return type;
}

/**
Expand Down Expand Up @@ -789,6 +804,7 @@ module.exports.csdl2markdown = function (filename, csdl, referenced = {}) {
"AllowedValues",
"ApplicableTerms",
"AllowedTerms",
"DerivedTypeConstraint",
"Exclusive",
"Maximum",
"Minimum",
Expand Down
63 changes: 63 additions & 0 deletions test/csdl2markdown.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,69 @@ describe("Edge cases", function () {
const markdown = lib.csdl2markdown(filename, vocabulary);
assert.deepStrictEqual(markdown, expectedMarkdown);
});

it("Allowed derived types", function () {
const filename = "allowedDerivedTypes.xml";
const type = {
"@Org.OData.Core.V1.Description": "Tag or duration",
"@Org.OData.Validation.V1.DerivedTypeConstraint": [
"Org.OData.Core.V1.Tag",
"Edm.Duration",
],
};
const mdtype =
"PrimitiveType<br>Allowed derived types:<ul><li>[Tag](https://github.com/oasis-tcs/odata-vocabularies/blob/main/vocabularies/Org.OData.Core.V1.md#Tag)</li><li>Duration</li></ul>";
const vocabulary = {
$Version: "4.01",
$Reference: {
"https://oasis-tcs.github.io/odata-vocabularies/vocabularies/Org.OData.Core.V1.json":
{
$Include: [
{
$Namespace: "Org.OData.Core.V1",
},
],
},
},
"Allowed.v1": {
Duration: {
$Kind: "TypeDefinition",
$UnderlyingType: "Edm.PrimitiveType",
...type,
},
Event: {
$Kind: "ComplexType",
Duration: {
$Type: "Edm.PrimitiveType",
...type,
},
},
},
};
const expectedMarkdown = [
"# Allowed Vocabulary",
"**Namespace: [Allowed.v1](allowedDerivedTypes.xml)**",
"",
"",
"",
'<a name="Duration"></a>',
"## Duration",
"**Type:** " + mdtype,
"",
"Tag or duration",
"",
'<a name="Event"></a>',
"## Event",
"",
"",
"Property|Type|Description",
":-------|:---|:----------",
"Duration|" + mdtype + "|Tag or duration",
"",
];
const markdown = lib.csdl2markdown(filename, vocabulary);
assert.deepStrictEqual(markdown, expectedMarkdown);
});
});

function check(actual, expected) {
Expand Down