Skip to content

Commit 0e49127

Browse files
jianyexijianye xi
andauthored
fix type object change (#237)
* fix type object change * fix test * fix test * fix test * fix test Co-authored-by: jianye xi <[email protected]>
1 parent 87568dc commit 0e49127

File tree

6 files changed

+143
-3
lines changed

6 files changed

+143
-3
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 0.9.7 Released on 2022-08-15
4+
5+
- change rule 'TypeChanged' to 'Info' if adding 'type:object' to an schema with "properties".
6+
37
## 0.9.6 Released on 2022-06-09
48

59
- using autorest v3.6.1'.
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
{
2+
"swagger": 2.0,
3+
"info": {
4+
"title": "type_changed",
5+
"version": "1.0"
6+
},
7+
"host": "localhost:8000",
8+
"schemes": [ "http", "https" ],
9+
"consumes": [ "text/plain", "text/json" ],
10+
"produces": [ "text/plain" ],
11+
"paths": {
12+
"/api/Parameters": {
13+
"put": {
14+
"tag": [ "Parameters" ],
15+
"operationId": "Parameters_Put",
16+
"produces": [
17+
"text/plain"
18+
],
19+
"parameters": [
20+
{
21+
"name": "database",
22+
"in": "body",
23+
"required": true,
24+
"type": "object",
25+
"schema": { "$ref": "#/definitions/Database" }
26+
}
27+
]
28+
}
29+
}
30+
},
31+
"definitions": {
32+
"Database": {
33+
"properties": {
34+
"a": {
35+
"type": "object",
36+
"readOnly": true,
37+
"properties": {
38+
"b": {
39+
"type": "string",
40+
"readOnly": true
41+
}
42+
},
43+
"description": "This is a system generated property.\nThe _rid value is empty for this operation."
44+
},
45+
"b": {
46+
"type": "integer",
47+
"readOnly": true,
48+
"default": 0,
49+
"description": "This property shows the number of databases returned."
50+
}
51+
}
52+
}
53+
}
54+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
{
2+
"swagger": 2.0,
3+
"info": {
4+
"title": "type_changed",
5+
"version": "1.0"
6+
},
7+
"host": "localhost:8000",
8+
"schemes": [ "http", "https" ],
9+
"consumes": [ "text/plain", "text/json" ],
10+
"produces": [ "text/plain" ],
11+
"paths": {
12+
"/api/Parameters": {
13+
"put": {
14+
"tag": [ "Parameters" ],
15+
"operationId": "Parameters_Put",
16+
"produces": [
17+
"text/plain"
18+
],
19+
"parameters": [
20+
{
21+
"name": "database",
22+
"in": "body",
23+
"required": true,
24+
"type": "object",
25+
"schema": { "$ref": "#/definitions/Database" }
26+
}
27+
]
28+
}
29+
}
30+
},
31+
"definitions": {
32+
"Database": {
33+
"properties": {
34+
"a": {
35+
"readOnly": true,
36+
"properties": {
37+
"b": {
38+
"type": "string",
39+
"readOnly": true
40+
}
41+
},
42+
"description": "This is a system generated property.\nThe _rid value is empty for this operation."
43+
},
44+
"b": {
45+
"type": "integer",
46+
"readOnly": true,
47+
"default": 0,
48+
"description": "This property shows the number of databases returned."
49+
}
50+
}
51+
}
52+
}
53+
}

openapi-diff/src/modeler/AutoRest.Swagger.Tests/SwaggerModelerCompareTests.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,21 @@ public void PropertyTypeChanged()
150150
Assert.Equal("new/type_changed.json#/definitions/Database/properties/a", error.NewJsonRef);
151151
}
152152

153+
/// <summary>
154+
/// Verifies that if adding a 'type:object' to a schema with 'properties', it's not error.
155+
/// </summary>
156+
[Fact]
157+
public void TypeObjectChanged()
158+
{
159+
var messages = CompareSwagger("type_changed_01.json").ToArray();
160+
var missing = messages.Where(m => m.Id == ComparisonMessages.TypeChanged.Id);
161+
Assert.NotEmpty(missing);
162+
var error = missing.Where(err => err.NewJsonRef.StartsWith("new/type_changed_01.json#/definitions/")).FirstOrDefault();
163+
Assert.NotNull(error);
164+
Assert.Equal(Category.Info, error.Severity);
165+
Assert.Equal("new/type_changed_01.json#/definitions/Database/properties/a", error.NewJsonRef);
166+
}
167+
153168
/// <summary>
154169
/// Verifies that if you change the type format of a schema property, it's caught.
155170
/// </summary>

openapi-diff/src/modeler/AutoRest.Swagger/Model/SwaggerObject.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,26 @@ T previous
127127

128128
// Are the types the same?
129129

130-
if (prior.Type.HasValue != Type.HasValue || (Type.HasValue && prior.Type.Value != Type.Value))
130+
if ((Type.HasValue && prior.Type.HasValue && prior.Type.Value != Type.Value))
131131
{
132-
context.LogBreakingChange(ComparisonMessages.TypeChanged,
132+
context.LogError(ComparisonMessages.TypeChanged,
133133
Type.HasValue ? Type.Value.ToString().ToLower() : "",
134134
prior.Type.HasValue ? prior.Type.Value.ToString().ToLower() : "");
135135
}
136+
var isObject = Type.HasValue && Type.Value == DataType.Object && (this is Schema) ? (this as Schema).Properties != null : false;
137+
if (prior.Type.HasValue != Type.HasValue) {
138+
if (!prior.Type.HasValue && Type.HasValue && isObject)
139+
{
140+
context.LogInfo(ComparisonMessages.TypeChanged,
141+
Type.HasValue ? Type.Value.ToString().ToLower() : "",
142+
prior.Type.HasValue ? prior.Type.Value.ToString().ToLower() : "");
143+
}
144+
else {
145+
context.LogError(ComparisonMessages.TypeChanged,
146+
Type.HasValue ? Type.Value.ToString().ToLower() : "",
147+
prior.Type.HasValue ? prior.Type.Value.ToString().ToLower() : "");
148+
}
149+
}
136150

137151
// What about the formats?
138152

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@azure/oad",
3-
"version": "0.9.6",
3+
"version": "0.9.7",
44
"author": {
55
"name": "Microsoft Corporation",
66
"email": "[email protected]",

0 commit comments

Comments
 (0)