Skip to content

Commit

Permalink
Fix issue: OpenAPI 3 Emitter failed to check type of termsOfService i…
Browse files Browse the repository at this point in the history
…n @info (#4483)

[issue 3885](#3885)

---------

Co-authored-by: Kyle Zhang <[email protected]>
Co-authored-by: Timothee Guerin <[email protected]>
  • Loading branch information
3 people authored Sep 27, 2024
1 parent 1a39552 commit ee4172a
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .chronus/changes/termsOfServiceUrlCheck-2024-8-23-12-59-15.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: fix
packages:
- "@typespec/openapi"
---

`@info` decorator validate `termsOfService` is a valid url
19 changes: 19 additions & 0 deletions packages/openapi/src/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,11 @@ export const $info: InfoDecorator = (
if (data === undefined) {
return;
}
if (data.termsOfService) {
if (!validateIsUri(context, data.termsOfService, "TermsOfService")) {
return;
}
}
setInfo(context.program, entity, data);
};

Expand Down Expand Up @@ -214,3 +219,17 @@ export function resolveInfo(program: Program, entity: Namespace): AdditionalInfo
function omitUndefined<T extends Record<string, unknown>>(data: T): T {
return Object.fromEntries(Object.entries(data).filter(([k, v]) => v !== undefined)) as any;
}

function validateIsUri(context: DecoratorContext, url: string, propertyName: string) {
try {
new URL(url);
return true;
} catch {
reportDiagnostic(context.program, {
code: "not-url",
target: context.getArgumentTarget(0)!,
format: { property: propertyName, value: url },
});
return false;
}
}
6 changes: 6 additions & 0 deletions packages/openapi/src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ export const $lib = createTypeSpecLibrary({
parameter: paramMessage`Duplicate parameter key: '${"value"}'. Check @friendlyName decorators and overlap with types in TypeSpec or service namespace.`,
},
},
"not-url": {
severity: "error",
messages: {
default: paramMessage`${"property"}: ${"value"} is not a valid URL.`,
},
},
},
});

Expand Down
12 changes: 12 additions & 0 deletions packages/openapi/test/decorators.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,18 @@ describe("openapi: decorators", () => {
});

describe("@info", () => {
it("emit diagnostic if termsOfService is not a valid url", async () => {
const diagnostics = await runner.diagnose(`
@info({termsOfService:"notvalidurl"})
@test namespace Service {}
`);

expectDiagnostics(diagnostics, {
code: "@typespec/openapi/not-url",
message: "TermsOfService: notvalidurl is not a valid URL.",
});
});

it("emit diagnostic if use on non namespace", async () => {
const diagnostics = await runner.diagnose(`
@info({})
Expand Down

0 comments on commit ee4172a

Please sign in to comment.