Skip to content

Commit 2ab6456

Browse files
jianyexijianye xi
andauthored
support multiple default types (#249)
Co-authored-by: jianye xi <[email protected]>
1 parent be86411 commit 2ab6456

File tree

6 files changed

+162
-3
lines changed

6 files changed

+162
-3
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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": "string",
36+
"readOnly": true,
37+
"description": "This is a system generated property.\nThe _rid value is empty for this operation.",
38+
"default":"aa"
39+
},
40+
"b": {
41+
"type": "string",
42+
"enum": [
43+
"A",
44+
"B"
45+
],
46+
"x-ms-enum": {
47+
"name": "ResourceIdentityType",
48+
"modelAsString": true
49+
},
50+
"default":"A",
51+
"description": "This property shows the number of databases returned."
52+
},
53+
"c": {
54+
"type": "number",
55+
"description": "This is a system generated property.\nThe _rid value is empty for this operation.",
56+
"default":2
57+
}
58+
},
59+
"default": { "c":"test"}
60+
}
61+
},
62+
"DatabaseList":{
63+
"type":"array",
64+
"items": {
65+
"$ref": "#/definitions/Database"
66+
},
67+
"default": [{"c":4}]
68+
}
69+
}

openapi-diff/src/modeler/AutoRest.Swagger.Tests/Resource/Swagger/new/operation_check_02.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,8 @@
120120
"b": {
121121
"type": "integer",
122122
"readOnly": true,
123-
"description": "This is a system generated property.\nThe resource id (_rid) is a unique identifier that is also hierarchical per the resource stack on the resource model. It is used internally for placement of and navigation to the database resource."
123+
"description": "This is a system generated property.\nThe resource id (_rid) is a unique identifier that is also hierarchical per the resource stack on the resource model. It is used internally for placement of and navigation to the database resource.",
124+
"default": 11
124125
},
125126
"c": {
126127
"type": "integer",
@@ -144,6 +145,10 @@
144145
"description": "This is a system generated property.\nIt specifies the addressable path of the users resource."
145146
}
146147
}
148+
,
149+
"default": {
150+
"id": "test"
151+
}
147152
}
148153
}
149154
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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": "string",
36+
"readOnly": true,
37+
"description": "This is a system generated property.\nThe _rid value is empty for this operation.",
38+
"default":"a"
39+
},
40+
"b": {
41+
"type": "string",
42+
"enum": [
43+
"A",
44+
"B"
45+
],
46+
"x-ms-enum": {
47+
"name": "ResourceIdentityType",
48+
"modelAsString": true
49+
},
50+
"description": "This property shows the number of databases returned."
51+
},
52+
"c": {
53+
"type": "number",
54+
"description": "This is a system generated property.\nThe _rid value is empty for this operation.",
55+
"default":1
56+
}
57+
},
58+
"default": { "b":"test"}
59+
}
60+
},
61+
"DatabaseList":{
62+
"type":"array",
63+
"items": {
64+
"$ref": "#/definitions/Database"
65+
},
66+
"default": [{"c":2}]
67+
}
68+
}

openapi-diff/src/modeler/AutoRest.Swagger.Tests/Resource/Swagger/old/operation_check_02.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,9 @@
142142
"readOnly": true,
143143
"description": "This is a system generated property.\nIt specifies the addressable path of the users resource."
144144
}
145+
},
146+
"default": {
147+
"id": "test"
145148
}
146149
}
147150
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,18 @@ public void TypeObjectChanged()
165165
Assert.Equal("new/type_changed_01.json#/definitions/Database/properties/a", error.NewJsonRef);
166166
}
167167

168+
/// <summary>
169+
/// Verifies that if if you change the default value of a schema, it's caught .
170+
/// </summary>
171+
[Fact]
172+
public void DefaultValueChanged()
173+
{
174+
var messages = CompareSwagger("default_changed_01.json").ToArray();
175+
var errors = messages.Where(m => m.Id == ComparisonMessages.DefaultValueChanged.Id).ToArray();
176+
Assert.NotNull(errors);
177+
Assert.Equal(errors.Count(), 8);
178+
}
179+
168180
/// <summary>
169181
/// Verifies that if you change the type format of a schema property, it's caught.
170182
/// </summary>

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
using System.Collections.Generic;
77
using System.Diagnostics.CodeAnalysis;
88
using Newtonsoft.Json;
9+
using Newtonsoft.Json.Linq;
10+
911

1012
namespace AutoRest.Swagger.Model
1113
{
@@ -51,7 +53,7 @@ public abstract class SwaggerObject<T> : SwaggerBase<T>
5153
/// <summary>
5254
/// Sets a default value to the parameter.
5355
/// </summary>
54-
public virtual string Default { get; set; }
56+
public virtual dynamic Default { get; set; }
5557

5658
public virtual string MultipleOf { get; set; }
5759

@@ -154,7 +156,7 @@ T previous
154156

155157
CompareItems(context, prior);
156158

157-
if (Default != null && !Default.Equals(prior.Default) || (Default == null && !string.IsNullOrEmpty(prior.Default)))
159+
if ((Default != null || prior.Default != null) && !JToken.DeepEquals(Default,prior.Default))
158160
{
159161
context.LogBreakingChange(ComparisonMessages.DefaultValueChanged);
160162
}

0 commit comments

Comments
 (0)