-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add rule for RPC-Policy-V1-03: no properties of type object without a…
… schema (#1555) Issue: #1426 --------- Co-authored-by: Timothee Guerin <timothee.guerin@outlook.com>
- Loading branch information
1 parent
2e65ae9
commit 22aa5ee
Showing
9 changed files
with
195 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
changeKind: feature | ||
packages: | ||
- "@azure-tools/typespec-azure-resource-manager" | ||
--- | ||
|
||
Add `no-empty-model` rule |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
changeKind: feature | ||
packages: | ||
- "@azure-tools/typespec-azure-rulesets" | ||
--- | ||
|
||
Add `no-empty-model` rule to ruleset |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
docs/libraries/azure-resource-manager/rules/no-empty-model.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
--- | ||
title: no-empty-model | ||
--- | ||
|
||
```text title=- Full name- | ||
@azure-tools/typespec-azure-resource-manager/no-empty-model | ||
``` | ||
|
||
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: {}; | ||
} | ||
``` | ||
|
||
#### ❌ Incorrect | ||
|
||
```tsp | ||
model Empty {} | ||
``` | ||
|
||
#### ✅ 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; | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
packages/typespec-azure-resource-manager/src/rules/no-empty-model.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { createRule, Model } from "@typespec/compiler"; | ||
|
||
export const noEmptyModel = createRule({ | ||
name: "no-empty-model", | ||
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.", | ||
url: "https://azure.github.io/typespec-azure/docs/libraries/azure-resource-manager/rules/no-empty-model", | ||
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) { | ||
return { | ||
model: (model: Model) => { | ||
if (model.properties.size === 0) { | ||
context.reportDiagnostic({ | ||
code: "no-empty-model", | ||
target: model, | ||
}); | ||
} | ||
}, | ||
}; | ||
}, | ||
}); |
99 changes: 99 additions & 0 deletions
99
packages/typespec-azure-resource-manager/test/rules/no-empty-model.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import { | ||
BasicTestRunner, | ||
LinterRuleTester, | ||
createLinterRuleTester, | ||
} from "@typespec/compiler/testing"; | ||
import { beforeEach, it } from "vitest"; | ||
import { noEmptyModel } from "../../src/rules/no-empty-model.js"; | ||
import { createAzureResourceManagerTestRunner } from "../test-host.js"; | ||
|
||
const armDef = ` | ||
@armProviderNamespace | ||
@useDependency(Azure.ResourceManager.Versions.v1_0_Preview_1) | ||
namespace Microsoft.Contoso; | ||
`; | ||
|
||
let runner: BasicTestRunner; | ||
let tester: LinterRuleTester; | ||
|
||
beforeEach(async () => { | ||
runner = await createAzureResourceManagerTestRunner(); | ||
tester = createLinterRuleTester( | ||
runner, | ||
noEmptyModel, | ||
"@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/no-empty-model", | ||
message: "Properties with type:object must have definition of a reference model.", | ||
}); | ||
}); | ||
|
||
it("emits diagnostic when model type:object is not defined", async () => { | ||
await tester | ||
.expect( | ||
` | ||
${armDef} | ||
model Foo { } | ||
`, | ||
) | ||
.toEmitDiagnostics({ | ||
code: "@azure-tools/typespec-azure-resource-manager/no-empty-model", | ||
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(); | ||
}); | ||
|
||
it("valid when a property use a simple data type ", async () => { | ||
await tester | ||
.expect( | ||
` | ||
${armDef} | ||
model WidgetProperties { | ||
Name: string; | ||
} | ||
`, | ||
) | ||
.toBeValid(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters