Skip to content

Commit 0d3593d

Browse files
Merge pull request #2337 from microsoft/chore/update-docs
chore: update upgrade guide
2 parents 4c9cef5 + 09cd4af commit 0d3593d

File tree

1 file changed

+152
-15
lines changed

1 file changed

+152
-15
lines changed

docs/upgrade-guide-2.md

Lines changed: 152 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,29 @@ public class OpenApiSchema : IMetadataContainer, IOpenApiExtensible, IOpenApiRef
361361

362362
There are a number of new features in OpenAPI v3.1 that are now supported in OpenAPI.NET.
363363

364+
### JsonSchema Dialect and BaseUri in OpenApiDocument
365+
To enable full compatibility with JSON Schema, the OpenApiDocument class now supports a jsonSchemaDialect property. This property specifies the JSON Schema dialect used throughout the document, using a URI. By explicitly declaring the dialect, tooling can be directed to use a JSON Schema version other than the default [2020-12 draft](https://json-schema.org/draft/2020-12/json-schema-core.html). However, OpenAPI.NET does not guarantee compatibility with versions other than 2020-12.
366+
367+
In addition, a BaseUri property has been added to represent the identity of the OpenAPI document. If the document’s identity is not provided or cannot be determined at based on its location, this property will be set to a generated placeholder URI.
368+
369+
```csharp
370+
/// <summary>
371+
/// Describes an OpenAPI object (OpenAPI document). See: https://spec.openapis.org
372+
/// </summary>
373+
public class OpenApiDocument : IOpenApiSerializable, IOpenApiExtensible, IMetadataContainer
374+
{
375+
/// <summary>
376+
/// The default value for the $schema keyword within Schema Objects contained within this OAS document. This MUST be in the form of a URI.
377+
/// </summary>
378+
public Uri? JsonSchemaDialect { get; set; }
379+
380+
/// <summary>
381+
/// Absolute location of the document or a generated placeholder if location is not given
382+
/// </summary>
383+
public Uri BaseUri { get; internal set; }
384+
}
385+
```
386+
364387
### Webhooks
365388

366389
```csharp
@@ -483,6 +506,7 @@ OpenApiComponents components = new OpenApiComponents
483506
### OpenApiDocument.SerializeAs()
484507

485508
The `SerializeAs()` method simplifies serialization scenarios, making it easier to convert OpenAPI documents to different formats.
509+
486510
**Example:**
487511

488512
```csharp
@@ -505,6 +529,133 @@ var outputString = openApiDocument.Serialize(OpenApiSpecVersion.OpenApi2_0, Open
505529
var outputString = openApiDocument.Serialize(OpenApiSpecVersion.OpenApi2_0, OpenApiConstants.Json);
506530
```
507531

532+
### OpenApiSchema's Type property is now a flaggable enum
533+
534+
In v2.0, the Type property in OpenApiSchema is now defined as a flaggable enum, allowing consumers to swap nullable for type arrays.
535+
536+
**Example:**
537+
```csharp
538+
// v1.6.x
539+
var schema = new OpenApiSchema
540+
{
541+
Type = "string",
542+
Nullable = true
543+
}
544+
545+
// v2.0
546+
// bitwise OR(|) - combines flags to allow multiple types
547+
var schema = new OpenApiSchema
548+
{
549+
Type = JsonSchemaType.String | JsonSchemaType.Null
550+
}
551+
552+
// bitwise NOT(~) - inverts bits; filters out null flag
553+
var schema = new OpenApiSchema
554+
{
555+
Type = JsonSchemaType.String & ~JsonSchemaType.Null
556+
}
557+
558+
// bitwise AND(&) - intersects flags to check for a specific type
559+
var schema = new OpenApiSchema
560+
{
561+
Type = (JsonSchemaType.String & JsonSchemaType.Null) == JsonSchemaType.Null
562+
}
563+
564+
```
565+
566+
### Component registration in a document's workspace
567+
568+
When loading up a file into an in-memory document, all the components contained in the document are registered within the document's workspace by default to aid with reference resolution.
569+
However if you're working directly with a DOM and you need the references resolved, you can register the components as below:
570+
571+
```csharp
572+
// register all components
573+
document.Workspace.RegisterComponents(OpenApiDocument document);
574+
575+
// register single component
576+
document.AddComponent<T>(string id, T componentToRegister);
577+
```
578+
579+
### Refactored model architecture
580+
581+
The following structural improvements have been made to the OpenAPI model layer to enhance type safety, extensibility, and maintainability:
582+
583+
1. Model Interfaces Introduced:
584+
Each model now has a corresponding interface (e.g., IOpenApiSchema for OpenApiSchema). This allows for better abstraction and testing support, while also simplifying cross-cutting concerns like serialization.
585+
586+
2. Models as Reference Types:
587+
All models are now implemented as reference types to ensure consistent identity semantics, especially when managing circular references or shared definitions.
588+
589+
3. Type Assertion Pattern Adopted:
590+
A standardized pattern has been introduced for casting model instances to specific types safely and predictably, reducing the risk of invalid casts or reflection-based logic.
591+
592+
4. Removed Reference Fields from Base Models:
593+
Fields like Reference that were previously defined on base model types have been removed. Models that support referencing now handle this behavior explicitly via composition rather than inheritance.
594+
595+
5. New Target and RecursiveTarget Properties:
596+
A Target property that points to the actual resolved model instance as well as a RecursiveTarget property that handles recursive references and supports advanced dereferencing logic have been introduced.
597+
598+
6. Removed OpenApiReferenceResolver:
599+
This resolver class has been removed in favor of a more streamlined resolution model using the Target and RecursiveTarget properties along with updated reader/serializer pipelines.
600+
601+
### Visitor and Validator now pass an interface model
602+
603+
**Example:**
604+
```csharp
605+
//v1.6.x
606+
public override void Visit(OpenApiParameter parameter){}
607+
608+
//v2.0
609+
public override void Visit(IOpenApiParameter parameter){}
610+
```
611+
612+
### Cleaned up the IEffective/GetEffective infrastructure
613+
614+
All the IEffective and GetEffective methods in the models have been removed as we've implemented lazy reference resolution using the proxy design.
615+
616+
### Shallow Copy in place of copy constructors
617+
618+
Copy constructors for referenceable components have been made internal, a new *CreateShallowCopy()* method has been exposed on these models to facilitate cloning.
619+
620+
**Example:**
621+
```csharp
622+
var schema = new OpenApiSchema();
623+
var schemaCopy = schema.CreateShallowCopy();
624+
```
625+
626+
### Duplicated _style Property on Parameter Removed
627+
628+
The redundant _style property on the Parameter model has been removed to simplify the model's structure.
629+
630+
### Discriminator now use References
631+
632+
Discriminator mappings have been updated from using a Dictionary<string, string> to a Dictionary<string, OpenApiSchemaReference>. This change improves the handling of discriminator mappings by referencing OpenAPI schema components more explicitly, which enhances schema resolution.
633+
634+
**Example:**
635+
```csharp
636+
// v1.6.x
637+
Discriminator = new()
638+
{
639+
PropertyName = "@odata.type",
640+
Mapping = new Dictionary<string, string> {
641+
{
642+
"#microsoft.graph.directoryObject", "#/components/schemas/microsoft.graph.directoryObject"
643+
}
644+
}
645+
}
646+
647+
//v2.0
648+
Discriminator = new()
649+
{
650+
PropertyName = "@odata.type",
651+
Mapping = new Dictionary<string, OpenApiSchemaReference> {
652+
{
653+
"#microsoft.graph.directoryObject", new OpenApiSchemaReference("microsoft.graph.directoryObject")
654+
}
655+
}
656+
}
657+
```
658+
508659
### Bug Fixes
509660

510661
## Serialization of References
@@ -523,18 +674,4 @@ OpenApiSchemaReference schemaRef = new OpenApiSchemaReference("MySchema")
523674
## Feedback
524675

525676
If you have any feedback please file a GitHub issue [here](https://github.com/microsoft/OpenAPI.NET/issues)
526-
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.
527-
528-
## Todos
529-
530-
- Models now have matching interfaces + reference type + type assertion pattern + reference fields removed from the base model + Target + RecursiveTarget + removed OpenApiReferenceResolver.
531-
- Workspace + component resolution.
532-
- Visitor and Validator method now pass the interface model.
533-
- Removed all the IEffective/GetEffective infrastructure.
534-
- OpenApiSchema.Type is now a flag enum + bitwise operations.
535-
- JsonSchemaDialect + BaseUri in document.
536-
- Copy constructors are gone, use shallow copy method.
537-
- Multiple methods that should have been internal have been changed from public to private link OpenApiLink.SerializeAsV3WithoutReference.
538-
- duplicated _style property on parameter was removed.
539-
- discriminator now uses references.
540-
- ValidationRuleSet now accepts a key?
677+
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)