Skip to content

Commit 167342f

Browse files
author
khalil-kooli
committed
Support xs-ms-enum values without the 'value' label
This commit fixes the following issue Azure#216
1 parent d82b3c4 commit 167342f

File tree

4 files changed

+321
-5
lines changed

4 files changed

+321
-5
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"invalid_swagger_specification"
3+
}
Lines changed: 257 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,257 @@
1+
{
2+
"swagger": "2.0",
3+
"info": {
4+
"contact": {
5+
"email": "[email protected]",
6+
"name": "Swagger API Team",
7+
"url": "http://swagger.io"
8+
},
9+
"description": "A sample API that uses a petstore as an example to demonstrate features in the Swagger 2.0 specification",
10+
"license": {
11+
"name": "Apache 2.0",
12+
"url": "https://www.apache.org/licenses/LICENSE-2.0.html"
13+
},
14+
"termsOfService": "http://swagger.io/terms/",
15+
"title": "Swagger Petstore",
16+
"version": "1.0.0"
17+
},
18+
"host": "petstore.swagger.io",
19+
"basePath": "/api",
20+
"schemes": [
21+
"http"
22+
],
23+
"paths": {
24+
"/pets": {
25+
"get": {
26+
"produces": [
27+
"application/json"
28+
],
29+
"parameters": [
30+
{
31+
"collectionFormat": "multi",
32+
"description": "tags to filter by",
33+
"in": "query",
34+
"items": {
35+
"type": "string"
36+
},
37+
"name": "tags",
38+
"required": false,
39+
"type": "array"
40+
},
41+
{
42+
"description": "maximum number of results to return",
43+
"format": "int32",
44+
"in": "query",
45+
"name": "limit",
46+
"required": false,
47+
"type": "integer"
48+
}
49+
],
50+
"responses": {
51+
"200": {
52+
"description": "pet response",
53+
"schema": {
54+
"items": {
55+
"$ref": "#/definitions/Pet"
56+
},
57+
"type": "array"
58+
}
59+
},
60+
"default": {
61+
"description": "unexpected error",
62+
"schema": {
63+
"$ref": "#/definitions/Error"
64+
}
65+
}
66+
},
67+
"description": "Returns all pets from the system that the user has access to the system",
68+
"operationId": "findPets"
69+
},
70+
"post": {
71+
"consumes": [
72+
"application/json"
73+
],
74+
"produces": [
75+
"application/json"
76+
],
77+
"parameters": [
78+
{
79+
"description": "Pet to add to the store",
80+
"in": "body",
81+
"name": "body",
82+
"required": true,
83+
"schema": {
84+
"$ref": "#/definitions/NewPet"
85+
}
86+
}
87+
],
88+
"responses": {
89+
"200": {
90+
"description": "pet response",
91+
"schema": {
92+
"$ref": "#/definitions/Pet"
93+
}
94+
},
95+
"default": {
96+
"description": "unexpected error",
97+
"schema": {
98+
"$ref": "#/definitions/Error"
99+
}
100+
}
101+
},
102+
"description": "Creates a new pet in the store. Duplicates are allowed",
103+
"operationId": "addPet"
104+
}
105+
},
106+
"/pets/{id}": {
107+
"delete": {
108+
"produces": [
109+
"application/json"
110+
],
111+
"parameters": [
112+
{
113+
"description": "ID of pet to delete",
114+
"format": "int64",
115+
"in": "path",
116+
"name": "id",
117+
"required": true,
118+
"type": "integer"
119+
}
120+
],
121+
"responses": {
122+
"204": {
123+
"description": "pet deleted"
124+
},
125+
"default": {
126+
"description": "unexpected error",
127+
"schema": {
128+
"$ref": "#/definitions/Error"
129+
}
130+
}
131+
},
132+
"description": "deletes a single pet based on the ID supplied",
133+
"operationId": "deletePet"
134+
},
135+
"get": {
136+
"produces": [
137+
"application/json"
138+
],
139+
"parameters": [
140+
{
141+
"description": "ID of pet to fetch",
142+
"format": "int64",
143+
"in": "path",
144+
"name": "id",
145+
"required": true,
146+
"type": "integer"
147+
}
148+
],
149+
"responses": {
150+
"200": {
151+
"description": "pet response",
152+
"schema": {
153+
"$ref": "#/definitions/Pet"
154+
}
155+
},
156+
"default": {
157+
"description": "unexpected error",
158+
"schema": {
159+
"$ref": "#/definitions/Error"
160+
}
161+
}
162+
},
163+
"description": "Returns a user based on a single ID, if the user does not have access to the pet",
164+
"operationId": "find pet by id"
165+
}
166+
}
167+
},
168+
"definitions": {
169+
"Error": {
170+
"properties": {
171+
"code": {
172+
"format": "int32",
173+
"type": "integer"
174+
},
175+
"message": {
176+
"type": "string"
177+
}
178+
},
179+
"required": [
180+
"code",
181+
"message"
182+
],
183+
"type": "object"
184+
},
185+
"NewPet": {
186+
"properties": {
187+
"name": {
188+
"type": "string"
189+
},
190+
"tag": {
191+
"type": "string"
192+
}
193+
},
194+
"required": [
195+
"name"
196+
],
197+
"type": "object"
198+
},
199+
"Pet": {
200+
"allOf": [
201+
{
202+
"$ref": "#/definitions/NewPet"
203+
},
204+
{
205+
"properties": {
206+
"id": {
207+
"format": "int64",
208+
"type": "integer"
209+
},
210+
"petType": {
211+
"$ref": "#/definitions/PetType"
212+
}
213+
},
214+
"required": [
215+
"id"
216+
],
217+
"type": "object"
218+
}
219+
]
220+
},
221+
"PetType": {
222+
"enum": [
223+
"Mammals",
224+
"Fish",
225+
"Birds",
226+
"Reptiles",
227+
"Amphibians",
228+
"Invertebrates"
229+
],
230+
"type": "string",
231+
"x-ms-enum": {
232+
"name": "PetType",
233+
"modelAsString": false,
234+
"values": [
235+
{
236+
"value": 0,
237+
"description": "humans and all other animals that are warm-blooded vertebrates",
238+
"name": "Mammals"
239+
},
240+
{
241+
"value": 1,
242+
"description": "aquatic, craniate, gill-bearing animals that lack limbs with digits"
243+
},
244+
{
245+
"value": 2,
246+
"name": "Birds"
247+
},
248+
3,
249+
4,
250+
5
251+
],
252+
"x-nullable": false
253+
}
254+
}
255+
},
256+
"x-components": {}
257+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using System.Reflection;
2+
3+
using System.IO;
4+
using AutoRest.Swagger.Model;
5+
using Newtonsoft.Json;
6+
using Xunit;
7+
using OpenApiDiff.Core;
8+
9+
namespace AutoRest.Swagger.UTest
10+
{
11+
/// <summary>
12+
/// This class contains tests about the swagger parser
13+
/// </summary>
14+
[Collection("Comparison Tests")]
15+
public class SwaggerParserTest
16+
{
17+
private static string ReadSwaggerFile(string fileName)
18+
{
19+
var baseDir = Directory.GetParent(typeof(SwaggerParserTest).GetTypeInfo().Assembly.Location)
20+
.ToString();
21+
var filePath = Path.Combine(baseDir, "Resource", "Swagger", fileName);
22+
return File.ReadAllText(filePath);
23+
}
24+
25+
/// <summary>
26+
/// Verifies that the Parser throws an Exception when an in valid json is given
27+
/// </summary>
28+
[Fact]
29+
public void SwaggerParser_Should_Throw_Exception_When_Invalid_Json()
30+
{
31+
const string fileName = "invalid_swagger_specification.json";
32+
var documentAsString = ReadSwaggerFile(fileName);
33+
Assert.Throws<JsonReaderException>(() => SwaggerParser.Parse(documentAsString, fileName));
34+
}
35+
36+
/// <summary>
37+
/// Verifies that a valid JsonDocument object is parsed when input is a valid Swagger
38+
/// </summary>
39+
[Fact]
40+
public void SwaggerParser_Should_Return_Valid_Swagger_Document_Object()
41+
{
42+
const string fileName = "swagger_specification.json";
43+
var documentAsString = ReadSwaggerFile(fileName);
44+
var validSwaggerDocument = SwaggerParser.Parse(documentAsString, fileName);
45+
Assert.IsType<JsonDocument<ServiceDefinition>>(validSwaggerDocument);
46+
}
47+
}
48+
}

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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,30 @@
77
namespace AutoRest.Swagger.Model
88
{
99
/// <summary>
10-
/// The object provides metadata about the API.
11-
/// The metadata can be used by the clients if needed, and can be presented
10+
/// The object provides metadata about the API.
11+
/// The metadata can be used by the clients if needed, and can be presented
1212
/// in the Swagger-UI for convenience.
1313
/// </summary>
14-
14+
1515
public class XmsEnumValue
1616
{
1717
public dynamic value;
1818
public string description;
1919
public string name;
20+
public XmsEnumValue(dynamic v)
21+
{
22+
value = v;
23+
}
24+
25+
public static explicit operator XmsEnumValue(int v) => new XmsEnumValue(v);
26+
public static explicit operator XmsEnumValue(long v) => new XmsEnumValue(v);
27+
public static explicit operator XmsEnumValue(string v) => new XmsEnumValue(v);
2028
}
2129

22-
public class XmsEnumExtension
30+
public class XmsEnumExtension
2331
{
2432
public string Name { get; set; }
25-
33+
2634
public Boolean ModelAsString { get; set; }
2735

2836
public IList<XmsEnumValue> values { get; set; }

0 commit comments

Comments
 (0)