File tree Expand file tree Collapse file tree 2 files changed +28
-4
lines changed
src/Microsoft.OpenApi/Models
test/Microsoft.OpenApi.Tests/Models Expand file tree Collapse file tree 2 files changed +28
-4
lines changed Original file line number Diff line number Diff line change @@ -785,9 +785,15 @@ private void SerializeAsV2(
785785 } ) ;
786786
787787 // additionalProperties
788- // a schema cannot be serialized in v2
789788 // true is the default, no need to write it out
790- if ( ! AdditionalPropertiesAllowed )
789+ if ( AdditionalProperties is not null )
790+ {
791+ writer . WriteOptionalObject (
792+ OpenApiConstants . AdditionalProperties ,
793+ AdditionalProperties ,
794+ ( w , s ) => s . SerializeAsV2 ( w ) ) ;
795+ }
796+ else if ( ! AdditionalPropertiesAllowed )
791797 {
792798 writer . WriteProperty ( OpenApiConstants . AdditionalProperties , AdditionalPropertiesAllowed ) ;
793799 }
Original file line number Diff line number Diff line change @@ -718,9 +718,9 @@ public async Task SerializeConstAsEnumV20()
718718 }
719719
720720 [ Fact ]
721- public async Task SerializeAdditionalPropertiesAsV2DoesNotEmit ( )
721+ public async Task SerializeAdditionalPropertiesAsV2WithEmptySchemaEmits ( )
722722 {
723- var expected = @"{ }" ;
723+ var expected = @"{ ""additionalProperties"": { } }" ;
724724 // Given
725725 var schema = new OpenApiSchema
726726 {
@@ -734,6 +734,24 @@ public async Task SerializeAdditionalPropertiesAsV2DoesNotEmit()
734734 Assert . True ( JsonNode . DeepEquals ( JsonNode . Parse ( expected ) , JsonNode . Parse ( actual ) ) ) ;
735735 }
736736
737+ [ Fact ]
738+ public async Task SerializeAdditionalPropertiesAsV2WithRefSchemaEmits ( )
739+ {
740+ var expected = @"{ ""type"": ""object"", ""additionalProperties"": { ""$ref"": ""#/definitions/MyModel"" } }" ;
741+ // Given - schema with additionalProperties pointing to a ref (dictionary case)
742+ var schema = new OpenApiSchema
743+ {
744+ Type = JsonSchemaType . Object ,
745+ AdditionalProperties = new OpenApiSchemaReference ( "MyModel" , null )
746+ } ;
747+
748+ // When
749+ var actual = await schema . SerializeAsJsonAsync ( OpenApiSpecVersion . OpenApi2_0 ) ;
750+
751+ // Then
752+ Assert . True ( JsonNode . DeepEquals ( JsonNode . Parse ( expected ) , JsonNode . Parse ( actual ) ) ) ;
753+ }
754+
737755 [ Fact ]
738756 public async Task SerializeAdditionalPropertiesAllowedAsV2DefaultDoesNotEmit ( )
739757 {
You can’t perform that action at this time.
0 commit comments