From 0da884af7b81b673dfbcaff3426681ce7ff45819 Mon Sep 17 00:00:00 2001 From: Travis Prescott Date: Thu, 16 Nov 2023 08:37:14 -0800 Subject: [PATCH] Updates. --- packages/http/src/lib.ts | 2 +- packages/http/src/operations.ts | 22 ++++++++++++++----- packages/openapi3/test/decorators.test.ts | 4 +--- .../openapi3/test/no-service-found.test.ts | 17 +++++++++++++- packages/openapi3/test/openapi-output.test.ts | 2 +- packages/openapi3/test/output-file.test.ts | 2 +- packages/openapi3/test/test-host.ts | 4 ++-- 7 files changed, 38 insertions(+), 15 deletions(-) diff --git a/packages/http/src/lib.ts b/packages/http/src/lib.ts index 42a5b92f326..90ef8831f01 100644 --- a/packages/http/src/lib.ts +++ b/packages/http/src/lib.ts @@ -91,7 +91,7 @@ export const $lib = createTypeSpecLibrary({ "no-service-found": { severity: "warning", messages: { - default: paramMessage`No namespace with '@service' was found.`, + default: paramMessage`No namespace with '@service' was found, but Namespace '${"namespace"}' contains routes. Did you mean to annotate this with '@service'?`, }, }, "invalid-type-for-auth": { diff --git a/packages/http/src/operations.ts b/packages/http/src/operations.ts index 6f69aa4374c..cd8fccf1389 100644 --- a/packages/http/src/operations.ts +++ b/packages/http/src/operations.ts @@ -119,14 +119,24 @@ export function getAllRoutes( export function reportIfNoRoutes(program: Program, routes: HttpOperation[]) { if (routes.length === 0) { - // FIXME: Fix this. - reportDiagnostic(program, { - code: "no-service-found", - format: { - namespace: "Blah", + const namespaceCounts = new Map(); + navigateProgram(program, { + namespace: (namespace) => { + namespaceCounts.set(namespace, namespace.operations.size); }, - target: program.getGlobalNamespaceType(), }); + // if there is any namespace with namespaceCounts > 0, then report the diagnostic + for (const [namespace, count] of namespaceCounts) { + if (count > 0) { + reportDiagnostic(program, { + code: "no-service-found", + format: { + namespace: namespace.name, + }, + target: namespace, + }); + } + } } } diff --git a/packages/openapi3/test/decorators.test.ts b/packages/openapi3/test/decorators.test.ts index 07b22d9d9a3..712fba3dd7c 100644 --- a/packages/openapi3/test/decorators.test.ts +++ b/packages/openapi3/test/decorators.test.ts @@ -59,9 +59,7 @@ describe("openapi3: decorators", () => { model Foo {} `); - expectDiagnosticEmpty( - diagnostics.filter((d) => d.code !== "@typespec/http/no-service-found") - ); + expectDiagnosticEmpty(diagnostics); strictEqual(getRef(runner.program, Foo), "../common.json#/definitions/Foo"); }); diff --git a/packages/openapi3/test/no-service-found.test.ts b/packages/openapi3/test/no-service-found.test.ts index 8748113855a..4d2befe1a6b 100644 --- a/packages/openapi3/test/no-service-found.test.ts +++ b/packages/openapi3/test/no-service-found.test.ts @@ -26,7 +26,7 @@ describe("openapi3: no-service-found diagnostic", () => { ); expectDiagnostics(diagnostics, [ { - code: "no-service-found", + code: "@typespec/http/no-service-found", }, ]); }); @@ -42,4 +42,19 @@ describe("openapi3: no-service-found diagnostic", () => { ); expectDiagnosticEmpty(diagnostics); }); + + it("does not emit a warning if a service namespace has routes", async () => { + const diagnostics = await diagnoseOpenApiFor( + ` + @service + namespace Test { + model Foo {}; + + @route("/foo") + op get(): Foo; + } + ` + ); + expectDiagnosticEmpty(diagnostics); + }); }); diff --git a/packages/openapi3/test/openapi-output.test.ts b/packages/openapi3/test/openapi-output.test.ts index de73f1af4c8..55359585cf4 100644 --- a/packages/openapi3/test/openapi-output.test.ts +++ b/packages/openapi3/test/openapi-output.test.ts @@ -19,7 +19,7 @@ async function openapiWithOptions( options: { "@typespec/openapi3": { ...options, "output-file": outPath } }, }); - expectDiagnosticEmpty(diagnostics.filter((x) => x.code !== "@typespec/http/no-service-found")); + expectDiagnosticEmpty(diagnostics); const content = runner.fs.get(outPath)!; return JSON.parse(content); diff --git a/packages/openapi3/test/output-file.test.ts b/packages/openapi3/test/output-file.test.ts index ad479a8aa74..be129deda24 100644 --- a/packages/openapi3/test/output-file.test.ts +++ b/packages/openapi3/test/output-file.test.ts @@ -46,7 +46,7 @@ describe("openapi3: output file", () => { options: { "@typespec/openapi3": { ...options, "emitter-output-dir": outputDir } }, }); - expectDiagnosticEmpty(diagnostics.filter((x) => x.code !== "@typespec/http/no-service-found")); + expectDiagnosticEmpty(diagnostics); } function expectOutput( diff --git a/packages/openapi3/test/test-host.ts b/packages/openapi3/test/test-host.ts index c8ac6191e1e..c238e3695ac 100644 --- a/packages/openapi3/test/test-host.ts +++ b/packages/openapi3/test/test-host.ts @@ -53,7 +53,7 @@ export async function diagnoseOpenApiFor(code: string, options: OpenAPI3EmitterO emit: ["@typespec/openapi3"], options: { "@typespec/openapi3": options as any }, }); - return diagnostics.filter((x) => x.code !== "@typespec/http/no-service-found"); + return diagnostics; } export async function openApiFor( @@ -74,7 +74,7 @@ export async function openApiFor( emit: ["@typespec/openapi3"], options: { "@typespec/openapi3": { ...options, "output-file": outPath } }, }); - expectDiagnosticEmpty(diagnostics.filter((x) => x.code !== "@typespec/http/no-service-found")); + expectDiagnosticEmpty(diagnostics); if (!versions) { return JSON.parse(host.fs.get(resolveVirtualPath("openapi.json"))!);