@@ -379,6 +379,32 @@ public void SerializeAsV3WithoutReference(IOpenApiWriter writer)
379379 /// Serialize <see cref="OpenApiSchema"/> to Open Api v2.0
380380 /// </summary>
381381 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 )
382408 {
383409 if ( writer == null )
384410 {
@@ -391,16 +417,28 @@ public void SerializeAsV2(IOpenApiWriter writer)
391417 return ;
392418 }
393419
394- SerializeAsV2WithoutReference ( writer ) ;
420+ if ( parentRequiredProperties == null )
421+ {
422+ parentRequiredProperties = new List < string > ( ) ;
423+ }
424+
425+ SerializeAsV2WithoutReference ( writer , parentRequiredProperties , propertyName ) ;
395426 }
396427
397428 /// <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.
399431 /// </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 )
401439 {
402440 writer . WriteStartObject ( ) ;
403- WriteAsSchemaProperties ( writer ) ;
441+ WriteAsSchemaProperties ( writer , parentRequiredProperties , propertyName ) ;
404442 writer . WriteEndObject ( ) ;
405443 }
406444
@@ -468,7 +506,10 @@ internal void WriteAsItemsProperties(IOpenApiWriter writer)
468506 writer . WriteExtensions ( Extensions ) ;
469507 }
470508
471- internal void WriteAsSchemaProperties ( IOpenApiWriter writer )
509+ internal void WriteAsSchemaProperties (
510+ IOpenApiWriter writer ,
511+ IList < string > parentRequiredProperties ,
512+ string propertyName )
472513 {
473514 if ( writer == null )
474515 {
@@ -542,7 +583,8 @@ internal void WriteAsSchemaProperties(IOpenApiWriter writer)
542583 writer . WriteOptionalCollection ( OpenApiConstants . AllOf , AllOf , ( w , s ) => s . SerializeAsV2 ( w ) ) ;
543584
544585 // 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 ) ) ;
546588
547589 // additionalProperties
548590 writer . WriteOptionalObject (
@@ -554,7 +596,12 @@ internal void WriteAsSchemaProperties(IOpenApiWriter writer)
554596 writer . WriteProperty ( OpenApiConstants . Discriminator , Discriminator ? . PropertyName ) ;
555597
556598 // 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+ }
558605
559606 // xml
560607 writer . WriteOptionalObject ( OpenApiConstants . Xml , Xml , ( w , s ) => s . SerializeAsV2 ( w ) ) ;
0 commit comments