@@ -374,6 +374,32 @@ public void SerializeAsV3WithoutReference(IOpenApiWriter writer)
374374 /// Serialize <see cref="OpenApiSchema"/> to Open Api v2.0
375375 /// </summary>
376376 public void SerializeAsV2 ( IOpenApiWriter writer )
377+ {
378+ SerializeAsV2 ( writer : writer , parentRequiredProperties : new List < string > ( ) , propertyName : null ) ;
379+ }
380+
381+ /// <summary>
382+ /// Serialize to OpenAPI V2 document without using reference.
383+ /// </summary>
384+ public void SerializeAsV2WithoutReference ( IOpenApiWriter writer )
385+ {
386+ SerializeAsV2WithoutReference (
387+ writer : writer ,
388+ parentRequiredProperties : new List < string > ( ) ,
389+ propertyName : null ) ;
390+ }
391+
392+ /// <summary>
393+ /// Serialize <see cref="OpenApiSchema"/> to Open Api v2.0 and handles not marking the provided property
394+ /// as readonly if its included in the provided list of required properties of parent schema.
395+ /// </summary>
396+ /// <param name="writer">The open api writer.</param>
397+ /// <param name="parentRequiredProperties">The list of required properties in parent schema.</param>
398+ /// <param name="propertyName">The property name that will be serialized.</param>
399+ internal void SerializeAsV2 (
400+ IOpenApiWriter writer ,
401+ IList < string > parentRequiredProperties ,
402+ string propertyName )
377403 {
378404 if ( writer == null )
379405 {
@@ -386,16 +412,28 @@ public void SerializeAsV2(IOpenApiWriter writer)
386412 return ;
387413 }
388414
389- SerializeAsV2WithoutReference ( writer ) ;
415+ if ( parentRequiredProperties == null )
416+ {
417+ parentRequiredProperties = new List < string > ( ) ;
418+ }
419+
420+ SerializeAsV2WithoutReference ( writer , parentRequiredProperties , propertyName ) ;
390421 }
391422
392423 /// <summary>
393- /// Serialize to OpenAPI V2 document without using reference.
424+ /// Serialize to OpenAPI V2 document without using reference and handles not marking the provided property
425+ /// as readonly if its included in the provided list of required properties of parent schema.
394426 /// </summary>
395- public void SerializeAsV2WithoutReference ( IOpenApiWriter writer )
427+ /// <param name="writer">The open api writer.</param>
428+ /// <param name="parentRequiredProperties">The list of required properties in parent schema.</param>
429+ /// <param name="propertyName">The property name that will be serialized.</param>
430+ internal void SerializeAsV2WithoutReference (
431+ IOpenApiWriter writer ,
432+ IList < string > parentRequiredProperties ,
433+ string propertyName )
396434 {
397435 writer . WriteStartObject ( ) ;
398- WriteAsSchemaProperties ( writer ) ;
436+ WriteAsSchemaProperties ( writer , parentRequiredProperties , propertyName ) ;
399437 writer . WriteEndObject ( ) ;
400438 }
401439
@@ -463,7 +501,10 @@ internal void WriteAsItemsProperties(IOpenApiWriter writer)
463501 writer . WriteExtensions ( Extensions ) ;
464502 }
465503
466- internal void WriteAsSchemaProperties ( IOpenApiWriter writer )
504+ internal void WriteAsSchemaProperties (
505+ IOpenApiWriter writer ,
506+ IList < string > parentRequiredProperties ,
507+ string propertyName )
467508 {
468509 if ( writer == null )
469510 {
@@ -537,7 +578,8 @@ internal void WriteAsSchemaProperties(IOpenApiWriter writer)
537578 writer . WriteOptionalCollection ( OpenApiConstants . AllOf , AllOf , ( w , s ) => s . SerializeAsV2 ( w ) ) ;
538579
539580 // properties
540- writer . WriteOptionalMap ( OpenApiConstants . Properties , Properties , ( w , s ) => s . SerializeAsV2 ( w ) ) ;
581+ writer . WriteOptionalMap ( OpenApiConstants . Properties , Properties , ( w , key , s ) =>
582+ s . SerializeAsV2 ( w , Required , key ) ) ;
541583
542584 // additionalProperties
543585 writer . WriteOptionalObject (
@@ -549,7 +591,12 @@ internal void WriteAsSchemaProperties(IOpenApiWriter writer)
549591 writer . WriteProperty ( OpenApiConstants . Discriminator , Discriminator ? . PropertyName ) ;
550592
551593 // readOnly
552- writer . WriteProperty ( OpenApiConstants . ReadOnly , ReadOnly , false ) ;
594+ // In V2 schema if a property is part of required properties of parent schema,
595+ // it cannot be marked as readonly.
596+ if ( ! parentRequiredProperties . Contains ( propertyName ) )
597+ {
598+ writer . WriteProperty ( name : OpenApiConstants . ReadOnly , value : ReadOnly , defaultValue : false ) ;
599+ }
553600
554601 // xml
555602 writer . WriteOptionalObject ( OpenApiConstants . Xml , Xml , ( w , s ) => s . SerializeAsV2 ( w ) ) ;
0 commit comments