Skip to content

Commit 7212aa3

Browse files
committed
Added examples
1 parent cb2a248 commit 7212aa3

File tree

2 files changed

+152
-1
lines changed

2 files changed

+152
-1
lines changed

README.md

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
The **OpenAPI.NET** SDK contains a useful object model for OpenAPI documents in .NET along with common serializers to extract raw OpenAPI JSON and YAML documents from the model.
77

8-
**See more information on the Open API spec and its history here: <a href="https://www.openapis.org">Open API Initiative</a>**
8+
**See more information on the OpenAPI spec and its history here: <a href="https://www.openapis.org">OpenAPI Initiative</a>**
99

1010
Project Objectives
1111

@@ -22,6 +22,73 @@ The base JSON and YAML Readers are built into this project. Below is the list of
2222
- .NET Comment Reader: [Coming Soon]
2323
- OData (CSDL) Reader: [Comming Soon]
2424

25+
# Example Usage
26+
27+
Creating a OpenAPI Document
28+
29+
```Csharp
30+
var document = new OpenApiDocument
31+
{
32+
Info = new OpenApiInfo
33+
{
34+
Version = "1.0.0",
35+
Title = "Swagger Petstore (Simple)",
36+
},
37+
Servers = new List<OpenApiServer>
38+
{
39+
new OpenApiServer { Url = "http://petstore.swagger.io/api" }
40+
},
41+
Paths = new OpenApiPaths
42+
{
43+
["/pets"] = new OpenApiPathItem
44+
{
45+
Operations = new Dictionary<OperationType, OpenApiOperation>
46+
{
47+
[OperationType.Get] = new OpenApiOperation
48+
{
49+
Description = "Returns all pets from the system that the user has access to",
50+
Responses = new OpenApiResponses
51+
{
52+
["200"] = new OpenApiResponse
53+
{
54+
Description = "OK"
55+
}
56+
}
57+
}
58+
}
59+
}
60+
}
61+
};
62+
```
63+
64+
Reading and writing a OpenAPI description
65+
66+
``` CSharp
67+
var httpClient = new HttpClient
68+
{
69+
BaseAddress = new Uri("https://raw.githubusercontent.com/OAI/OpenAPI-Specification/")
70+
};
71+
72+
var stream = await httpClient.GetStreamAsync("master/examples/v3.0/petstore.yaml");
73+
74+
// Read V3 as YAML
75+
var openApiDocument = new OpenApiStreamReader().Read(stream, out var diagnostics);
76+
77+
var outputStringWriter = new StringWriter();
78+
var writer = new OpenApiJsonWriter(outputStringWriter);
79+
80+
// Write V2 as JSON
81+
openApiDocument.SerializeAsV2(writer);
82+
83+
outputStringWriter.Flush();
84+
var output = outputStringWriter.GetStringBuilder().ToString();
85+
86+
Assert.Empty(diagnostics.Errors);
87+
Assert.NotNull(openApiDocument);
88+
Assert.NotEmpty(output);
89+
90+
```
91+
2592
# Build Status
2693

2794
|**master**|
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
using Microsoft.OpenApi.Models;
2+
using Microsoft.OpenApi.Writers;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.IO;
6+
using System.Linq;
7+
using System.Net.Http;
8+
using System.Text;
9+
using System.Threading.Tasks;
10+
using Xunit;
11+
12+
namespace Microsoft.OpenApi.Readers.Tests.V3Tests
13+
{
14+
public class OpenApiReadmeTests
15+
{
16+
17+
[Fact]
18+
public void CreatingADocumentTest()
19+
{
20+
var document = new OpenApiDocument
21+
{
22+
Info = new OpenApiInfo
23+
{
24+
Version = "1.0.0",
25+
Title = "Swagger Petstore (Simple)",
26+
},
27+
Servers = new List<OpenApiServer>
28+
{
29+
new OpenApiServer { Url = "http://petstore.swagger.io/api" }
30+
},
31+
Paths = new OpenApiPaths
32+
{
33+
["/pets"] = new OpenApiPathItem
34+
{
35+
Operations = new Dictionary<OperationType, OpenApiOperation>
36+
{
37+
[OperationType.Get] = new OpenApiOperation
38+
{
39+
Description = "Returns all pets from the system that the user has access to",
40+
Responses = new OpenApiResponses
41+
{
42+
["200"] = new OpenApiResponse
43+
{
44+
Description = "OK"
45+
}
46+
}
47+
}
48+
}
49+
}
50+
}
51+
};
52+
53+
Assert.NotNull(document);
54+
}
55+
56+
[Fact]
57+
public async Task ReadingAndWritingADocument()
58+
{
59+
var httpClient = new HttpClient
60+
{
61+
BaseAddress = new Uri("https://raw.githubusercontent.com/OAI/OpenAPI-Specification/")
62+
};
63+
64+
var stream = await httpClient.GetStreamAsync("master/examples/v3.0/petstore.yaml");
65+
66+
// Read V3 as YAML
67+
var openApiDocument = new OpenApiStreamReader().Read(stream, out var diagnostics);
68+
69+
var outputStringWriter = new StringWriter();
70+
var writer = new OpenApiJsonWriter(outputStringWriter);
71+
72+
// Write V2 as JSON
73+
openApiDocument.SerializeAsV2(writer);
74+
75+
outputStringWriter.Flush();
76+
var output = outputStringWriter.GetStringBuilder().ToString();
77+
78+
Assert.Empty(diagnostics.Errors);
79+
Assert.NotNull(openApiDocument);
80+
Assert.NotEmpty(output);
81+
82+
}
83+
}
84+
}

0 commit comments

Comments
 (0)