Skip to content
Open
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,8 @@
---
# Change versionKind to one of: internal, fix, dependencies, feature, deprecation, breaking
changeKind: fix
packages:
- "@typespec/openapi"
---

Fix tagMetadata extension diagnostic targets
4 changes: 2 additions & 2 deletions packages/openapi/src/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ export const tagMetadataDecorator: TagMetadataDecorator = (
if (
!validateAdditionalInfoModel(
context.program,
context.getArgumentTarget(0)!,
context.getArgumentTarget(1)!,
tagMetadata,
"TypeSpec.OpenAPI.TagMetadata",
)
Expand All @@ -334,7 +334,7 @@ export const tagMetadataDecorator: TagMetadataDecorator = (
if (
!validateIsUri(
context.program,
context.getArgumentTarget(0)!,
context.getArgumentTarget(1)!,
tagMetadata.externalDocs.url,
"externalDocs.url",
)
Expand Down
40 changes: 37 additions & 3 deletions packages/openapi/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ import {
Type,
TypeNameOptions,
} from "@typespec/compiler";
import {
SyntaxKind,
type ObjectLiteralNode,
type ObjectLiteralPropertyNode,
} from "@typespec/compiler/ast";
import { getOperationId } from "./decorators.js";
import { createDiagnostic, reportDiagnostic } from "./lib.js";
import { ExtensionKey } from "./types.js";
Expand Down Expand Up @@ -253,17 +258,30 @@ function checkNoAdditionalProperties(
jsonObject: any,
target: DiagnosticTarget,
source: Model,
): Diagnostic[] {
const targetNode = getObjectLiteralNode(target);
return checkNoAdditionalPropertiesInternal(jsonObject, target, source, targetNode);
}

function checkNoAdditionalPropertiesInternal(
jsonObject: any,
target: DiagnosticTarget,
source: Model,
targetNode: ObjectLiteralNode | undefined,
): Diagnostic[] {
const diagnostics: Diagnostic[] = [];

for (const name of Object.keys(jsonObject)) {
const sourceProperty = getProperty(source, name);
const propertyNode = getObjectLiteralProperty(targetNode, name);
if (sourceProperty) {
if (sourceProperty.type.kind === "Model") {
const nestedDiagnostics = checkNoAdditionalProperties(
const nestedTarget = getObjectLiteralNode(propertyNode?.value);
const nestedDiagnostics = checkNoAdditionalPropertiesInternal(
jsonObject[name],
target,
propertyNode?.value ?? target,
sourceProperty.type,
nestedTarget,
);
diagnostics.push(...nestedDiagnostics);
}
Expand All @@ -272,11 +290,27 @@ function checkNoAdditionalProperties(
createDiagnostic({
code: "invalid-extension-key",
format: { value: name },
target,
target: propertyNode?.id ?? target,
}),
);
}
}

return diagnostics;
}

function getObjectLiteralNode(target: DiagnosticTarget | undefined): ObjectLiteralNode | undefined {
return target !== undefined && "kind" in target && target.kind === SyntaxKind.ObjectLiteral
? target
: undefined;
}

function getObjectLiteralProperty(
node: ObjectLiteralNode | undefined,
name: string,
): ObjectLiteralPropertyNode | undefined {
return node?.properties.find(
(property): property is ObjectLiteralPropertyNode =>
property.kind === SyntaxKind.ObjectLiteralProperty && property.id.sv === name,
);
}
14 changes: 14 additions & 0 deletions packages/openapi/test/decorators.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,20 @@ describe("openapi: decorators", () => {
});

describe("emit diagnostics when passing extension key not starting with `x-` in metadata", () => {
it("reports the diagnostic on the invalid metadata property", async () => {
const [{ pos }, diagnostics] = await Tester.compileAndDiagnose(`
@service
@tagMetadata("tagName", #{ /*custom*/custom: "Bar" })
namespace PetStore{};
`);

expectDiagnostics(diagnostics, {
code: "@typespec/openapi/invalid-extension-key",
message: `OpenAPI extension must start with 'x-' but was 'custom'`,
pos: pos.custom.pos,
});
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

expectDiagnostics takes in a pos and the actual value can be retrieved if using compileAndDiagnose and using a marker in the code

});

it.each([
["root", `#{ foo:"Bar" }`],
["externalDocs", `#{ externalDocs: #{ url: "https://example.com", foo:"Bar"} }`],
Expand Down