Skip to content

Commit 42ae8ee

Browse files
authored
Migrating Csharp validator from AutoRest (#22)
Migrating Csharp validator from AutoRest
1 parent bead41c commit 42ae8ee

File tree

318 files changed

+27438
-130
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

318 files changed

+27438
-130
lines changed

.gitattributes

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
###############################################################################
2+
# Set default behavior to automatically normalize line endings.
3+
###############################################################################
4+
* text=auto
5+
6+
###############################################################################
7+
# Set default behavior for command prompt diff.
8+
#
9+
# This is need for earlier builds of msysgit that does not have it on by
10+
# default for csharp files.
11+
# Note: This is only used by command line
12+
###############################################################################
13+
#*.cs diff=csharp
14+
15+
###############################################################################
16+
# Set the merge driver for project and solution files
17+
#
18+
# Merging from the command prompt will add diff markers to the files if there
19+
# are conflicts (Merging from VS is not affected by the settings below, in VS
20+
# the diff markers are never inserted). Diff markers may cause the following
21+
# file extensions to fail to load in VS. An alternative would be to treat
22+
# these files as binary and thus will always conflict and require user
23+
# intervention with every merge. To do so, just uncomment the entries below
24+
###############################################################################
25+
#*.sln merge=binary
26+
#*.csproj merge=binary
27+
#*.vbproj merge=binary
28+
#*.vcxproj merge=binary
29+
#*.vcproj merge=binary
30+
#*.dbproj merge=binary
31+
#*.fsproj merge=binary
32+
#*.lsproj merge=binary
33+
#*.wixproj merge=binary
34+
#*.modelproj merge=binary
35+
#*.sqlproj merge=binary
36+
#*.wwaproj merge=binary
37+
38+
###############################################################################
39+
# behavior for image files
40+
#
41+
# image files are treated as binary by default.
42+
###############################################################################
43+
#*.jpg binary
44+
#*.png binary
45+
#*.gif binary
46+
47+
###############################################################################
48+
# diff behavior for common document formats
49+
#
50+
# Convert binary document formats to text before diffing them. This feature
51+
# is only available from the command line. Turn it on by uncommenting the
52+
# entries below.
53+
###############################################################################
54+
#*.doc diff=astextplain
55+
#*.DOC diff=astextplain
56+
#*.docx diff=astextplain
57+
#*.DOCX diff=astextplain
58+
#*.dot diff=astextplain
59+
#*.DOT diff=astextplain
60+
#*.pdf diff=astextplain
61+
#*.PDF diff=astextplain
62+
#*.rtf diff=astextplain
63+
#*.RTF diff=astextplain

.gitignore

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,14 @@ src/**/*.js
33
src/**/*.d.ts
44
node_modules/
55
pack/
6-
*.tgz
6+
src/**/*.tgz
7+
8+
# remove dotnet artifacts
9+
src/dotnet/**/[Bb]in/
10+
src/dotnet/**/[Oo]bj/
11+
src/dotnet/**/*.deps.json
12+
src/dotnet/**/[Dd]ebug*/
13+
src/dotnet/**/[Rr]elease/
14+
**/*.DotSettings.user
15+
# ignore vs
16+
**/.vs/

.npmignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
node_modules/
22
# Any logs
3-
.log
3+
.log

.travis.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,13 @@ dist: trusty
33
language: node_js
44
node_js:
55
- "6.9.5"
6+
install:
7+
# node packages
8+
- npm install
9+
# dotnet
10+
- sudo sh -c 'echo "deb [arch=amd64] https://apt-mo.trafficmanager.net/repos/dotnet/ trusty main" > /etc/apt/sources.list.d/dotnetdev.list'
11+
- sudo apt-key adv --keyserver apt-mo.trafficmanager.net --recv-keys 417A0893
12+
- sudo apt-get update
13+
- sudo apt-get install dotnet-dev-1.0.0-rc4-004769 -y
614
script:
715
- gulp

.vscode/launch.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"cwd": "${workspaceRoot}",
99
"program": "${workspaceRoot}/node_modules/mocha/bin/mocha",
1010
"args": [
11-
"./src/azure-openapi-validator/tests/*.js",
11+
"./src/typescript/azure-openapi-validator/tests/*.js",
1212
"--colors"
1313
],
1414
"outFiles": [],

README.md

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,40 @@ This project has adopted the [Microsoft Open Source Code of Conduct](https://ope
1010
## Build scripts
1111
### How to build
1212
The first step would be to run ```npm install``` so we have all the required modules installed.
13-
13+
#### How to build the whole repo
1414
```
1515
gulp build
1616
```
17-
This transpiles the typescript scripts into javascript files under the js folder.
17+
#### How to build the typescript repo
18+
```
19+
gulp build/typescript
20+
```
21+
#### How to build the dotnet repo
22+
To restore the dotnet packages
23+
```
24+
gulp restore/dotnet
25+
```
26+
This builds the dotnet projects.
27+
```
28+
gulp build/dotnet
29+
```
1830
### How to test
31+
To run all tests under the repo
1932
```
2033
gulp test
2134
```
22-
This runs the tests under tests directory.
23-
24-
### How to write a new validation rule
35+
#### How to run the typescript tests
36+
```
37+
gulp test/typescript
38+
```
39+
#### How to run the dotnet tests
40+
```
41+
gulp test/dotnet
42+
```
43+
### How to write a new validation rule using typescript
2544
1. Add a typescript file under ```azure-openapi-validator``` directory named same as the name of the rule. Add the ```id```, ```name```, ```severity```, ```category```, ```mergeState```, ```openapiType```, ```appliesTo_JsonQuery``` properties to the rule. ```appliesTo_JsonQuery``` is the node(s) to which the rule needs to be applied. This is evaluated using JsonPaths. Please refer [here](https://www.npmjs.com/package/jsonpath#jsonpath-syntax) for a brief tutorial about JsonPaths.
26-
2. Next, implement the ```run``` method under the rule that actually does the validation. Add a reference to this script file under ```src\azure-openapi-validator\index.ts```.
27-
3. Lastly add a test case for the validation rule, by adding a test json/yaml under ```src\azure-openapi-validator\tests\resources``` and a script under ```src\azure-openapi-validator\tests``` depending on the type of the validation rule.
45+
2. Next, implement the ```run``` method under the rule that actually does the validation. Add a reference to this script file under ```src/typescript/azure-openapi-validator/index.ts```.
46+
3. Lastly add a test case for the validation rule, by adding a test json/yaml under ```src/typescript/azure-openapi-validator/tests/resources``` and a script under ```src/typescript/azure-openapi-validator/tests``` depending on the type of the validation rule.
2847

29-
## Publishing the package
30-
Npm packages **MUST** be published from the `release` branch, `master` is promoted to release on a monthly cadence. All development **MUST** be done on the `master` branch. The package version of `master` branch **MUST** be incremented after every promotion to release.
48+
### How to publish
3149

docs/anonymous-parameter-types.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# AnonymousParameterTypes
2+
## Description
3+
This rule appears when you define a model type inline, rather than in the `definitions` section. Since code generation does not have a name to call your model, the class will have a non-descriptive name. Additionally, if it represents the same type as another parameter in a different operation, then it becomes impossible to reuse that same class for both operations.
4+
## How to fix
5+
Move the schema to the `definitions` section and reference it using `$ref`.
6+
## Effect on generated code
7+
### Before
8+
#### Spec
9+
```json5
10+
11+
"parameters": [
12+
{
13+
"name": "foo",
14+
"in": "body",
15+
"schema": {
16+
"type": "object",
17+
"properties": {
18+
19+
}
20+
}
21+
}
22+
]
23+
24+
```
25+
#### Generated code
26+
```csharp
27+
public class FooParameter1 {
28+
29+
}
30+
```
31+
### After
32+
#### Spec
33+
```json5
34+
35+
"parameters": [
36+
{
37+
"name": "foo",
38+
"in": "body",
39+
"schema": {
40+
"$ref": "#/definition/FooCreationSettings"
41+
}
42+
}
43+
],
44+
45+
"definitions": {
46+
"FooCreationSettings": {
47+
"type": "object",
48+
"properties": {
49+
50+
}
51+
}
52+
}
53+
54+
```
55+
#### Generated code
56+
```csharp
57+
public class FooCreationSettings {
58+
59+
}
60+
```

docs/avoid-nested-properties.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# AvoidNestedProperties
2+
## Description
3+
This rule appears when you define a property with the name `properties`, and do not use the [`x-ms-client-flatten` extension](../../extensions/readme.md#x-ms-client-flatten). Users often provide feedback that they don't want to create multiple levels of properties to be able to use an operation. By applying the `x-ms-client-flatten` extension, you move the inner `properties` to the top level of your definition.
4+
## How to fix
5+
Add the `x-ms-client-flatten` extension to the inner `properties` definition.
6+
## Effect on generated code
7+
### Before
8+
#### Spec
9+
```json5
10+
11+
"definitions": {
12+
"Foo": {
13+
"type": "object",
14+
"properties": {
15+
"properties": {
16+
"type": "object",
17+
"properties": {
18+
"bar": {
19+
"type": "string"
20+
}
21+
}
22+
}
23+
}
24+
}
25+
}
26+
27+
```
28+
#### Generated code
29+
```csharp
30+
public class Foo {
31+
FooProperties Properties { get; set; }
32+
}
33+
34+
public class FooProperties {
35+
string Bar { get; set; }
36+
}
37+
```
38+
### After
39+
#### Spec
40+
```json5
41+
42+
"definitions": {
43+
"Foo": {
44+
"type": "object",
45+
"properties": {
46+
"properties": {
47+
"type": "object",
48+
"properties": {
49+
"bar": {
50+
"type": "string"
51+
}
52+
}
53+
},
54+
"x-ms-client-flatten": true
55+
}
56+
}
57+
}
58+
59+
```
60+
#### Generated code
61+
```csharp
62+
public class Foo {
63+
string Bar { get; set; }
64+
}
65+
```

docs/boolean-property.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# M3018 - BooleanPropertyNotRecommended
2+
## Description
3+
Booleans are not descriptive and make them hard to use. Instead use string enums with allowed set of values defined.
4+
5+
## Why?
6+
This is a warning prompting to evaluate whether the property is really a boolean or not, the intent is to consider if there could be more than 2 values possible for the property in the future or not. If the answer is no, then a boolean is fine, if the answer is yes, there could be other values added in the future, making it an enum can help avoid breaking changes in the SDKs in the future.
7+
8+
## How to fix
9+
Create an enum property, follow autorest [x-ms-enum extension guidelines](https://github.com/Azure/autorest/blob/master/docs/extensions/readme.md#x-ms-enum)
10+
11+
12+
## Impact on generated code
13+
Boolean property will turn into a String or an Enum (if SDK language supports it), this will depend on "modelAsString" property values as specified in [x-ms-enum extension guidelines](https://github.com/Azure/autorest/blob/master/docs/extensions/readme.md#x-ms-enum)

docs/camel-casing-validation-rules.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#M3016 - DefinitionsPropertiesNamesCamelCase/BodyPropertiesNamesCamelCase
2+
## Description
3+
This violation is flagged if a model definition's property name (DefinitionsPropertiesNamesCamelCase) or a request body parameter's property name (BodyPropertiesNamesCamelCase) is not in `camelCase` format. This is because the model's properties are sent across the wire and must adhere to common Json conventions for naming. This implies that every property name must start with a lower cased letter and all subsequent words within the name must start with a capital letter. In cases where there are acronyms involved, a maximum of three contiguous capital letters are allowed (acronyms should not be longer that 2 characters - see https://msdn.microsoft.com/en-us/library/141e06ef(v=vs.71).aspx, third upper case could be start of next word). Eg: `redisCache`, `publicIPAddress`, `location` are valid, but `sampleSQLQuery` is not (must be renamed as `sampleSqlQuery`).
4+
5+
## How to fix
6+
Convert the property name string into `camelCase` format as suggested in the description
7+
8+
9+
## Effect on generated code
10+
### Before
11+
#### Spec
12+
```json5
13+
14+
"ModelDefinition":{
15+
"properties":{
16+
"ModelProperty1":{
17+
"type":"string"
18+
}
19+
}
20+
}
21+
22+
```
23+
#### Generated code
24+
```csharp
25+
public class FooParameter1 {
26+
27+
}
28+
```
29+
### After
30+
#### Spec
31+
```json5
32+
33+
"parameters": [
34+
{
35+
"name": "foo",
36+
"in": "body",
37+
"schema": {
38+
"$ref": "#/definition/FooCreationSettings"
39+
}
40+
}
41+
],
42+
43+
"definitions": {
44+
"FooCreationSettings": {
45+
"type": "object",
46+
"properties": {
47+
48+
}
49+
}
50+
}
51+
52+
```
53+
#### Generated code
54+
```csharp
55+
public class FooCreationSettings {
56+
57+
}
58+
```

docs/default-in-enum.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# DefaultInEnum
2+
## Description
3+
This rule applies when the value specified by the `default` property does not appear in the `enum` constraint for a schema.
4+
## How to fix
5+
Add the value from `default` property to the set of values in the `enum` constraint.
6+
7+
_or_
8+
9+
Change the value for the `default` property to one of the values in the `enum` array.
10+
## Effect on generated code
11+
Failing to pass this validation rule affects the generated code because default values allow users to call an operation without specifying a value for that parameter or property. In that case, the generated code will send the default value instead, which should be invalid for the server. Spec files that have this issue generate code that can easily send bad requests.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# DescriptiveDescriptionRequired
2+
## Description
3+
This rule validates that a description property is not empty. An empty description does not provide value for customers and makes it more difficult to use the generated code as a consumer.
4+
## How to fix
5+
Replace the empty string with a useful description.
6+
## Effect on generated code
7+
### Before
8+
```
9+
10+
```
11+
### After
12+
```
13+
14+
```

0 commit comments

Comments
 (0)