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

New Azure specs ruleset package #854

Merged
merged 14 commits into from
May 21, 2024
Merged
1 change: 1 addition & 0 deletions packages/samples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"@azure-tools/typespec-azure-core": "workspace:~",
"@azure-tools/typespec-azure-resource-manager": "workspace:~",
"@azure-tools/typespec-client-generator-core": "workspace:~",
"@azure-tools/typespec-azure-spec-rulesets": "workspace:~",
"@typespec/compiler": "workspace:~",
"@typespec/http": "workspace:~",
"@typespec/openapi": "workspace:~",
Expand Down
2 changes: 1 addition & 1 deletion packages/samples/specs/data-plane/tspconfig.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ emit:
- "@typespec/openapi3"
linter:
extends:
# - "@azure-tools/typespec-azure-core/all"
- "@azure-tools/typespec-azure-specs-rulesets/data-plane"
enable:
"@azure-tools/typespec-azure-core/no-enum": true
1 change: 1 addition & 0 deletions packages/samples/specs/resource-manager/tspconfig.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ options:
linter:
extends:
# - "@azure-tools/typespec-azure-resource-manager/all"
- "@azure-tools/typespec-azure-specs-rulesets/resource-manager"
enable:
"@azure-tools/typespec-azure-core/no-enum": true
21 changes: 21 additions & 0 deletions packages/typespec-azure-specs-rulesets/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) Microsoft Corporation. All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE
173 changes: 173 additions & 0 deletions packages/typespec-azure-specs-rulesets/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
# @azure-tools/typespec-autorest

TypeSpec library for emitting openapi from the TypeSpec REST protocol binding

## Install

```bash
npm install @azure-tools/typespec-autorest
```

## Emitter

### Usage

1. Via the command line

```bash
tsp compile . --emit=@azure-tools/typespec-autorest
```

2. Via the config

```yaml
emit:
- "@azure-tools/typespec-autorest"
```

### Emitter options

#### `output-dir`

**Type:** `string`

Deprecated DO NOT USE. Use built-in emitter-output-dir instead

#### `output-file`

**Type:** `string`

Name of the output file.
Output file will interpolate the following values:

- service-name: Name of the service if multiple
- version: Version of the service if multiple
- azure-resource-provider-folder: Value of the azure-resource-provider-folder option
- version-status: Only enabled if azure-resource-provider-folder is set. `preview` if version contains preview, stable otherwise.

Default: `{azure-resource-provider-folder}/{service-name}/{version-status}/{version}/openapi.json`

Example: Single service no versioning

- `openapi.yaml`

Example: Multiple services no versioning

- `openapi.Org1.Service1.yaml`
- `openapi.Org1.Service2.yaml`

Example: Single service with versioning

- `openapi.v1.yaml`
- `openapi.v2.yaml`

Example: Multiple service with versioning

- `openapi.Org1.Service1.v1.yaml`
- `openapi.Org1.Service1.v2.yaml`
- `openapi.Org1.Service2.v1.0.yaml`
- `openapi.Org1.Service2.v1.1.yaml`

Example: azureResourceProviderFolder is provided

- `arm-folder/AzureService/preview/2020-01-01.yaml`
- `arm-folder/AzureService/preview/2020-01-01.yaml`

#### `examples-directory`

**Type:** `string`

Directory where the examples are located. Default: `{cwd}/examples`.

#### `version`

**Type:** `string`

#### `azure-resource-provider-folder`

**Type:** `string`

#### `arm-types-dir`

**Type:** `string`

Path to the common-types.json file folder. Default: '${project-root}/../../common-types/resource-management'

#### `new-line`

**Type:** `"crlf" | "lf"`

Set the newline character for emitting files.

#### `omit-unreachable-types`

**Type:** `boolean`

Omit unreachable types. By default all types declared under the service namespace will be included. With this flag on only types references in an operation will be emitted.

#### `version-enum-strategy`

**Type:** `string`

Decide how to deal with the Version enum when when `omit-unreachable-types` is not set. Default to 'omit'

#### `include-x-typespec-name`

**Type:** `"inline-only" | "never"`

If the generated openapi types should have the `x-typespec-name` extension set with the name of the TypeSpec type that created it.
This extension is meant for debugging and should not be depended on.

#### `use-read-only-status-schema`

**Type:** `boolean`

Create read-only property schema for lro status

## Decorators

### Autorest

- [`@example`](#@example)
- [`@useRef`](#@useref)

#### `@example`

`@example` - attaches example files to an operation. Multiple examples can be specified.

`@example` can be specified on Operations.

```typespec
@Autorest.example(pathOrUri: valueof string, title: valueof string)
```

##### Target

`Operation`

##### Parameters

| Name | Type | Description |
| --------- | ---------------- | ---------------------------------------- |
| pathOrUri | `valueof string` | path or Uri to the example file. |
| title | `valueof string` | name or description of the example file. |

#### `@useRef`

`@useRef` - is used to replace the TypeSpec model type in emitter output with a pre-existing named OpenAPI schema such as Azure Resource Manager common types.

`@useRef` can be specified on Models and ModelProperty.

```typespec
@Autorest.useRef(jsonRef: valueof string)
```

##### Target

`Model | ModelProperty`

##### Parameters

| Name | Type | Description |
| ------- | ---------------- | --------------------------------- |
| jsonRef | `valueof string` | path or Uri to an OpenAPI schema. |
65 changes: 65 additions & 0 deletions packages/typespec-azure-specs-rulesets/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
{
"name": "@azure-tools/typespec-azure-specs-rulesets",
"version": "0.42.0",
"author": "Microsoft Corporation",
"description": "TypeSpec ruleset for azure specs",
"homepage": "https://azure.github.io/typespec-azure",
"readme": "https://github.com/Azure/typespec-azure/blob/main/packages/typespec-azure-specs-rulesets/README.md",
"license": "MIT",
"repository": {
"type": "git",
"url": "git+https://github.com/Azure/typespec-azure.git"
},
"bugs": {
"url": "https://github.com/Azure/typespec-azure/issues"
},
"keywords": [
"typespec",
"autorest"
],
"type": "module",
"main": "dist/src/index.js",
"exports": {
".": "./dist/src/index.js"
},
"engines": {
"node": ">=18.0.0"
},
"scripts": {
"clean": "rimraf ./dist ./temp",
"build": "tsc -p .",
"watch": "tsc -p . --watch",
"test": "vitest run",
"test:watch": "vitest -w",
"test:ui": "vitest --ui",
"test:ci": "vitest run --coverage --reporter=junit --reporter=default",
"lint": "eslint . --max-warnings=0",
"lint:fix": "eslint . --fix ",
"regen-docs": "tspd doc . --enable-experimental --output-dir ../../docs/emitters/typespec-azure-specs-rulesets/reference"
},
"files": [
"dist/**",
"!dist/test/**"
],
"peerDependencies": {
"@azure-tools/typespec-azure-core": "workspace:~",
"@azure-tools/typespec-azure-resource-manager": "workspace:~",
"@azure-tools/typespec-client-generator-core": "workspace:~",
"@typespec/compiler": "workspace:~"
},
"devDependencies": {
"@azure-tools/typespec-azure-core": "workspace:~",
"@azure-tools/typespec-azure-resource-manager": "workspace:~",
"@azure-tools/typespec-client-generator-core": "workspace:~",
"@types/node": "~18.11.19",
"@typespec/compiler": "workspace:~",
"@typespec/tspd": "workspace:~",
"@vitest/coverage-v8": "^1.5.0",
"@vitest/ui": "^1.5.0",
"c8": "^9.1.0",
"change-case": "~5.4.4",
"rimraf": "~5.0.5",
"typescript": "~5.4.5",
"vitest": "^1.5.0"
}
}
16 changes: 16 additions & 0 deletions packages/typespec-azure-specs-rulesets/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { createTypeSpecLibrary, defineLinter } from "@typespec/compiler";
import dataPlaneRuleset from "./rulesets/data-plane.js";
import resourceManagerRuleset from "./rulesets/resource-manager.js";

export const $lib = createTypeSpecLibrary({
name: "@azure-tools/typespec-azure-specs-rulesets",
diagnostics: {},
});

export const $linter = defineLinter({
rules: [],
ruleSets: {
"data-plane": dataPlaneRuleset,
"resource-manager": resourceManagerRuleset,
},
});
42 changes: 42 additions & 0 deletions packages/typespec-azure-specs-rulesets/src/rulesets/data-plane.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import type { LinterRuleSet } from "@typespec/compiler";

export default {
enable: {
"@azure-tools/typespec-azure-core/operation-missing-api-version": true,
"@azure-tools/typespec-azure-core/auth-required": true,
"@azure-tools/typespec-azure-core/request-body-problem": true,
"@azure-tools/typespec-azure-core/byos": true,
"@azure-tools/typespec-azure-core/casing-style": true,
"@azure-tools/typespec-azure-core/composition-over-inheritance": true,
"@azure-tools/typespec-azure-core/use-extensible-enum": true,
"@azure-tools/typespec-azure-core/known-encoding": true,
"@azure-tools/typespec-azure-core/long-running-polling-operation-required": true,
"@azure-tools/typespec-azure-core/no-closed-literal-union": true,
"@azure-tools/typespec-azure-core/no-enum": true,
"@azure-tools/typespec-azure-core/no-error-status-codes": true,
"@azure-tools/typespec-azure-core/no-explicit-routes-resource-ops": true,
"@azure-tools/typespec-azure-core/no-fixed-enum-discriminator": true,
"@azure-tools/typespec-azure-core/non-breaking-versioning": true,
"@azure-tools/typespec-azure-core/no-generic-numeric": true,
"@azure-tools/typespec-azure-core/no-nullable": true,
"@azure-tools/typespec-azure-core/no-offsetdatetime": true,
"@azure-tools/typespec-azure-core/no-response-body": true,
"@azure-tools/typespec-azure-core/no-rpc-path-params": true,
"@azure-tools/typespec-azure-core/no-operation-id": true,
"@azure-tools/typespec-azure-core/prefer-csv-collection-format": true,
"@azure-tools/typespec-azure-core/no-format": true,
"@azure-tools/typespec-azure-core/no-multiple-discriminator": true,
"@azure-tools/typespec-azure-core/no-rest-library-interfaces": true,
"@azure-tools/typespec-azure-core/no-unknown": true,
"@azure-tools/typespec-azure-core/property-name-conflict": true,
"@azure-tools/typespec-azure-core/bad-record-type": true,
"@azure-tools/typespec-azure-core/documentation-required": true,
"@azure-tools/typespec-azure-core/key-visibility-required": true,
"@azure-tools/typespec-azure-core/response-schema-problem": true,
"@azure-tools/typespec-azure-core/rpc-operation-request-body": true,
"@azure-tools/typespec-azure-core/spread-discriminated-model": true,
"@azure-tools/typespec-azure-core/use-standard-names": true,
"@azure-tools/typespec-azure-core/use-standard-operations": true,
"@azure-tools/typespec-azure-core/no-string-discriminator": true,
},
} satisfies LinterRuleSet;
Loading
Loading