Skip to content

Commit

Permalink
Fix Bug for OpenAPI 3 Emitter crash on @useAuth({}) (microsoft#4133)
Browse files Browse the repository at this point in the history
OpenAPI 3 Emitter crash on @useAuth({}). 
seems the diagnostics is not initial before add item. 

fix: microsoft#4097

---------

Co-authored-by: Kyle Zhang <[email protected]>
Co-authored-by: Mark Cowlishaw <[email protected]>
Co-authored-by: Timothee Guerin <[email protected]>
Co-authored-by: Christopher Radek <[email protected]>
  • Loading branch information
5 people authored and weidongxu-microsoft committed Sep 3, 2024
1 parent 15f3a5e commit 02ed0ba
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
7 changes: 7 additions & 0 deletions .chronus/changes/CrashOnunsupported-auth-2024-7-13-17-3-33.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: fix
packages:
- "@typespec/openapi3"
---

Fix Bug for OpenAPI 3 Emitter crash on `@useAuth({})`
3 changes: 2 additions & 1 deletion packages/openapi3/src/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ function createOAPIEmitter(
defaultAuth: AuthenticationReference,
version?: string
) {
diagnostics = createDiagnosticCollector();
currentService = service;
metadataInfo = createMetadataInfo(program, {
canonicalVisibility: Visibility.Read,
Expand Down Expand Up @@ -331,7 +332,7 @@ function createOAPIEmitter(
securitySchemes: securitySchemes,
},
};
diagnostics = createDiagnosticCollector();

const servers = getServers(program, service.type);
if (servers) {
root.servers = resolveServers(servers);
Expand Down
16 changes: 15 additions & 1 deletion packages/openapi3/test/security.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { expectDiagnostics } from "@typespec/compiler/testing";
import { deepStrictEqual } from "assert";
import { describe, expect, it } from "vitest";
import { openApiFor } from "./test-host.js";
import { diagnoseOpenApiFor, openApiFor } from "./test-host.js";

describe("openapi3: security", () => {
it("set a basic auth", async () => {
Expand Down Expand Up @@ -246,6 +247,19 @@ describe("openapi3: security", () => {
deepStrictEqual(res.security, [{ OpenIdConnectAuth: [] }]);
});

it("set a unsupported auth", async () => {
const diagnostics = await diagnoseOpenApiFor(
`
@service
@useAuth({})
namespace MyService {}
`
);
expectDiagnostics(diagnostics, {
code: "@typespec/openapi3/unsupported-auth",
});
});

it("can specify custom auth name with description", async () => {
const res = await openApiFor(
`
Expand Down

0 comments on commit 02ed0ba

Please sign in to comment.