Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Microsoft.OpenApi/Models/OpenApiConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,11 @@ public static class OpenApiConstants
/// </summary>
public const string Wrapped = "wrapped";

/// <summary>
/// Field: NodeType
/// </summary>
public const string NodeType = "nodeType";

/// <summary>
/// Field: In
/// </summary>
Expand Down
58 changes: 49 additions & 9 deletions src/Microsoft.OpenApi/Models/OpenApiXml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,40 @@ public class OpenApiXml : IOpenApiSerializable, IOpenApiExtensible
/// Declares whether the property definition translates to an attribute instead of an element.
/// Default value is false.
/// </summary>
public bool Attribute { get; set; }
[Obsolete("Use NodeType property instead. This property will be removed in a future version.")]
internal bool Attribute
{
get
{
return NodeType == OpenApiXmlNodeType.Attribute;
}
set
{
NodeType = value ? OpenApiXmlNodeType.Attribute : OpenApiXmlNodeType.None;
}
}

/// <summary>
/// Signifies whether the array is wrapped.
/// Default value is false.
/// </summary>
public bool Wrapped { get; set; }
[Obsolete("Use NodeType property instead. This property will be removed in a future version.")]
internal bool Wrapped
{
get
{
return NodeType == OpenApiXmlNodeType.Element;
}
set
{
NodeType = value ? OpenApiXmlNodeType.Element : OpenApiXmlNodeType.None;
}
}

/// <summary>
/// The node type of the XML representation.
/// </summary>
public OpenApiXmlNodeType? NodeType { get; set; }

/// <summary>
/// Specification Extensions.
Expand All @@ -56,8 +83,7 @@ public OpenApiXml(OpenApiXml xml)
Name = xml?.Name ?? Name;
Namespace = xml?.Namespace ?? Namespace;
Prefix = xml?.Prefix ?? Prefix;
Attribute = xml?.Attribute ?? Attribute;
Wrapped = xml?.Wrapped ?? Wrapped;
NodeType = xml?.NodeType ?? NodeType;
Extensions = xml?.Extensions != null ? new Dictionary<string, IOpenApiExtension>(xml.Extensions) : null;
}

Expand Down Expand Up @@ -108,11 +134,25 @@ private void Write(IOpenApiWriter writer, OpenApiSpecVersion specVersion)
// prefix
writer.WriteProperty(OpenApiConstants.Prefix, Prefix);

// attribute
writer.WriteProperty(OpenApiConstants.Attribute, Attribute, false);

// wrapped
writer.WriteProperty(OpenApiConstants.Wrapped, Wrapped, false);
// For OpenAPI 3.2.0 and above, serialize nodeType
if (specVersion >= OpenApiSpecVersion.OpenApi3_2)
{
if (NodeType.HasValue)
{
writer.WriteProperty(OpenApiConstants.NodeType, NodeType.Value.GetDisplayName());
}
}
else
{
// For OpenAPI 3.1.0 and below, serialize attribute and wrapped
// Use backing fields if they were set via obsolete properties,
// otherwise derive from NodeType if set
var attribute = NodeType.HasValue && NodeType == OpenApiXmlNodeType.Attribute;
var wrapped = NodeType.HasValue && NodeType == OpenApiXmlNodeType.Element;

writer.WriteProperty(OpenApiConstants.Attribute, attribute, false);
writer.WriteProperty(OpenApiConstants.Wrapped, wrapped, false);
}

// extensions
writer.WriteExtensions(Extensions, specVersion);
Expand Down
36 changes: 36 additions & 0 deletions src/Microsoft.OpenApi/Models/OpenApiXmlNodeType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

namespace Microsoft.OpenApi
{
/// <summary>
/// The type of the XML node
/// </summary>
public enum OpenApiXmlNodeType
{
/// <summary>
/// Element node type
/// </summary>
[Display("element")] Element,

/// <summary>
/// Attribute node type
/// </summary>
[Display("attribute")] Attribute,

/// <summary>
/// Text node type
/// </summary>
[Display("text")] Text,

/// <summary>
/// CDATA node type
/// </summary>
[Display("cdata")] Cdata,

/// <summary>
/// None node type
/// </summary>
[Display("none")] None
}
}
4 changes: 4 additions & 0 deletions src/Microsoft.OpenApi/Reader/V2/OpenApiXmlDeserializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ internal static partial class OpenApiV2Deserializer
var attribute = n.GetScalarValue();
if (attribute is not null)
{
#pragma warning disable CS0618 // Type or member is obsolete
o.Attribute = bool.Parse(attribute);
#pragma warning restore CS0618 // Type or member is obsolete
}
}
},
Expand All @@ -53,7 +55,9 @@ internal static partial class OpenApiV2Deserializer
var wrapped = n.GetScalarValue();
if (wrapped is not null)
{
#pragma warning disable CS0618 // Type or member is obsolete
o.Wrapped = bool.Parse(wrapped);
#pragma warning restore CS0618 // Type or member is obsolete
}
}
},
Expand Down
4 changes: 4 additions & 0 deletions src/Microsoft.OpenApi/Reader/V3/OpenApiXmlDeserializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ internal static partial class OpenApiV3Deserializer
var attribute = n.GetScalarValue();
if (attribute is not null)
{
#pragma warning disable CS0618 // Type or member is obsolete
o.Attribute = bool.Parse(attribute);
#pragma warning restore CS0618 // Type or member is obsolete
}
}
},
Expand All @@ -50,7 +52,9 @@ internal static partial class OpenApiV3Deserializer
var wrapped = n.GetScalarValue();
if (wrapped is not null)
{
#pragma warning disable CS0618 // Type or member is obsolete
o.Wrapped = bool.Parse(wrapped);
#pragma warning restore CS0618 // Type or member is obsolete
}
}
},
Expand Down
4 changes: 4 additions & 0 deletions src/Microsoft.OpenApi/Reader/V31/OpenApiXmlDeserializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ internal static partial class OpenApiV31Deserializer
var attribute = n.GetScalarValue();
if (attribute is not null)
{
#pragma warning disable CS0618 // Type or member is obsolete
o.Attribute = bool.Parse(attribute);
#pragma warning restore CS0618 // Type or member is obsolete
}
}
},
Expand All @@ -52,7 +54,9 @@ internal static partial class OpenApiV31Deserializer
var wrapped = n.GetScalarValue();
if (wrapped is not null)
{
#pragma warning disable CS0618 // Type or member is obsolete
o.Wrapped = bool.Parse(wrapped);
#pragma warning restore CS0618 // Type or member is obsolete
}
}
}
Expand Down
19 changes: 4 additions & 15 deletions src/Microsoft.OpenApi/Reader/V32/OpenApiXmlDeserializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,14 @@ internal static partial class OpenApiV32Deserializer
(o, n, _) => o.Prefix = n.GetScalarValue()
},
{
"attribute",
"nodeType",
(o, n, _) =>
{
var attribute = n.GetScalarValue();
if (attribute is not null)
if (!n.GetScalarValue().TryGetEnumFromDisplayName<OpenApiXmlNodeType>(n.Context, out var nodeType))
{
o.Attribute = bool.Parse(attribute);
}
}
},
{
"wrapped",
(o, n, _) =>
{
var wrapped = n.GetScalarValue();
if (wrapped is not null)
{
o.Wrapped = bool.Parse(wrapped);
return;
}
o.NodeType = nodeType;
}
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public async Task ParseBasicXmlShouldSucceed()
var xml = await OpenApiModelFactory.LoadAsync<OpenApiXml>(Resources.GetStream(Path.Combine(SampleFolderPath, "basicXml.yaml")), OpenApiSpecVersion.OpenApi3_0, new(), settings: SettingsFixture.ReaderSettings);

// Assert
#pragma warning disable CS0618 // Type or member is obsolete
Assert.Equivalent(
new OpenApiXml
{
Expand All @@ -29,6 +30,7 @@ public async Task ParseBasicXmlShouldSucceed()
Prefix = "samplePrefix",
Wrapped = true
}, xml);
#pragma warning restore CS0618 // Type or member is obsolete
}
}
}
Loading