Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix x-ms-identifiers rule to support inner properties #1744

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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,7 @@
---
changeKind: fix
packages:
- "@azure-tools/typespec-azure-resource-manager"
---

Fix the `x-ms-identifier` rule. The `x-ms-identifier` supports indexing into inner properties, but the linter does not support that and reports a warning.
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,29 @@ export const missingXmsIdentifiersRule = createRule({
}

if (Array.isArray(xmsIdentifiers)) {
for (const prop of xmsIdentifiers) {
if (typeof prop === "string") {
if (getProperty(elementType, prop) === undefined) {
context.reportDiagnostic({
messageId: "missingProperty",
format: { propertyName: prop, targetModelName: elementType.name },
target: property,
});
for (const propIdentifier of xmsIdentifiers) {
if (typeof propIdentifier === "string") {
const props = propIdentifier.split("/");
let element = elementType;
for (const prop of props) {
const propertyValue = getProperty(element, prop);
if (propertyValue === undefined) {
context.reportDiagnostic({
messageId: "missingProperty",
format: { propertyName: prop, targetModelName: elementType.name },
target: property,
});
}

const propertyType = propertyValue?.type as ArrayModelType;
if (propertyType !== undefined && propertyType.kind === "Model") {
element = propertyType;
}
}
} else {
context.reportDiagnostic({
messageId: "notArray",
format: { valueType: typeof prop },
format: { valueType: typeof propIdentifier },
target: property,
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,29 @@ describe("typespec-azure-core: no-enum rule", () => {
)
.toBeValid();
});

it("allow array of x-ms-identifiers", async () => {
await tester
.expect(
`
model Pet {
@OpenAPI.extension("x-ms-identifiers", ["food/brand/name"])
Copy link
Member

Choose a reason for hiding this comment

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

The doc linked in the issue mentioned that those should start with / in the example.

Not really sure if that's accurate but maybe we should support it or confirm there isn't use case of one or the other

pet: Dog[];
}

model Dog {
food: Food;
}

model Food {
brand: Brand;
}

model Brand {
name: string;
}
`,
)
.toBeValid();
});
});
Loading