Skip to content

Commit

Permalink
Derived type constraints in Markdown
Browse files Browse the repository at this point in the history
  • Loading branch information
HeikoTheissen committed Oct 10, 2024
1 parent 14647cd commit 78d9aa4
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 5 deletions.
29 changes: 24 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,23 @@ 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 +=
" (Allowed derived types: " +
modelElement[voc.Validation.DerivedTypeConstraint]
.map((dt) => {
let link;
if (dt.startsWith("Collection(")) {
dt = dt.substring(11, dt.length - 1);
link = "[.]";
} else link = ".";
return link.replace(".", typeLink({ $Type: dt }));
})
.join(", ") +
")";
}
return type;
}

/**
Expand Down Expand Up @@ -789,6 +807,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 (Allowed derived types: [Tag](https://github.com/oasis-tcs/odata-vocabularies/blob/main/vocabularies/Org.OData.Core.V1.md#Tag), Duration)";
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

0 comments on commit 78d9aa4

Please sign in to comment.