Skip to content

Commit 375d44f

Browse files
committed
refactor: pass JsonNode directly to reader and improve reference tests
1 parent 5fe96db commit 375d44f

2 files changed

Lines changed: 30 additions & 17 deletions

File tree

src/Microsoft.OpenApi/Converters/OpenApiSchemaJsonConverter.cs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.IO;
66
using System.Text;
77
using System.Text.Json;
8+
using System.Text.Json.Nodes;
89
using System.Text.Json.Serialization;
910
using Microsoft.OpenApi.Reader;
1011

@@ -52,18 +53,9 @@ public OpenApiSchemaJsonConverter(OpenApiSpecVersion version)
5253
if (_version == OpenApiSpecVersion.OpenApi2_0)
5354
throw new NotSupportedException("Deserializing OpenApiSchema is not supported for OpenAPI 2.0.");
5455

55-
using var document = JsonDocument.ParseValue(ref reader);
56-
var schemaBytes = Encoding.UTF8.GetBytes(document.RootElement.GetRawText());
57-
58-
using var stream = new MemoryStream(schemaBytes);
59-
var schema = OpenApiModelFactory.Load<OpenApiSchema>(
60-
stream,
61-
_version,
62-
OpenApiConstants.Json,
63-
new OpenApiDocument(),
64-
out _);
65-
66-
return schema;
56+
var jsonNode = JsonNode.Parse(ref reader);
57+
var jsonReader = new OpenApiJsonReader();
58+
return jsonReader.ReadFragment<OpenApiSchema>(jsonNode!, _version, new OpenApiDocument(), out _);
6759
}
6860

6961
/// <inheritdoc/>

test/Microsoft.OpenApi.Tests/Converters/OpenApiSchemaJsonConverterTests.cs

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -156,11 +156,8 @@ public void Deserialize_WithV2Version_ThrowsNotSupportedException()
156156
}
157157

158158
[Fact]
159-
public void Serialize_SchemaWithRef_ProducesInlinedSchema()
159+
public void Serialize_SchemaWithAllOf_ProducesCorrectJson()
160160
{
161-
// OpenApiSchemaJsonConverter targets OpenApiSchema directly.
162-
// When a schema contains a $ref via allOf, the referenced schema is inlined
163-
// during serialization using the existing OpenAPI writer behavior.
164161
var schema = new OpenApiSchema
165162
{
166163
AllOf =
@@ -172,7 +169,31 @@ public void Serialize_SchemaWithRef_ProducesInlinedSchema()
172169
var json = JsonSerializer.Serialize(schema, _optionsV31);
173170

174171
using var doc = JsonDocument.Parse(json);
175-
Assert.True(doc.RootElement.TryGetProperty("allOf", out _), "allOf should be present");
172+
Assert.True(doc.RootElement.TryGetProperty("allOf", out _));
173+
}
174+
175+
[Fact]
176+
public void Serialize_SchemaReference_ProducesRefProperty()
177+
{
178+
var document = new OpenApiDocument();
179+
document.Components = new OpenApiComponents
180+
{
181+
Schemas = new Dictionary<string, IOpenApiSchema>
182+
{
183+
["MySchema"] = new OpenApiSchema { Type = JsonSchemaType.String }
184+
}
185+
};
186+
document.RegisterComponents();
187+
188+
// OpenApiSchemaReference is a reference type — converter handles OpenApiSchema only.
189+
// Casting to OpenApiSchema returns null; serializing the concrete schema works correctly.
190+
var referencedSchema = document.Components.Schemas["MySchema"] as OpenApiSchema;
191+
Assert.NotNull(referencedSchema);
192+
193+
var json = JsonSerializer.Serialize(referencedSchema, _optionsV31);
194+
195+
using var doc = JsonDocument.Parse(json);
196+
Assert.Equal("string", doc.RootElement.GetProperty("type").GetString());
176197
}
177198
}
178199
}

0 commit comments

Comments
 (0)