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

Add rule for RPC-Policy-V1-03: no properties of type object without a schema #1555

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
7 changes: 7 additions & 0 deletions .chronus/changes/almend-Rule-2024-8-17-16-58-23.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: feature
packages:
- "@azure-tools/typespec-azure-resource-manager"
---

Add arm-properties-type-object-no-definition rule
AlitzelMendez marked this conversation as resolved.
Show resolved Hide resolved
7 changes: 7 additions & 0 deletions .chronus/changes/almend-Rule-2024-8-18-10-2-28.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: feature
packages:
- "@azure-tools/typespec-azure-rulesets"
---

Add arm-properties-type-object-no-definition rule to ruleset
AlitzelMendez marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Available ruleSets:
| [`@azure-tools/typespec-azure-resource-manager/arm-delete-operation-response-codes`](/libraries/azure-resource-manager/rules/delete-operation-response-codes.md) | Ensure delete operations have the appropriate status codes. |
| [`@azure-tools/typespec-azure-resource-manager/arm-put-operation-response-codes`](/libraries/azure-resource-manager/rules/put-operation-response-codes.md) | Ensure put operations have the appropriate status codes. |
| [`@azure-tools/typespec-azure-resource-manager/arm-post-operation-response-codes`](/libraries/azure-resource-manager/rules/post-operation-response-codes.md) | Ensure post operations have the appropriate status codes. |
| `@azure-tools/typespec-azure-resource-manager/arm-properties-type-object-no-definition` | ARM Properties with type:object that don't reference a model definition are not allowed. ARM doesn't allow generic type definitions as this leads to bad customer experience. |
| `@azure-tools/typespec-azure-resource-manager/arm-resource-action-no-segment` | `@armResourceAction` should not be used with `@segment`. |
| `@azure-tools/typespec-azure-resource-manager/arm-resource-duplicate-property` | Warn about duplicate properties in resources. |
| `@azure-tools/typespec-azure-resource-manager/arm-resource-invalid-envelope-property` | Check for invalid resource envelope properties. |
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
---
title: arm-properties-type-object-no-definition
AlitzelMendez marked this conversation as resolved.
Show resolved Hide resolved
---

```text title=- Full name-
@azure-tools/typespec-azure-resource-manager/arm-properties-type-object-no-definition
```

ARM Properties with type:object that don't reference a model definition are not allowed. ARM doesn't allow generic type definitions as this leads to bad customer experience.

#### ❌ Incorrect

```tsp
model Information {
address: {};
}
AlitzelMendez marked this conversation as resolved.
Show resolved Hide resolved
```

#### ✅ Correct

```tsp
model Information {
address: Address;
}

model Address {
street: string;
city: string;
state: string;
country: string;
postalCode: string;
}
```

#### ✅ Correct

```tsp
model Information {
street: string;
city: string;
state: string;
country: string;
postalCode: string;
}
```
1 change: 1 addition & 0 deletions packages/typespec-azure-resource-manager/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Available ruleSets:
| [`@azure-tools/typespec-azure-resource-manager/arm-delete-operation-response-codes`](https://azure.github.io/typespec-azure/docs/libraries/azure-resource-manager/rules/delete-operation-response-codes) | Ensure delete operations have the appropriate status codes. |
| [`@azure-tools/typespec-azure-resource-manager/arm-put-operation-response-codes`](https://azure.github.io/typespec-azure/docs/libraries/azure-resource-manager/rules/put-operation-response-codes) | Ensure put operations have the appropriate status codes. |
| [`@azure-tools/typespec-azure-resource-manager/arm-post-operation-response-codes`](https://azure.github.io/typespec-azure/docs/libraries/azure-resource-manager/rules/post-operation-response-codes) | Ensure post operations have the appropriate status codes. |
| `@azure-tools/typespec-azure-resource-manager/arm-properties-type-object-no-definition` | ARM Properties with type:object that don't reference a model definition are not allowed. ARM doesn't allow generic type definitions as this leads to bad customer experience. |
| `@azure-tools/typespec-azure-resource-manager/arm-resource-action-no-segment` | `@armResourceAction` should not be used with `@segment`. |
| `@azure-tools/typespec-azure-resource-manager/arm-resource-duplicate-property` | Warn about duplicate properties in resources. |
| `@azure-tools/typespec-azure-resource-manager/arm-resource-invalid-envelope-property` | Check for invalid resource envelope properties. |
Expand Down
2 changes: 2 additions & 0 deletions packages/typespec-azure-resource-manager/src/linter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { armCommonTypesVersionRule } from "./rules/arm-common-types-version.js";
import { armDeleteResponseCodesRule } from "./rules/arm-delete-response-codes.js";
import { armNoRecordRule } from "./rules/arm-no-record.js";
import { armPostResponseCodesRule } from "./rules/arm-post-response-codes.js";
import { armPropertiesTypeObjectNoDefinitionRule } from "./rules/arm-properties-type-object-no-definition.js";
import { armPutResponseCodesRule } from "./rules/arm-put-response-codes.js";
import { armResourceActionNoSegmentRule } from "./rules/arm-resource-action-no-segment.js";
import { armResourceDuplicatePropertiesRule } from "./rules/arm-resource-duplicate-property.js";
Expand Down Expand Up @@ -36,6 +37,7 @@ const rules = [
armDeleteResponseCodesRule,
armPutResponseCodesRule,
armPostResponseCodesRule,
armPropertiesTypeObjectNoDefinitionRule,
armResourceActionNoSegmentRule,
armResourceDuplicatePropertiesRule,
armResourceEnvelopeProperties,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { createRule, Model, ModelProperty, SemanticNodeListener } from "@typespec/compiler";

export const armPropertiesTypeObjectNoDefinitionRule = createRule({
name: "arm-properties-type-object-no-definition",
severity: "warning",
description:
"ARM Properties with type:object that don't reference a model definition are not allowed. ARM doesn't allow generic type definitions as this leads to bad customer experience.",
AlitzelMendez marked this conversation as resolved.
Show resolved Hide resolved
messages: {
default: "Properties with type:object must have definition of a reference model.",
extends:
"The `type:object` model is not defined in the payload. Define the reference model of the object or change the `type` to a primitive data type like string, int, etc",
},
create(context): SemanticNodeListener {
return {
model: (model: Model) => {
for (const property of getProperties(model)) {
if (property.type.kind === "Model" && !property.type.name) {
context.reportDiagnostic({
code: "arm-no-record",
AlitzelMendez marked this conversation as resolved.
Show resolved Hide resolved
target: model,
});
}
}
},
};

function getProperties(model: Model): ModelProperty[] {
const result: ModelProperty[] = [];
let current: Model | undefined = model;
while (current !== undefined) {
if (current.properties.size > 0) result.push(...current.properties.values());
current = current.baseModel;
}
return result;
}
},
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import {
BasicTestRunner,
LinterRuleTester,
createLinterRuleTester,
} from "@typespec/compiler/testing";
import { beforeEach, describe, it } from "vitest";
import { armPropertiesTypeObjectNoDefinitionRule } from "../../src/rules/arm-properties-type-object-no-definition.js";
import { createAzureResourceManagerTestRunner } from "../test-host.js";

const armDef = `
@armProviderNamespace
@useDependency(Azure.ResourceManager.Versions.v1_0_Preview_1)
namespace Microsoft.Contoso;
`;

describe("typespec-azure-resource-manager: arm properties type-object no definition rule", () => {
let runner: BasicTestRunner;
let tester: LinterRuleTester;

beforeEach(async () => {
runner = await createAzureResourceManagerTestRunner();
tester = createLinterRuleTester(
runner,
armPropertiesTypeObjectNoDefinitionRule,
"@azure-tools/typespec-azure-resource-manager",
);
});

it("emits diagnostic when a property use type:object that is not defined", async () => {
await tester
.expect(
`
${armDef}
model Foo {
props: {};
}
`,
)
.toEmitDiagnostics({
code: "@azure-tools/typespec-azure-resource-manager/arm-properties-type-object-no-definition",
message: "Properties with type:object must have definition of a reference model.",
});
});

it("valid when a property use type:object that is defined", async () => {
await tester
.expect(
`
${armDef}
model WidgetProperties {
props: Foo;
}

model Foo{
Name: string;
}
`,
)
.toBeValid();
});

it("valid when a property use type:object known scalar", async () => {
await tester
.expect(
`
${armDef}
model WidgetProperties {
Date: utcDateTime;
}
`,
)
.toBeValid();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export default {

// Azure resource manager
"@azure-tools/typespec-azure-resource-manager/arm-no-record": true,
"@azure-tools/typespec-azure-resource-manager/arm-properties-type-object-no-definition": true,
"@azure-tools/typespec-azure-resource-manager/arm-common-types-version": true,
"@azure-tools/typespec-azure-resource-manager/arm-delete-operation-response-codes": true,
"@azure-tools/typespec-azure-resource-manager/arm-put-operation-response-codes": true,
Expand Down
Loading