11using Newtonsoft . Json ;
22using Newtonsoft . Json . Linq ;
33using NJsonSchema ;
4+ using NJsonSchema . CodeGeneration ;
45using NJsonSchema . Generation ;
56using NJsonSchema . Yaml ;
67using YamlDotNet . Serialization ;
@@ -59,11 +60,13 @@ public static Task<AsyncApiDocument> FromJsonAsync(string data)
5960 /// <param name="data">JSON text.</param>
6061 /// <param name="documentPath"> Path to document. </param>
6162 /// <returns>AsyncApi document object model.</returns>
62- public static Task < AsyncApiDocument > FromJsonAsync ( string data , string ? documentPath )
63+ public static async Task < AsyncApiDocument > FromJsonAsync ( string data , string ? documentPath )
6364 {
6465 var document = JsonConvert . DeserializeObject < AsyncApiDocument > ( data , JSONSERIALIZERSETTINGS ) ! ;
6566 document . DocumentPath = documentPath ;
66- return UpdateSchemaReferencesAsync ( document ) ;
67+ await UpdateSchemaReferencesAsync ( document ) ;
68+ BuildAsyncApiDescriminatorMapping ( document ) ;
69+ return document ;
6770 }
6871
6972 /// <summary>
@@ -80,7 +83,7 @@ public static Task<AsyncApiDocument> FromYamlAsync(string data)
8083 /// <param name="data">YAML text.</param>
8184 /// <param name="documentPath"> Path to document. </param>
8285 /// <returns>AsyncApi document object model.</returns>
83- public static Task < AsyncApiDocument > FromYamlAsync ( string data , string ? documentPath )
86+ public static async Task < AsyncApiDocument > FromYamlAsync ( string data , string ? documentPath )
8487 {
8588 var deserializer = new DeserializerBuilder ( ) . Build ( ) ;
8689 using var reader = new StringReader ( data ) ;
@@ -90,15 +93,40 @@ public static Task<AsyncApiDocument> FromYamlAsync(string data, string? document
9093 var serializer = JsonSerializer . Create ( JSONSERIALIZERSETTINGS ) ;
9194 var doc = jObject . ToObject < AsyncApiDocument > ( serializer ) ! ;
9295 doc . DocumentPath = documentPath ;
93- return UpdateSchemaReferencesAsync ( doc ) ;
96+ await UpdateSchemaReferencesAsync ( doc ) ;
97+ BuildAsyncApiDescriminatorMapping ( doc ) ;
98+ return doc ;
9499 }
95100
96- private static async Task < AsyncApiDocument > UpdateSchemaReferencesAsync ( AsyncApiDocument document )
101+ private static Task UpdateSchemaReferencesAsync ( AsyncApiDocument document )
97102 {
98- await JsonSchemaReferenceUtilities . UpdateSchemaReferencesAsync (
103+ return JsonSchemaReferenceUtilities . UpdateSchemaReferencesAsync (
99104 document ,
100105 new JsonAndYamlReferenceResolver ( new AsyncApiSchemaResolver ( document , new SystemTextJsonSchemaGeneratorSettings ( ) ) ) ) ;
101- return document ;
106+ }
107+
108+ private static void BuildAsyncApiDescriminatorMapping ( AsyncApiDocument document )
109+ {
110+ foreach ( var schema in document . Components ? . Schemas . Values ?? [ ] )
111+ {
112+ var discriminatorPropName = schema . DiscriminatorObject ? . PropertyName ;
113+ if ( discriminatorPropName != null )
114+ {
115+ var derivedSchemas = schema . GetDerivedSchemas ( document ) ;
116+ foreach ( var item in derivedSchemas )
117+ {
118+ var derivedSchema = item . Key ;
119+ if ( ( derivedSchema . Properties . TryGetValue ( discriminatorPropName , out var discriminatorProp )
120+ || derivedSchema . AllOf ? . FirstOrDefault ( i => i != schema && i . Properties . ContainsKey ( discriminatorPropName ) ) ? . Properties . TryGetValue ( discriminatorPropName , out discriminatorProp ) == true )
121+ && discriminatorProp . ExtensionData ? . TryGetValue ( "const" , out var constValue ) == true )
122+ {
123+ var constValueStr = constValue ! . ToString ( ) ;
124+ discriminatorProp . ParentSchema ! . Properties . Remove ( discriminatorPropName ) ;
125+ schema . DiscriminatorObject ! . Mapping . Add ( constValueStr , derivedSchema ) ;
126+ }
127+ }
128+ }
129+ }
102130 }
103131 }
104132#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
0 commit comments