Skip to content

Typespec Generation for Empty Schema #4923 #4934

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

Merged
merged 5 commits into from
Nov 1, 2024
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,7 @@
---
changeKind: fix
packages:
- "@typespec/openapi3"
---

tsp-openapi3 - fixes typespec generation for empty schema
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ function generateScalar(scalar: TypeSpecScalar, context: Context): string {
definitions.push(...generateDecorators(scalar.decorators));
const type = context.generateTypeFromRefableSchema(scalar.schema, scalar.scope);

definitions.push(`scalar ${scalar.name} extends ${type};`);
if (type === "unknown") {
definitions.push(`scalar ${scalar.name};`);
} else {
definitions.push(`scalar ${scalar.name} extends ${type};`);
}

return definitions.join("\n");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,44 @@ it("should convert an OpenAPI3 document to a formatted TypeSpec program", async
),
);
});

it("converts an OpenAPI 3 document with an empty schema to a valid TypeSpec representation", async () => {
const tsp = await convertOpenAPI3Document({
info: {
title: "Test",
version: "0.0.0",
},
openapi: "3.0.0",
paths: {},
components: {
schemas: {
Foo: {},
},
},
});

strictEqual(
tsp,
await formatTypeSpec(
`
import "@typespec/http";
import "@typespec/openapi";
import "@typespec/openapi3";

using Http;
using OpenAPI;

@service({
title: "Test",
})
@info({
version: "0.0.0",
})
namespace Test;

scalar Foo;
`,
{ printWidth: 100, tabWidth: 2 },
),
);
});
Loading