Skip to content

Commit e6fdb02

Browse files
authored
Merge pull request #528 from WillGunn/AllowWriteOpenApiStringAsRaw
Allow OpenApiString's raw value to be written by the OpenApiWriter
2 parents 9e45713 + 54554b3 commit e6fdb02

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

src/Microsoft.OpenApi/Any/OpenApiPrimitive.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,10 @@ public void Write(IOpenApiWriter writer, OpenApiSpecVersion specVersion)
7070

7171
case PrimitiveType.String:
7272
var stringValue = (OpenApiString)(IOpenApiPrimitive)this;
73-
writer.WriteValue(stringValue.Value);
73+
if (stringValue.IsRawString())
74+
writer.WriteRaw(stringValue.Value);
75+
else
76+
writer.WriteValue(stringValue.Value);
7477
break;
7578

7679
case PrimitiveType.Byte:

src/Microsoft.OpenApi/Any/OpenApiString.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ namespace Microsoft.OpenApi.Any
99
public class OpenApiString : OpenApiPrimitive<string>
1010
{
1111
private bool isExplicit;
12+
private bool isRawString;
1213

1314
/// <summary>
1415
/// Initializes the <see cref="OpenApiString"/> class.
@@ -30,6 +31,19 @@ public OpenApiString(string value, bool isExplicit)
3031
this.isExplicit = isExplicit;
3132
}
3233

34+
/// <summary>
35+
/// Initializes the <see cref="OpenApiString"/> class.
36+
/// </summary>
37+
/// <param name="value"></param>
38+
/// <param name="isExplicit">Used to indicate if a string is quoted.</param>
39+
/// <param name="isRawString">Used to indicate to the writer that the value should be written without encoding.</param>
40+
public OpenApiString(string value, bool isExplicit, bool isRawString)
41+
: base(value)
42+
{
43+
this.isExplicit = isExplicit;
44+
this.isRawString = isRawString;
45+
}
46+
3347
/// <summary>
3448
/// The primitive class this object represents.
3549
/// </summary>
@@ -42,5 +56,13 @@ public bool IsExplicit()
4256
{
4357
return this.isExplicit;
4458
}
59+
60+
/// <summary>
61+
/// True if the writer should process the value as supplied without encoding.
62+
/// </summary>
63+
public bool IsRawString()
64+
{
65+
return this.isRawString;
66+
}
4567
}
4668
}

test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,10 @@ namespace Microsoft.OpenApi.Any
9999
{
100100
public OpenApiString(string value) { }
101101
public OpenApiString(string value, bool isExplicit) { }
102+
public OpenApiString(string value, bool isExplicit, bool isRawString) { }
102103
public override Microsoft.OpenApi.Any.PrimitiveType PrimitiveType { get; }
103104
public bool IsExplicit() { }
105+
public bool IsRawString() { }
104106
}
105107
public enum PrimitiveType
106108
{

0 commit comments

Comments
 (0)