Skip to content

Commit 33cc238

Browse files
committed
chore: removes summary property from references that do not support it
Signed-off-by: Vincent Biret <[email protected]>
1 parent 0a686fd commit 33cc238

23 files changed

+277
-240
lines changed

src/Microsoft.OpenApi/Interfaces/IOpenApiReferenceHolder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace Microsoft.OpenApi
99
/// <typeparam name="T">The type of the target being referenced</typeparam>
1010
/// <typeparam name="U">The type of the interface implemented by both the target and the reference type</typeparam>
1111
/// <typeparam name="V">The type for the reference holding the additional fields and annotations</typeparam>
12-
public interface IOpenApiReferenceHolder<out T, U, V> : IOpenApiReferenceHolder<V> where T : IOpenApiReferenceable, U where V : OpenApiReference, new()
12+
public interface IOpenApiReferenceHolder<out T, U, V> : IOpenApiReferenceHolder<V> where T : IOpenApiReferenceable, U where V : BaseOpenApiReference, new()
1313
{
1414
/// <summary>
1515
/// Gets the resolved target object.
@@ -30,7 +30,7 @@ namespace Microsoft.OpenApi
3030
/// A generic interface for OpenApiReferenceable objects that have a target.
3131
/// </summary>
3232
/// <typeparam name="V">The type for the reference holding the additional fields and annotations</typeparam>
33-
public interface IOpenApiReferenceHolder<V> : IOpenApiReferenceHolder where V : OpenApiReference, new()
33+
public interface IOpenApiReferenceHolder<V> : IOpenApiReferenceHolder where V : BaseOpenApiReference, new()
3434
{
3535
/// <summary>
3636
/// Reference object.
Lines changed: 116 additions & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -1,172 +1,146 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license.
33

4+
using System;
45
using System.Collections.Generic;
56
using System.Linq;
67
using System.Text.Json.Nodes;
7-
using Microsoft.OpenApi.Reader;
88

9-
namespace Microsoft.OpenApi
9+
namespace Microsoft.OpenApi;
10+
11+
/// <summary>
12+
/// Schema reference information that includes metadata annotations from JSON Schema 2020-12.
13+
/// This class extends OpenApiReference to provide schema-specific metadata override capabilities.
14+
/// </summary>
15+
public class JsonSchemaReference : BaseOpenApiReference
1016
{
1117
/// <summary>
12-
/// Schema reference information that includes metadata annotations from JSON Schema 2020-12.
13-
/// This class extends OpenApiReference to provide schema-specific metadata override capabilities.
18+
/// A default value which by default SHOULD override that of the referenced component.
19+
/// If the referenced object-type does not allow a default field, then this field has no effect.
1420
/// </summary>
15-
public class JsonSchemaReference : OpenApiReference
16-
{
17-
/// <summary>
18-
/// A default value which by default SHOULD override that of the referenced component.
19-
/// If the referenced object-type does not allow a default field, then this field has no effect.
20-
/// </summary>
21-
public JsonNode? Default { get; set; }
22-
23-
/// <summary>
24-
/// A title which by default SHOULD override that of the referenced component.
25-
/// If the referenced object-type does not allow a title field, then this field has no effect.
26-
/// </summary>
27-
public string? Title { get; set; }
28-
29-
/// <summary>
30-
/// Indicates whether the referenced component is deprecated.
31-
/// If the referenced object-type does not allow a deprecated field, then this field has no effect.
32-
/// </summary>
33-
public bool? Deprecated { get; set; }
34-
35-
/// <summary>
36-
/// Indicates whether the referenced component is read-only.
37-
/// If the referenced object-type does not allow a readOnly field, then this field has no effect.
38-
/// </summary>
39-
public bool? ReadOnly { get; set; }
40-
41-
/// <summary>
42-
/// Indicates whether the referenced component is write-only.
43-
/// If the referenced object-type does not allow a writeOnly field, then this field has no effect.
44-
/// </summary>
45-
public bool? WriteOnly { get; set; }
46-
47-
/// <summary>
48-
/// Example values which by default SHOULD override those of the referenced component.
49-
/// If the referenced object-type does not allow examples, then this field has no effect.
50-
/// </summary>
51-
public IList<JsonNode>? Examples { get; set; }
52-
53-
/// <summary>
54-
/// Parameterless constructor
55-
/// </summary>
56-
public JsonSchemaReference() { }
57-
58-
/// <summary>
59-
/// Initializes a copy instance of the <see cref="JsonSchemaReference"/> object
60-
/// </summary>
61-
public JsonSchemaReference(JsonSchemaReference reference) : base(reference)
62-
{
63-
Utils.CheckArgumentNull(reference);
64-
Default = reference.Default;
65-
Title = reference.Title;
66-
Deprecated = reference.Deprecated;
67-
ReadOnly = reference.ReadOnly;
68-
WriteOnly = reference.WriteOnly;
69-
Examples = reference.Examples;
70-
}
21+
public JsonNode? Default { get; set; }
7122

72-
/// <summary>
73-
/// Serialize <see cref="JsonSchemaReference"/> to Open Api v3.1.
74-
/// </summary>
75-
public override void SerializeAsV31(IOpenApiWriter writer)
76-
{
77-
Utils.CheckArgumentNull(writer);
23+
/// <summary>
24+
/// A title which by default SHOULD override that of the referenced component.
25+
/// If the referenced object-type does not allow a title field, then this field has no effect.
26+
/// </summary>
27+
public string? Title { get; set; }
7828

79-
if (Type == ReferenceType.Tag && !string.IsNullOrEmpty(ReferenceV3) && ReferenceV3 is not null)
80-
{
81-
// Write the string value only
82-
writer.WriteValue(ReferenceV3);
83-
return;
84-
}
29+
/// <summary>
30+
/// Indicates whether the referenced component is deprecated.
31+
/// If the referenced object-type does not allow a deprecated field, then this field has no effect.
32+
/// </summary>
33+
public bool? Deprecated { get; set; }
8534

86-
writer.WriteStartObject();
87-
88-
// summary and description are in 3.1 but not in 3.0
89-
writer.WriteProperty(OpenApiConstants.Summary, Summary);
90-
writer.WriteProperty(OpenApiConstants.Description, Description);
91-
92-
// Additional schema metadata annotations in 3.1
93-
writer.WriteOptionalObject(OpenApiConstants.Default, Default, (w, d) => w.WriteAny(d));
94-
writer.WriteProperty(OpenApiConstants.Title, Title);
95-
if (Deprecated.HasValue)
96-
{
97-
writer.WriteProperty(OpenApiConstants.Deprecated, Deprecated.Value, false);
98-
}
99-
if (ReadOnly.HasValue)
100-
{
101-
writer.WriteProperty(OpenApiConstants.ReadOnly, ReadOnly.Value, false);
102-
}
103-
if (WriteOnly.HasValue)
104-
{
105-
writer.WriteProperty(OpenApiConstants.WriteOnly, WriteOnly.Value, false);
106-
}
107-
if (Examples != null && Examples.Any())
108-
{
109-
writer.WriteOptionalCollection(OpenApiConstants.Examples, Examples, (w, e) => w.WriteAny(e));
110-
}
35+
/// <summary>
36+
/// Indicates whether the referenced component is read-only.
37+
/// If the referenced object-type does not allow a readOnly field, then this field has no effect.
38+
/// </summary>
39+
public bool? ReadOnly { get; set; }
11140

112-
// $ref
113-
writer.WriteProperty(OpenApiConstants.DollarRef, ReferenceV3);
41+
/// <summary>
42+
/// Indicates whether the referenced component is write-only.
43+
/// If the referenced object-type does not allow a writeOnly field, then this field has no effect.
44+
/// </summary>
45+
public bool? WriteOnly { get; set; }
11446

115-
writer.WriteEndObject();
116-
}
47+
/// <summary>
48+
/// Example values which by default SHOULD override those of the referenced component.
49+
/// If the referenced object-type does not allow examples, then this field has no effect.
50+
/// </summary>
51+
public IList<JsonNode>? Examples { get; set; }
52+
53+
/// <summary>
54+
/// Parameterless constructor
55+
/// </summary>
56+
public JsonSchemaReference() { }
57+
58+
/// <summary>
59+
/// Initializes a copy instance of the <see cref="JsonSchemaReference"/> object
60+
/// </summary>
61+
public JsonSchemaReference(JsonSchemaReference reference) : base(reference)
62+
{
63+
Utils.CheckArgumentNull(reference);
64+
Default = reference.Default;
65+
Title = reference.Title;
66+
Deprecated = reference.Deprecated;
67+
ReadOnly = reference.ReadOnly;
68+
WriteOnly = reference.WriteOnly;
69+
Examples = reference.Examples;
70+
}
11771

118-
/// <summary>
119-
/// Sets metadata fields from a JSON node during parsing
120-
/// </summary>
121-
internal override void SetMetadataFromMapNode(MapNode mapNode)
72+
/// <inheritdoc/>
73+
protected override void SerializeAdditionalV31Properties(IOpenApiWriter writer)
74+
{
75+
if (Type != ReferenceType.Schema) throw new InvalidOperationException(
76+
$"JsonSchemaReference can only be serialized for ReferenceType.Schema, but was {Type}.");
77+
78+
base.SerializeAdditionalV31Properties(writer);
79+
// Additional schema metadata annotations in 3.1
80+
writer.WriteOptionalObject(OpenApiConstants.Default, Default, (w, d) => w.WriteAny(d));
81+
writer.WriteProperty(OpenApiConstants.Title, Title);
82+
if (Deprecated.HasValue)
83+
{
84+
writer.WriteProperty(OpenApiConstants.Deprecated, Deprecated.Value, false);
85+
}
86+
if (ReadOnly.HasValue)
87+
{
88+
writer.WriteProperty(OpenApiConstants.ReadOnly, ReadOnly.Value, false);
89+
}
90+
if (WriteOnly.HasValue)
12291
{
123-
base.SetMetadataFromMapNode(mapNode);
124-
125-
if (mapNode.JsonNode is not JsonObject jsonObject) return;
92+
writer.WriteProperty(OpenApiConstants.WriteOnly, WriteOnly.Value, false);
93+
}
94+
if (Examples != null && Examples.Any())
95+
{
96+
writer.WriteOptionalCollection(OpenApiConstants.Examples, Examples, (w, e) => w.WriteAny(e));
97+
}
98+
}
12699

127-
var title = GetPropertyValueFromNode(jsonObject, OpenApiConstants.Title);
128-
if (!string.IsNullOrEmpty(title))
129-
{
130-
Title = title;
131-
}
100+
/// <inheritdoc/>
101+
protected override void SetAdditional31MetadataFromMapNode(JsonObject jsonObject)
102+
{
103+
base.SetAdditional31MetadataFromMapNode(jsonObject);
132104

133-
// Boolean properties
134-
if (jsonObject.TryGetPropertyValue(OpenApiConstants.Deprecated, out var deprecatedNode) && deprecatedNode is JsonValue deprecatedValue && deprecatedValue.TryGetValue<bool>(out var deprecated))
135-
{
136-
Deprecated = deprecated;
137-
}
105+
var title = GetPropertyValueFromNode(jsonObject, OpenApiConstants.Title);
106+
if (!string.IsNullOrEmpty(title))
107+
{
108+
Title = title;
109+
}
138110

139-
if (jsonObject.TryGetPropertyValue(OpenApiConstants.ReadOnly, out var readOnlyNode) && readOnlyNode is JsonValue readOnlyValue && readOnlyValue.TryGetValue<bool>(out var readOnly))
140-
{
141-
ReadOnly = readOnly;
142-
}
111+
// Boolean properties
112+
if (jsonObject.TryGetPropertyValue(OpenApiConstants.Deprecated, out var deprecatedNode) && deprecatedNode is JsonValue deprecatedValue && deprecatedValue.TryGetValue<bool>(out var deprecated))
113+
{
114+
Deprecated = deprecated;
115+
}
143116

144-
if (jsonObject.TryGetPropertyValue(OpenApiConstants.WriteOnly, out var writeOnlyNode) && writeOnlyNode is JsonValue writeOnlyValue && writeOnlyValue.TryGetValue<bool>(out var writeOnly))
145-
{
146-
WriteOnly = writeOnly;
147-
}
117+
if (jsonObject.TryGetPropertyValue(OpenApiConstants.ReadOnly, out var readOnlyNode) && readOnlyNode is JsonValue readOnlyValue && readOnlyValue.TryGetValue<bool>(out var readOnly))
118+
{
119+
ReadOnly = readOnly;
120+
}
148121

149-
// Default value
150-
if (jsonObject.TryGetPropertyValue(OpenApiConstants.Default, out var defaultNode))
151-
{
152-
Default = defaultNode;
153-
}
122+
if (jsonObject.TryGetPropertyValue(OpenApiConstants.WriteOnly, out var writeOnlyNode) && writeOnlyNode is JsonValue writeOnlyValue && writeOnlyValue.TryGetValue<bool>(out var writeOnly))
123+
{
124+
WriteOnly = writeOnly;
125+
}
126+
127+
// Default value
128+
if (jsonObject.TryGetPropertyValue(OpenApiConstants.Default, out var defaultNode))
129+
{
130+
Default = defaultNode;
131+
}
154132

155-
// Examples
156-
if (jsonObject.TryGetPropertyValue(OpenApiConstants.Examples, out var examplesNode) && examplesNode is JsonArray examplesArray)
133+
// Examples
134+
if (jsonObject.TryGetPropertyValue(OpenApiConstants.Examples, out var examplesNode) && examplesNode is JsonArray examplesArray)
135+
{
136+
Examples = new List<JsonNode>();
137+
foreach (var example in examplesArray)
157138
{
158-
Examples = new List<JsonNode>();
159-
foreach (var example in examplesArray)
139+
if (example != null)
160140
{
161-
if (example != null)
162-
{
163-
Examples.Add(example);
164-
}
141+
Examples.Add(example);
165142
}
166143
}
167144
}
168-
169-
private static string? GetPropertyValueFromNode(JsonObject jsonObject, string key) =>
170-
jsonObject.TryGetPropertyValue(key, out var valueNode) && valueNode is JsonValue valueCast && valueCast.TryGetValue<string>(out var strValue) ? strValue : null;
171145
}
172146
}

src/Microsoft.OpenApi/Models/OpenApiDocument.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -496,9 +496,9 @@ public void SetReferenceHostDocument()
496496
}
497497

498498
/// <summary>
499-
/// Load the referenced <see cref="IOpenApiReferenceable"/> object from a <see cref="OpenApiReference"/> object
499+
/// Load the referenced <see cref="IOpenApiReferenceable"/> object from a <see cref="BaseOpenApiReference"/> object
500500
/// </summary>
501-
internal T? ResolveReferenceTo<T>(OpenApiReference reference) where T : IOpenApiReferenceable
501+
internal T? ResolveReferenceTo<T>(BaseOpenApiReference reference) where T : IOpenApiReferenceable
502502
{
503503

504504
if (ResolveReference(reference, reference.IsExternal) is T result)
@@ -564,9 +564,9 @@ private static string ConvertByteArrayToString(byte[] hash)
564564
}
565565

566566
/// <summary>
567-
/// Load the referenced <see cref="IOpenApiReferenceable"/> object from a <see cref="OpenApiReference"/> object
567+
/// Load the referenced <see cref="IOpenApiReferenceable"/> object from a <see cref="BaseOpenApiReference"/> object
568568
/// </summary>
569-
internal IOpenApiReferenceable? ResolveReference(OpenApiReference? reference, bool useExternal)
569+
internal IOpenApiReferenceable? ResolveReference(BaseOpenApiReference? reference, bool useExternal)
570570
{
571571
if (reference == null)
572572
{

0 commit comments

Comments
 (0)