@@ -379,6 +379,32 @@ public void SerializeAsV3WithoutReference(IOpenApiWriter writer)
379
379
/// Serialize <see cref="OpenApiSchema"/> to Open Api v2.0
380
380
/// </summary>
381
381
public void SerializeAsV2 ( IOpenApiWriter writer )
382
+ {
383
+ SerializeAsV2 ( writer : writer , parentRequiredProperties : new List < string > ( ) , propertyName : null ) ;
384
+ }
385
+
386
+ /// <summary>
387
+ /// Serialize to OpenAPI V2 document without using reference.
388
+ /// </summary>
389
+ public void SerializeAsV2WithoutReference ( IOpenApiWriter writer )
390
+ {
391
+ SerializeAsV2WithoutReference (
392
+ writer : writer ,
393
+ parentRequiredProperties : new List < string > ( ) ,
394
+ propertyName : null ) ;
395
+ }
396
+
397
+ /// <summary>
398
+ /// Serialize <see cref="OpenApiSchema"/> to Open Api v2.0 and handles not marking the provided property
399
+ /// as readonly if its included in the provided list of required properties of parent schema.
400
+ /// </summary>
401
+ /// <param name="writer">The open api writer.</param>
402
+ /// <param name="parentRequiredProperties">The list of required properties in parent schema.</param>
403
+ /// <param name="propertyName">The property name that will be serialized.</param>
404
+ internal void SerializeAsV2 (
405
+ IOpenApiWriter writer ,
406
+ IList < string > parentRequiredProperties ,
407
+ string propertyName )
382
408
{
383
409
if ( writer == null )
384
410
{
@@ -391,16 +417,28 @@ public void SerializeAsV2(IOpenApiWriter writer)
391
417
return ;
392
418
}
393
419
394
- SerializeAsV2WithoutReference ( writer ) ;
420
+ if ( parentRequiredProperties == null )
421
+ {
422
+ parentRequiredProperties = new List < string > ( ) ;
423
+ }
424
+
425
+ SerializeAsV2WithoutReference ( writer , parentRequiredProperties , propertyName ) ;
395
426
}
396
427
397
428
/// <summary>
398
- /// Serialize to OpenAPI V2 document without using reference.
429
+ /// Serialize to OpenAPI V2 document without using reference and handles not marking the provided property
430
+ /// as readonly if its included in the provided list of required properties of parent schema.
399
431
/// </summary>
400
- public void SerializeAsV2WithoutReference ( IOpenApiWriter writer )
432
+ /// <param name="writer">The open api writer.</param>
433
+ /// <param name="parentRequiredProperties">The list of required properties in parent schema.</param>
434
+ /// <param name="propertyName">The property name that will be serialized.</param>
435
+ internal void SerializeAsV2WithoutReference (
436
+ IOpenApiWriter writer ,
437
+ IList < string > parentRequiredProperties ,
438
+ string propertyName )
401
439
{
402
440
writer . WriteStartObject ( ) ;
403
- WriteAsSchemaProperties ( writer ) ;
441
+ WriteAsSchemaProperties ( writer , parentRequiredProperties , propertyName ) ;
404
442
writer . WriteEndObject ( ) ;
405
443
}
406
444
@@ -468,7 +506,10 @@ internal void WriteAsItemsProperties(IOpenApiWriter writer)
468
506
writer . WriteExtensions ( Extensions ) ;
469
507
}
470
508
471
- internal void WriteAsSchemaProperties ( IOpenApiWriter writer )
509
+ internal void WriteAsSchemaProperties (
510
+ IOpenApiWriter writer ,
511
+ IList < string > parentRequiredProperties ,
512
+ string propertyName )
472
513
{
473
514
if ( writer == null )
474
515
{
@@ -542,7 +583,8 @@ internal void WriteAsSchemaProperties(IOpenApiWriter writer)
542
583
writer . WriteOptionalCollection ( OpenApiConstants . AllOf , AllOf , ( w , s ) => s . SerializeAsV2 ( w ) ) ;
543
584
544
585
// properties
545
- writer . WriteOptionalMap ( OpenApiConstants . Properties , Properties , ( w , s ) => s . SerializeAsV2 ( w ) ) ;
586
+ writer . WriteOptionalMap ( OpenApiConstants . Properties , Properties , ( w , key , s ) =>
587
+ s . SerializeAsV2 ( w , Required , key ) ) ;
546
588
547
589
// additionalProperties
548
590
writer . WriteOptionalObject (
@@ -554,7 +596,12 @@ internal void WriteAsSchemaProperties(IOpenApiWriter writer)
554
596
writer . WriteProperty ( OpenApiConstants . Discriminator , Discriminator ? . PropertyName ) ;
555
597
556
598
// readOnly
557
- writer . WriteProperty ( OpenApiConstants . ReadOnly , ReadOnly , false ) ;
599
+ // In V2 schema if a property is part of required properties of parent schema,
600
+ // it cannot be marked as readonly.
601
+ if ( ! parentRequiredProperties . Contains ( propertyName ) )
602
+ {
603
+ writer . WriteProperty ( name : OpenApiConstants . ReadOnly , value : ReadOnly , defaultValue : false ) ;
604
+ }
558
605
559
606
// xml
560
607
writer . WriteOptionalObject ( OpenApiConstants . Xml , Xml , ( w , s ) => s . SerializeAsV2 ( w ) ) ;
0 commit comments