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

Exclude properties of type never when emitting model schemas #1127

Merged
merged 1 commit into from
Oct 5, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@cadl-lang/openapi3",
"comment": "Exclude properties of type `never` when emitting model schemas",
"type": "patch"
}
],
"packageName": "@cadl-lang/openapi3"
}
5 changes: 5 additions & 0 deletions packages/openapi3/src/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1046,6 +1046,11 @@ function createOAPIEmitter(program: Program, options: ResolvedOpenAPI3EmitterOpt
continue;
}

if (isNeverType(prop.type)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wondering if it would make sense to have this built at the checker level, like it doesn't even include the property in the model object then.

I guess the issue with it then is you can't project it if the property is not there.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, I think that's a good way to look at it. We might want to make the property have some other type in the future, so possibly better to leave it in at the checker level.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we maybe open a design issue on this? It's also worth considering if failing that it would be good to put it in MetadataInfo.isPayloadProperty called just above. Upside, one fewer thing for an emitter that follows the (to-be-written, #1083) docs on metadata. Seems reasonable to say respond false to both isPayload and isMetadata for that. Speaking of which, do we need to handle @header something: never, etc.?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is fine, just some thoughts for the future.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, I will open a design issue for it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I just opened #1128

// If the property has a type of 'never', don't include it in the schema
continue;
}

if (!prop.optional) {
if (!modelSchema.required) {
modelSchema.required = [];
Expand Down
30 changes: 30 additions & 0 deletions packages/openapi3/test/models.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -779,4 +779,34 @@ describe("openapi3: models", () => {

expectDiagnostics(diagnostics, [{ code: "@cadl-lang/openapi3/inline-cycle" }]);
});

it("excludes properties with type 'never'", async () => {
const res = await oapiForModel(
"Bar",
`
model Foo {
y: int32;
nope: never;
};
model Bar extends Foo {
x: int32;
}`
);

ok(res.isRef);
ok(res.schemas.Foo, "expected definition named Foo");
ok(res.schemas.Bar, "expected definition named Bar");
deepStrictEqual(res.schemas.Bar, {
type: "object",
properties: { x: { type: "integer", format: "int32" } },
allOf: [{ $ref: "#/components/schemas/Foo" }],
required: ["x"],
});

deepStrictEqual(res.schemas.Foo, {
type: "object",
properties: { y: { type: "integer", format: "int32" } },
required: ["y"],
});
});
});