Skip to content

Commit

Permalink
No matching versions found (#1838)
Browse files Browse the repository at this point in the history
issue: #1647
  • Loading branch information
AlitzelMendez authored Nov 15, 2024
1 parent bcbdcf6 commit a411cab
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: fix
packages:
- "@azure-tools/typespec-autorest"
---

Emit error `@azure-tools/typespec-autorest/no-matching-version-found` when the version option is used and does not match any versions of the service.
6 changes: 5 additions & 1 deletion packages/typespec-autorest/src/emit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
} from "@typespec/compiler";
import { resolveInfo } from "@typespec/openapi";
import { buildVersionProjections } from "@typespec/versioning";
import { AutorestEmitterOptions, getTracer } from "./lib.js";
import { AutorestEmitterOptions, getTracer, reportDiagnostic } from "./lib.js";
import {
AutorestDocumentEmitterOptions,
getOpenAPIForService,
Expand Down Expand Up @@ -128,6 +128,10 @@ export async function getAllServicesAtAllVersions(
(v) => !options.version || options.version === v.version,
);

if (versions.length === 0) {
reportDiagnostic(program, { code: "no-matching-version-found", target: service.type });
}

if (versions.length === 1 && versions[0].version === undefined) {
let projectedProgram;
if (versions[0].projections.length > 0) {
Expand Down
7 changes: 7 additions & 0 deletions packages/typespec-autorest/src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,13 @@ export const $lib = createTypeSpecLibrary({
default: paramMessage`Authentication "${"authType"}" is not a known authentication by the openapi3 emitter, it will be ignored.`,
},
},
"no-matching-version-found": {
severity: "error",
messages: {
default:
"The emitter did not emit any files because the specified version option does not match any versions of the service.",
},
},
},
emitter: {
options: EmitterOptionsSchema as JSONSchemaType<AutorestEmitterOptions>,
Expand Down
28 changes: 27 additions & 1 deletion packages/typespec-autorest/test/versioning.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { expectDiagnostics } from "@typespec/compiler/testing";
import { deepStrictEqual, strictEqual } from "assert";
import { describe, expect, it } from "vitest";
import { OpenAPI2Document } from "../src/openapi2-document.js";
import { openApiFor } from "./test-host.js";
import { diagnoseOpenApiFor, openApiFor } from "./test-host.js";

describe("typespec-autorest: versioning", () => {
it("if version enum is referenced only include current member and mark it with modelAsString: true", async () => {
Expand Down Expand Up @@ -269,4 +270,29 @@ describe("typespec-autorest: versioning", () => {
required: ["jsonProp1", "jsonProp2", "prop3"],
});
});

it("emit diagnostics when version option is used an doesn't match the versions of the service", async () => {
const diagnostics = await diagnoseOpenApiFor(
`
@versioned(Versions)
@service
namespace DemoService;
enum Versions {
v1,
v2,
}
model A {}
`,
{ version: "v3" },
);
expectDiagnostics(diagnostics, [
{
code: "@azure-tools/typespec-autorest/no-matching-version-found",
message:
"The emitter did not emit any files because the specified version option does not match any versions of the service.",
},
]);
});
});

0 comments on commit a411cab

Please sign in to comment.