Skip to content

Commit 48c4bb6

Browse files
author
Rachit Malik
committed
made changes requested by vincent
1 parent d928a4a commit 48c4bb6

File tree

1 file changed

+32
-52
lines changed

1 file changed

+32
-52
lines changed

docs/images/upgrade-guide-2.md

Lines changed: 32 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ OpenAPI.NET v2 is a major update to the OpenAPI.NET library. This release includ
1414

1515
Since the release of the first version of the OpenAPI.NET library in 2018, there has not been a major version update to the library. With the addition of support for OpenAPI v3.1 it was necessary to make some breaking changes. With this opportunity, we have taken the time to make some other improvements to the library, based on the experience we have gained supporting a large community of users for the last six years .
1616

17-
## Performance Improvements
17+
# Performance Improvements
1818

1919
One of the key features of OpenAPI.NET is its performance. This version makes it possible to parse JSON based OpenAPI descriptions even faster. OpenAPI.NET v1 relied on the excellent YamlSharp library for parsing both JSON and YAML files. With OpenAPI.NET v2 we are relying on System.Text.Json for parsing JSON files. For YAML files, we continue to use YamlSharp to parse YAML but then convert to JsonNodes for processing. This allows us to take advantage of the performance improvements in System.Text.Json while still supporting YAML files.
2020

@@ -24,7 +24,7 @@ In v1, instances of `$ref` were resolved in a second pass of the document to ens
2424

2525
## Reduced Dependencies
2626

27-
In OpenAPI v1, it was necessary to include the Microsoft.OpenApi.Readers library to be able to read OpenAPI descriptions in either YAML or JSON. In OpenAPI.NET v2, the core Microsoft.OpenAPI library can both read and write JSON. It is only necessary to use the readers library if you need YAML support. This allows teams who are only working in JSON to avoid the additional dependency and therefore eliminate all non-.NET library references.
27+
In OpenAPI v1, it was necessary to include the Microsoft.OpenApi.Readers library to be able to read OpenAPI descriptions in either YAML or JSON. In OpenAPI.NET v2, the core Microsoft.OpenAPI library can both read and write JSON. It is only necessary to use the newly renamed [Microsoft.OpenApi.YamlReader](https://www.nuget.org/packages/Microsoft.OpenApi.YamlReader/) library if you need YAML support. This allows teams who are only working in JSON to avoid the additional dependency and therefore eliminate all non-.NET library references.
2828

2929
## API Enhancements
3030

@@ -38,9 +38,9 @@ The same pattern can be used for `OpenApiStreamReader` and `OpenApiTextReader`.
3838

3939
```csharp
4040
var reader = new OpenApiStreamReader();
41-
var readResult = await reader.ReadAsync(streamOpenApiDoc);
41+
var (document, diagnostics) = await reader.ReadAsync(streamOpenApiDoc);
4242
```
43-
A `ReadResult` object acts as a tuple of `OpenApiDiagnostic` and `OpenApiDocument`.
43+
A `ReadResult` object acts as a tuple of `OpenApiDocument` and `OpenApiDiagnostic`.
4444

4545
The challenge with this approach is that the reader classes are not very discoverable and the behaviour is not actually consistent with `*TextReader` pattern that allows incrementally reading the document. This library does not support incrementally reading the OpenAPI Document. It only reads a complete document and returns an `OpenApiDocument` instance.
4646

@@ -57,7 +57,7 @@ public class OpenApiDocument {
5757

5858
This API design allows a developer to use IDE autocomplete to present all the loading options by simply knowning the name of the `OpenApiDocument` class. Each of these methods are layered ontop of the more primitive methods to ensure consistent behaviour.
5959

60-
As the YAML format is only supported when including the `Microsoft.OpenApi.Readers` library it was decided not to use an enum for the `format` parameter. We are considering implementing a more [strongly type solution](https://github.com/microsoft/OpenAPI.NET/issues/1952) similar to the way that `HttpMethod` is implemented so that we have a strongly typed experience that is also extensible.
60+
As the YAML format is only supported when including the `Microsoft.OpenApi.YamlReader` library it was decided not to use an enum for the `format` parameter. We are considering implementing a more [strongly type solution](https://github.com/microsoft/OpenAPI.NET/issues/1952) similar to the way that `HttpMethod` is implemented so that we have a strongly typed experience that is also extensible.
6161

6262
Where the loading methods are used without a format property, we will attempt to parse the document using the default JSON reader. If that fails and the Yaml reader is registered, then we will attempt to read as YAML. The goal is always to provide the fastest path with JSON but still maintain the convenience of not having to care whether a URL points to YAML or JSON if you need that flexibility.
6363

@@ -138,22 +138,22 @@ The OpenAPI 3.1 specification changes significantly how it leverages JSON Schema
138138

139139
```csharp
140140
/// $schema, a JSON Schema dialect identifier. Value must be a URI
141-
public virtual string Schema { get; set; }
141+
public string Schema { get; set; }
142142
/// $id - Identifies a schema resource with its canonical URI.
143-
public virtual string Id { get; set; }
143+
public string Id { get; set; }
144144
/// $comment - reserves a location for comments from schema authors to readers or maintainers of the schema.
145-
public virtual string Comment { get; set; }
145+
public string Comment { get; set; }
146146
/// $vocabulary- used in meta-schemas to identify the vocabularies available for use in schemas described by that meta-schema.
147-
public virtual IDictionary<string, bool> Vocabulary { get; set; }
147+
public IDictionary<string, bool> Vocabulary { get; set; }
148148
/// $dynamicRef - an applicator that allows for deferring the full resolution until runtime, at which point it is resolved each time it is encountered while evaluating an instance
149-
public virtual string DynamicRef { get; set; }
149+
public string DynamicRef { get; set; }
150150
/// $dynamicAnchor - used to create plain name fragments that are not tied to any particular structural location for referencing purposes, which are taken into consideration for dynamic referencing.
151-
public virtual string DynamicAnchor { get; set; }
151+
public string DynamicAnchor { get; set; }
152152
/// $defs - reserves a location for schema authors to inline re-usable JSON Schemas into a more general schema.
153-
public virtual IDictionary<string, OpenApiSchema> Definitions { get; set; }
154-
public virtual IDictionary<string, OpenApiSchema> PatternProperties { get; set; } = new Dictionary<string, OpenApiSchema>();
155-
public virtual bool UnevaluatedProperties { get; set;}
156-
public virtual bool UnEvaluatedProperties { get; set; } // Duplicate should be removed
153+
public IDictionary<string, OpenApiSchema> Definitions { get; set; }
154+
public IDictionary<string, OpenApiSchema> PatternProperties { get; set; } = new Dictionary<string, OpenApiSchema>();
155+
public bool UnevaluatedProperties { get; set;}
156+
public bool UnEvaluatedProperties { get; set; } // Duplicate should be removed
157157
158158
```
159159
#### Changes to existing keywords
@@ -162,44 +162,26 @@ The OpenAPI 3.1 specification changes significantly how it leverages JSON Schema
162162

163163
public virtual decimal? ExclusiveMaximum { get; set; } // New, but currently named v31ExclusiveMaximum
164164
public virtual decimal? ExclusiveMinimum { get; set; } // New, but Currently named v31ExclusiveMinimum)
165-
public virtual bool? ExclusiveMaximum { get; set; } // To be removed
166-
public virtual bool? ExclusiveMinimum { get; set; } // To be removed
167-
165+
public virtual decimal? ExclusiveMaximum { get; set; } //type changed to reflect the new version of JSON schema
166+
public virtual decimal? ExclusiveMinimum { get; set; } // type changed to reflect the new version of JSON schema
168167
public virtual JsonSchemaType? Type { get; set; } // Was string, now flagged enum
169168
public virtual decimal? Maximum { get; set; } // Double???
170169
public virtual decimal? Minimum { get; set; } // Double???
171170
172171
public virtual JsonNode Default { get; set; } // Type matching no longer enforced. Was IOpenApiAny
173172
public virtual bool ReadOnly { get; set; } // No longer has defined semantics in OpenAPI 3.1
174173
public virtual bool WriteOnly { get; set; } // No longer has defined semantics in OpenAPI 3.1
175-
public virtual bool UnresolvedReference { get; set; } // Can be removed
176-
public virtual OpenApiReference Reference { get; set; } // Can be removed
177174
178175
public virtual JsonNode Example { get; set; } // No longer IOpenApiAny
179176
public virtual IList<JsonNode> Examples { get; set; }
180177
public virtual IList<JsonNode> Enum { get; set; } = new List<JsonNode>();
181-
182-
public virtual bool Nullable { get; set; } // To be removed
183178
public virtual OpenApiExternalDocs ExternalDocs { get; set; } // OpenApi Vocab
184179
public virtual bool Deprecated { get; set; } // OpenApi Vocab
185180
public virtual OpenApiXml Xml { get; set; } // OpenApi Vocab
186181
187182
public IDictionary<string, object> Annotations { get; set; } // Custom keywords?
188183
```
189184

190-
#### Potential Performance improvements
191-
192-
```csharp
193-
public virtual IDictionary<string, OpenApiSchema> PatternProperties { get; set; } = new Dictionary<string, OpenApiSchema>();
194-
public virtual IList<JsonNode> Enum { get; set; } = new List<JsonNode>();
195-
public virtual IList<OpenApiSchema> AllOf { get; set; } = new List<OpenApiSchema>();
196-
public virtual IList<OpenApiSchema> OneOf { get; set; } = new List<OpenApiSchema>();
197-
public virtual IList<OpenApiSchema> AnyOf { get; set; } = new List<OpenApiSchema>();
198-
public virtual ISet<string> Required { get; set; } = new HashSet<string>();
199-
public virtual IDictionary<string, OpenApiSchema> Properties { get; set; } = new Dictionary<string, OpenApiSchema>();
200-
public virtual IDictionary<string, IOpenApiExtension> Extensions { get; set; } = new Dictionary<string, IOpenApiExtension>();
201-
```
202-
203185
#### OpenApiSchema methods
204186

205187
Other than the addition of the SerializeAsV31, the methods have not changed.
@@ -306,7 +288,7 @@ OpenApiOperation operation = new OpenApiOperation
306288
// After (2.0)
307289
OpenApiOperation operation = new OpenApiOperation
308290
{
309-
HttpMethod = new HttpMethod("GET")
291+
HttpMethod = new HttpMethod("GET")// or HttpMethod.Get
310292
};
311293
```
312294

@@ -326,7 +308,7 @@ OpenApiDocument document = new OpenApiDocument
326308
{
327309
Components = new OpenApiComponents()
328310
{
329-
Schemas = new Dictionary<string, OpenApiSchema?>()
311+
Schemas = new Dictionary<string, IOpenApiSchema?>()
330312
}
331313
};
332314

@@ -350,15 +332,11 @@ OpenApiSchema schema = new OpenApiSchema
350332
// After (2.0)
351333
OpenApiComponents components = new OpenApiComponents
352334
{
353-
Schemas = new Dictionary<string, OpenApiSchema>
335+
Schemas = new Dictionary<string, IOpenApiSchema>
354336
{
355337
["MySchema"] = new OpenApiSchema
356338
{
357-
Reference = new OpenApiReference
358-
{
359-
Type = ReferenceType.Schema,
360-
Id = "MySchema"
361-
}
339+
Reference = new OpenApiSchemaReference("MySchema")
362340
}
363341
}
364342
};
@@ -367,23 +345,25 @@ OpenApiComponents components = new OpenApiComponents
367345
### OpenApiDocument.SerializeAs()
368346
The `SerializeAs()` method simplifies serialization scenarios, making it easier to convert OpenAPI documents to different formats.
369347
**Example:**
348+
370349
```csharp
371-
OpenApiDocument document = new OpenApiDocument();string json = document.SerializeAs(OpenApiSpecVersion.OpenApi3_0, OpenApiFormat.Json);
350+
OpenApiDocument document = new OpenApiDocument();
351+
string json = document.SerializeAs(OpenApiSpecVersion.OpenApi3_0, OpenApiFormat.Json);
372352

373-
### Bug Fixes#### Serialization of ReferencesFixed a bug where references would not serialize summary or descriptions in OpenAPI 3.1.**Example:**
374-
OpenApiSchema schema = new OpenApiSchema
353+
```
354+
355+
### Bug Fixes
356+
357+
## Serialization of References:
358+
Fixed a bug where references would not serialize summary or descriptions in OpenAPI 3.1.**Example:**
359+
```csharp
360+
OpenApiSchemaReference schemaRef = new OpenApiSchemaReference("MySchema")
375361
{
376-
Reference = new OpenApiReference
377-
{
378-
Type = ReferenceType.Schema,
379-
Id = "MySchema"
380-
},
381362
Summary = "This is a summary",
382363
Description = "This is a description"
383364
};
384365
```
385366

386-
387367
## Feedback
388368
If you have any feedback please file a GitHub issue here: https://github.com/microsoft/OpenAPI.NET/issues
389369
The team is looking forward to hear your experience trying the new version and we hope you have fun busting out your OpenAPI 3.1 descriptions.

0 commit comments

Comments
 (0)