Skip to content

Commit

Permalink
Inline stub object
Browse files Browse the repository at this point in the history
  • Loading branch information
ralfhandl committed Jan 29, 2024
1 parent 9af4188 commit b80abf1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 19 deletions.
16 changes: 9 additions & 7 deletions lib/csdl2openapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ module.exports.csdl2openapi = function (
list: [],
used: {},
entityReferenceNeeded: false,
stubObjectNeeded: false,
};

const model = new EDM();
Expand Down Expand Up @@ -2091,8 +2090,6 @@ module.exports.csdl2openapi = function (
if (requiredSchemas.entityReferenceNeeded)
ordered.entityReference = entityReference();
ordered.error = error();
if (requiredSchemas.stubObjectNeeded)
ordered.stub = { title: "Stub object", type: "object" };
}

return ordered;
Expand Down Expand Up @@ -2273,14 +2270,14 @@ module.exports.csdl2openapi = function (
}

/**
* Return object if it references a kept type, otherwise clone it and reference the stub type
* Return element if it references a kept type, otherwise clone it and reference the stub type
* @param {object} element typed model element
* @return {object} original element or clone referencing stub type
*/
function stubIfNotKept(element) {
if (entityTypesToKeep && !entityTypesToKeep.includes(element?.$Type)) {
const clone = { ...element };
clone.$Type = "stub";
requiredSchemas.stubObjectNeeded = true;
clone.$Type = `stub-${element.$Type}`;
return clone;
} else return element;
}
Expand Down Expand Up @@ -2641,7 +2638,12 @@ module.exports.csdl2openapi = function (
};
break;
default:
if (element.$Type.startsWith("Edm.")) {
if (element.$Type.startsWith("stub-")) {
s = {
type: "object",
description: `Stub for ${element.$Type.substring(5)}`,
};
} else if (element.$Type.startsWith("Edm.")) {
messages.push("Unknown type: " + element.$Type);
} else {
let type = model.element(element.$Type);
Expand Down
29 changes: 17 additions & 12 deletions test/keep.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,15 +162,16 @@ describe("Keep", function () {
complex: { $ref: "#/components/schemas/this.CT" },
simple: { $ref: "#/components/schemas/this.TD" },
contained: { $ref: "#/components/schemas/this.CET" },
two: { $ref: "#/components/schemas/stub" },
two: { type: "object", description: "Stub for this.ET2" },
twoMany: {
type: "array",
items: { $ref: "#/components/schemas/stub" },
items: { type: "object", description: "Stub for this.ET2" },
},
"twoMany@count": { $ref: "#/components/schemas/count" },
twoOptional: {
nullable: true,
allOf: [{ $ref: "#/components/schemas/stub" }],
type: "object",
description: "Stub for this.ET2",
},
},
},
Expand Down Expand Up @@ -215,11 +216,6 @@ describe("Keep", function () {
"Operations",
);
assert.deepStrictEqual(schemas(actual), schemas(expected), "Schemas");
assert.deepStrictEqual(
actual.components.schemas.stub,
{ title: "Stub object", type: "object" },
"Stub object",
);
assert.deepStrictEqual(
actual.components.schemas["this.ET"],
expected.components.schemas["this.ET"],
Expand Down Expand Up @@ -748,12 +744,21 @@ describe("Keep", function () {
operations(expected),
"Operations",
);
assert.deepStrictEqual(schemas(actual), schemas(expected), "Schemas");
assert.deepStrictEqual(
actual.components.schemas.stub,
{ title: "Stub object", type: "object" },
"Stub object",
actual.paths["/Set/{id}/this.act"].post.responses["200"].content[
"application/json"
].schema,
{ type: "object", description: "Stub for this.ET2" },
"stubbed action return type",
);
assert.deepStrictEqual(
actual.paths["/Set/{id}/this.fun()"].get.responses["200"].content[
"application/json"
].schema,
{ type: "object", description: "Stub for this.ET2" },
"stubbed function return type",
);
assert.deepStrictEqual(schemas(actual), schemas(expected), "Schemas");
assert.deepStrictEqual(
actual.components.schemas["this.ET"],
expected.components.schemas["this.ET"],
Expand Down

0 comments on commit b80abf1

Please sign in to comment.