-
Notifications
You must be signed in to change notification settings - Fork 279
Add JSON Schema 2020-12 metadata annotations to OpenApiSchemaReference #2376
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
ed6df9f
Initial plan for issue
Copilot 7967256
Initial commit - fix build errors with string.Split ambiguity
Copilot 73ec5a7
Add metadata annotations to OpenApiSchemaReference
Copilot 66ccf74
Complete OpenApiSchemaReference annotations implementation
Copilot 10cde39
Refactor schema metadata annotations to separate class per review fee…
Copilot b328774
chore: reverts useless changes from copilot
baywet ddf17fe
chore: linting
baywet f0802e5
fix: makes reference serialization object generic
baywet ffb083c
chore: cleans up interface definitions
baywet 4139170
chore: fix implementation type definition
baywet 0a686fd
chore: fixes the reference copy conundrum
baywet 33cc238
chore: removes summary property from references that do not support it
baywet 03659f7
fix: removes description field from references that do not support it
baywet e355808
chore: updates test validation information
baywet f74afbc
Potential fix for code scanning alert no. 2304: Missed opportunity to…
baywet 18f91d0
chore: Apply suggestions from code review
baywet a37a871
chore: reverts undesired change from copilot
baywet 449ab26
chore: refactoring
baywet 9248560
fix: loading of header reference description
baywet 86892b3
fix: callback reference annotations parsing
baywet 8bf012b
fix: example reference annotation parsing
baywet 2a62c5a
fix: link reference annotations parsing
baywet b1578f3
fix: parameter reference annoation parsing
baywet d31ed4c
fix: path item reference annoations parsing
baywet e455f52
fix: response reference annotations parsing
baywet d9a78dc
fix: request body reference annotations parsing
baywet ccc3733
fix: security scheme reference annoations parsing
baywet 8ed4512
chore: adds unit tests for tags reference parsing
baywet 6e12152
chore: adds a unit test for json schema ref annotations parsing
baywet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
181 changes: 181 additions & 0 deletions
181
src/Microsoft.OpenApi/Models/OpenApiSchemaReferenceInformation.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,181 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT license. | ||
|
|
||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Text.Json.Nodes; | ||
| using Microsoft.OpenApi.Reader; | ||
|
|
||
| namespace Microsoft.OpenApi | ||
| { | ||
| /// <summary> | ||
| /// Schema reference information that includes metadata annotations from JSON Schema 2020-12. | ||
| /// This class extends OpenApiReference to provide schema-specific metadata override capabilities. | ||
| /// </summary> | ||
| public class OpenApiSchemaReferenceInformation : OpenApiReference | ||
| { | ||
| /// <summary> | ||
| /// A default value which by default SHOULD override that of the referenced component. | ||
| /// If the referenced object-type does not allow a default field, then this field has no effect. | ||
| /// </summary> | ||
| public JsonNode? Default { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// A title which by default SHOULD override that of the referenced component. | ||
| /// If the referenced object-type does not allow a title field, then this field has no effect. | ||
| /// </summary> | ||
| public string? Title { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Indicates whether the referenced component is deprecated. | ||
| /// If the referenced object-type does not allow a deprecated field, then this field has no effect. | ||
| /// </summary> | ||
| public bool? Deprecated { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Indicates whether the referenced component is read-only. | ||
| /// If the referenced object-type does not allow a readOnly field, then this field has no effect. | ||
| /// </summary> | ||
| public bool? ReadOnly { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Indicates whether the referenced component is write-only. | ||
| /// If the referenced object-type does not allow a writeOnly field, then this field has no effect. | ||
| /// </summary> | ||
| public bool? WriteOnly { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Example values which by default SHOULD override those of the referenced component. | ||
| /// If the referenced object-type does not allow examples, then this field has no effect. | ||
| /// </summary> | ||
| public IList<JsonNode>? Examples { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Parameterless constructor | ||
| /// </summary> | ||
| public OpenApiSchemaReferenceInformation() { } | ||
|
|
||
| /// <summary> | ||
| /// Initializes a copy instance of the <see cref="OpenApiSchemaReferenceInformation"/> object | ||
| /// </summary> | ||
| public OpenApiSchemaReferenceInformation(OpenApiSchemaReferenceInformation reference) : base(reference) | ||
| { | ||
| Utils.CheckArgumentNull(reference); | ||
| Default = reference.Default; | ||
| Title = reference.Title; | ||
| Deprecated = reference.Deprecated; | ||
| ReadOnly = reference.ReadOnly; | ||
| WriteOnly = reference.WriteOnly; | ||
| Examples = reference.Examples; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Serialize <see cref="OpenApiSchemaReferenceInformation"/> to Open Api v3.1. | ||
| /// </summary> | ||
| public new void SerializeAsV31(IOpenApiWriter writer) | ||
| { | ||
| Utils.CheckArgumentNull(writer); | ||
|
|
||
| if (Type == ReferenceType.Tag && !string.IsNullOrEmpty(ReferenceV3) && ReferenceV3 is not null) | ||
| { | ||
| // Write the string value only | ||
| writer.WriteValue(ReferenceV3); | ||
| return; | ||
| } | ||
|
|
||
| writer.WriteStartObject(); | ||
|
|
||
| // summary and description are in 3.1 but not in 3.0 | ||
| writer.WriteProperty(OpenApiConstants.Summary, Summary); | ||
| writer.WriteProperty(OpenApiConstants.Description, Description); | ||
|
|
||
| // Additional schema metadata annotations in 3.1 | ||
| writer.WriteOptionalObject(OpenApiConstants.Default, Default, (w, d) => w.WriteAny(d)); | ||
| writer.WriteProperty(OpenApiConstants.Title, Title); | ||
| if (Deprecated.HasValue) | ||
| { | ||
| writer.WriteProperty(OpenApiConstants.Deprecated, Deprecated.Value, false); | ||
| } | ||
| if (ReadOnly.HasValue) | ||
| { | ||
| writer.WriteProperty(OpenApiConstants.ReadOnly, ReadOnly.Value, false); | ||
| } | ||
| if (WriteOnly.HasValue) | ||
| { | ||
| writer.WriteProperty(OpenApiConstants.WriteOnly, WriteOnly.Value, false); | ||
| } | ||
| if (Examples != null && Examples.Any()) | ||
| { | ||
| writer.WriteOptionalCollection(OpenApiConstants.Examples, Examples, (w, e) => w.WriteAny(e)); | ||
| } | ||
|
|
||
| // $ref | ||
| writer.WriteProperty(OpenApiConstants.DollarRef, ReferenceV3); | ||
|
|
||
| writer.WriteEndObject(); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Sets metadata fields from a JSON node during parsing | ||
| /// </summary> | ||
| internal new void SetMetadataFromMapNode(MapNode mapNode) | ||
| { | ||
| base.SetMetadataFromMapNode(mapNode); | ||
|
|
||
| if (mapNode.JsonNode is not JsonObject jsonObject) return; | ||
|
|
||
| var title = GetPropertyValueFromNode(jsonObject, OpenApiConstants.Title); | ||
| if (!string.IsNullOrEmpty(title)) | ||
| { | ||
| Title = title; | ||
| } | ||
|
|
||
| // Boolean properties | ||
| if (jsonObject.TryGetPropertyValue(OpenApiConstants.Deprecated, out var deprecatedNode) && deprecatedNode is JsonValue deprecatedValue) | ||
| { | ||
| if (deprecatedValue.TryGetValue<bool>(out var deprecated)) | ||
| { | ||
| Deprecated = deprecated; | ||
| } | ||
| } | ||
|
|
||
| if (jsonObject.TryGetPropertyValue(OpenApiConstants.ReadOnly, out var readOnlyNode) && readOnlyNode is JsonValue readOnlyValue) | ||
| { | ||
| if (readOnlyValue.TryGetValue<bool>(out var readOnly)) | ||
| { | ||
| ReadOnly = readOnly; | ||
| } | ||
| } | ||
|
|
||
| if (jsonObject.TryGetPropertyValue(OpenApiConstants.WriteOnly, out var writeOnlyNode) && writeOnlyNode is JsonValue writeOnlyValue) | ||
| { | ||
| if (writeOnlyValue.TryGetValue<bool>(out var writeOnly)) | ||
| { | ||
| WriteOnly = writeOnly; | ||
| } | ||
| } | ||
|
|
||
| // Default value | ||
| if (jsonObject.TryGetPropertyValue(OpenApiConstants.Default, out var defaultNode)) | ||
| { | ||
| Default = defaultNode; | ||
| } | ||
|
|
||
| // Examples | ||
| if (jsonObject.TryGetPropertyValue(OpenApiConstants.Examples, out var examplesNode) && examplesNode is JsonArray examplesArray) | ||
| { | ||
| Examples = new List<JsonNode>(); | ||
| foreach (var example in examplesArray) | ||
| { | ||
| if (example != null) | ||
| { | ||
| Examples.Add(example); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static string? GetPropertyValueFromNode(JsonObject jsonObject, string key) => | ||
| jsonObject.TryGetPropertyValue(key, out var valueNode) && valueNode is JsonValue valueCast && valueCast.TryGetValue<string>(out var strValue) ? strValue : null; | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.