Skip to content

Regression in C# contract generation with oneOf/allOf combination since v1.27.0 (also in v1.28.0) #6778

Closed

Description

What are you generating using Kiota, clients or plugins?

API Client/SDK

In what context or format are you using Kiota?

Nuget tool

Client library/SDK language

Csharp

Describe the bug

There seems to be a regression in C# contract generation going from v1.26.1 to v1.27.0 and which is also present in v1.28.0.

The linked OpenAPI specification generates the following code for v1.26.1 where we can see that the class is inheriting from ComponentCommon and it has a property One:

// <auto-generated/>
#pragma warning disable CS0618
using Microsoft.Kiota.Abstractions.Extensions;
using Microsoft.Kiota.Abstractions.Serialization;
using System.Collections.Generic;
using System.IO;
using System;
namespace ApiSdk.Models
{
    [global::System.CodeDom.Compiler.GeneratedCode("Kiota", "1.0.0")]
    #pragma warning disable CS1591
    public partial class ExampleWithSingleOneOfWithTypeObject : global::ApiSdk.Models.ComponentCommon, IParsable
    #pragma warning restore CS1591
    {
        /// <summary>The one property</summary>
#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP3_1_OR_GREATER
#nullable enable
        public string? One { get; set; }
#nullable restore
#else
        public string One { get; set; }
#endif
        /// <summary>
        /// Creates a new instance of the appropriate class based on discriminator value
        /// </summary>
        /// <returns>A <see cref="global::ApiSdk.Models.ExampleWithSingleOneOfWithTypeObject"/></returns>
        /// <param name="parseNode">The parse node to use to read the discriminator value and create the object</param>
        public static new global::ApiSdk.Models.ExampleWithSingleOneOfWithTypeObject CreateFromDiscriminatorValue(IParseNode parseNode)
        {
            _ = parseNode ?? throw new ArgumentNullException(nameof(parseNode));
            return new global::ApiSdk.Models.ExampleWithSingleOneOfWithTypeObject();
        }
        /// <summary>
        /// The deserialization information for the current model
        /// </summary>
        /// <returns>A IDictionary&lt;string, Action&lt;IParseNode&gt;&gt;</returns>
        public override IDictionary<string, Action<IParseNode>> GetFieldDeserializers()
        {
            return new Dictionary<string, Action<IParseNode>>(base.GetFieldDeserializers())
            {
                { "one", n => { One = n.GetStringValue(); } },
            };
        }
        /// <summary>
        /// Serializes information the current object
        /// </summary>
        /// <param name="writer">Serialization writer to use to serialize this model</param>
        public override void Serialize(ISerializationWriter writer)
        {
            _ = writer ?? throw new ArgumentNullException(nameof(writer));
            base.Serialize(writer);
            writer.WriteStringValue("one", One);
        }
    }
}
#pragma warning restore CS0618

Using the same contract but using version v1.28.0 (happens also with v1.27.0) generates the following:
NOTE The class is no longer inheriting from the base class, nor does it have a property anymore.

// <auto-generated/>
#pragma warning disable CS0618
using Microsoft.Kiota.Abstractions.Extensions;
using Microsoft.Kiota.Abstractions.Serialization;
using System.Collections.Generic;
using System.IO;
using System;
namespace ApiSdk.Models
{
    [global::System.CodeDom.Compiler.GeneratedCode("Kiota", "1.0.0")]
    #pragma warning disable CS1591
    public partial class ExampleWithSingleOneOfWithTypeObject : IAdditionalDataHolder, IParsable
    #pragma warning restore CS1591
    {
        /// <summary>Stores additional data not described in the OpenAPI description found when deserializing. Can be used for serialization as well.</summary>
        public IDictionary<string, object> AdditionalData { get; set; }
        /// <summary>
        /// Instantiates a new <see cref="global::ApiSdk.Models.ExampleWithSingleOneOfWithTypeObject"/> and sets the default values.
        /// </summary>
        public ExampleWithSingleOneOfWithTypeObject()
        {
            AdditionalData = new Dictionary<string, object>();
        }
        /// <summary>
        /// Creates a new instance of the appropriate class based on discriminator value
        /// </summary>
        /// <returns>A <see cref="global::ApiSdk.Models.ExampleWithSingleOneOfWithTypeObject"/></returns>
        /// <param name="parseNode">The parse node to use to read the discriminator value and create the object</param>
        public static global::ApiSdk.Models.ExampleWithSingleOneOfWithTypeObject CreateFromDiscriminatorValue(IParseNode parseNode)
        {
            _ = parseNode ?? throw new ArgumentNullException(nameof(parseNode));
            return new global::ApiSdk.Models.ExampleWithSingleOneOfWithTypeObject();
        }
        /// <summary>
        /// The deserialization information for the current model
        /// </summary>
        /// <returns>A IDictionary&lt;string, Action&lt;IParseNode&gt;&gt;</returns>
        public virtual IDictionary<string, Action<IParseNode>> GetFieldDeserializers()
        {
            return new Dictionary<string, Action<IParseNode>>
            {
            };
        }
        /// <summary>
        /// Serializes information the current object
        /// </summary>
        /// <param name="writer">Serialization writer to use to serialize this model</param>
        public virtual void Serialize(ISerializationWriter writer)
        {
            _ = writer ?? throw new ArgumentNullException(nameof(writer));
            writer.WriteAdditionalData(AdditionalData);
        }
    }
}
#pragma warning restore CS0618

Expected behavior

The contract should generate the same output for both v1.26.1, v1.27.0 and v1.28.0.

How to reproduce

The following unit test example can be used in KiotaBuilderTests.cs, which succeeds when built & run v1.26.1 but fails from v1.27.0 (I believe the exact commit might be 707d36ca05080c6170a4beae8af14ad011590e02)

    [Fact]
    public async Task InclusiveUnionIntersectionEntriesMergingRegressionAsync()
    {
        var tempFilePath = Path.GetTempFileName();
        await using var fs = await GetDocumentStreamAsync(
    """
openapi: 3.0.0
info:
  title: "Generator not generating oneOf if the containing schema has type: object"
  version: "1.0.0"
servers:
  - url: https://mytodos.doesnotexist/
paths:
  /uses-components:
    post:
      description: Return something
      responses:
        "200":
          description: OK
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/UsesComponents"
      requestBody:
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/UsesComponents"
components:
  schemas:
    ExampleWithSingleOneOfWithTypeObject:
      type: object
      oneOf:
        - $ref: "#/components/schemas/Component1"
      discriminator:
        propertyName: objectType

    ExampleWithSingleOneOfWithoutTypeObject:
      oneOf:
        - $ref: "#/components/schemas/Component2"
      discriminator:
        propertyName: objectType

    UsesComponents:
      type: object
      properties:
        component_with_single_oneof_with_type_object:
          $ref: "#/components/schemas/ExampleWithSingleOneOfWithTypeObject"
        component_with_single_oneof_without_type_object:
          $ref: "#/components/schemas/ExampleWithSingleOneOfWithoutTypeObject"

    ComponentCommon:
      type: object
      required:
        - objectType
      properties:
        objectType:
          type: string
        common:
          type: string

    Component1:
      type: object
      allOf:
        - $ref: "#/components/schemas/ComponentCommon"
        - type: object
          properties:
            one:
              type: string

    Component2:
      type: object
      allOf:
        - $ref: "#/components/schemas/ComponentCommon"
        - type: object
          properties:
            two:
              type: string
""");
        var mockLogger = new Mock<ILogger<KiotaBuilder>>();
        var builder = new KiotaBuilder(mockLogger.Object, new GenerationConfiguration { ClientClassName = "Graph", OpenAPIFilePath = tempFilePath }, _httpClient);
        var document = await builder.CreateOpenApiDocumentAsync(fs);
        var node = builder.CreateUriSpace(document);
        var codeModel = builder.CreateSourceModel(node);

        // Verify both scenarios have all the properties available from all schemas
        var withObjectClass = codeModel.FindChildByName<CodeClass>("ExampleWithSingleOneOfWithTypeObject");
        Assert.NotNull(withObjectClass);
        var withObjectClassOneProperty = withObjectClass.FindChildByName<CodeProperty>("one", false);
        Assert.NotNull(withObjectClassOneProperty);
        // ExampleWithSingleOneOfWithTypeObject inherits from ComponentCommon
        Assert.Equal("ComponentCommon", withObjectClass.BaseClass?.Name);

        var withoutObjectClass = codeModel.FindChildByName<CodeClass>("Component2");
        Assert.NotNull(withoutObjectClass);
        var withoutObjectClassTwoProperty = withoutObjectClass.FindChildByName<CodeProperty>("two", false);
        Assert.NotNull(withoutObjectClassTwoProperty);
        // Component2 inherits from ComponentCommon
        Assert.Equal("ComponentCommon", withoutObjectClass.BaseClass?.Name);
    }

Open API description file

https://github.com/vipentti/kiota-type-object-one-of-issue/blob/main/DiscriminatorProblemSampleHierarchy.yaml

Kiota Version

1.28.0+57130b1b1db3bc5c060498682f41e20c8ae089f2

Latest Kiota version known to work for scenario above?(Not required)

1.26.1+a5df7c03bab621bb2d4dc728516fc9c488cabadb

Known Workarounds

No response

Configuration

No response

Debug output

Click to expand log ```

info: Kiota.Builder.KiotaBuilder[0]
Cleaning output directory .\Generated\Hierarchy\CSharp
dbug: Kiota.Builder.KiotaBuilder[0]
kiota version 1.28.0
info: Kiota.Builder.KiotaBuilder[0]
loaded description from local source
dbug: Kiota.Builder.KiotaBuilder[0]
step 1 - reading the stream - took 00:00:00.0051335
dbug: Kiota.Builder.KiotaBuilder[0]
step 2 - parsing the document - took 00:00:00.0627746
dbug: Kiota.Builder.KiotaBuilder[0]
step 3 - updating generation configuration from kiota extension - took 00:00:00.0000747
dbug: Kiota.Builder.KiotaBuilder[0]
step 4 - filtering API paths with patterns - took 00:00:00.0037156
info: Kiota.Builder.KiotaBuilder[0]
Client root URL set to https://mytodos.doesnotexist
dbug: Kiota.Builder.KiotaBuilder[0]
step 5 - checking whether the output should be updated - took 00:00:00.0116493
dbug: Kiota.Builder.KiotaBuilder[0]
step 6 - create uri space - took 00:00:00.0020482
dbug: Kiota.Builder.KiotaBuilder[0]
InitializeInheritanceIndex 00:00:00.0021495
dbug: Kiota.Builder.KiotaBuilder[0]
CreateRequestBuilderClass 00:00:00
dbug: Kiota.Builder.KiotaBuilder[0]
MapTypeDefinitions 00:00:00.0032270
info: Kiota.Builder.KiotaBuilder[0]
Removing unused model Component1 as it is not referenced by the client API surface
dbug: Kiota.Builder.KiotaBuilder[0]
TrimInheritedModels 00:00:00
dbug: Kiota.Builder.KiotaBuilder[0]
CleanUpInternalState 00:00:00
dbug: Kiota.Builder.KiotaBuilder[0]
step 7 - create source model - took 00:00:00.0477832
dbug: Kiota.Builder.KiotaBuilder[0]
14ms: Language refinement applied
dbug: Kiota.Builder.KiotaBuilder[0]
step 8 - refine by language - took 00:00:00.0145327
dbug: Kiota.Builder.KiotaBuilder[0]
step 9 - writing files - took 00:00:00.0201648
info: Kiota.Builder.KiotaBuilder[0]
loaded description from local source
dbug: Kiota.Builder.KiotaBuilder[0]
step 10 - writing lock file - took 00:00:00.0078204
Generation completed successfully
Client base url set to https://mytodos.doesnotexist
dbug: Kiota.Builder.KiotaBuilder[0]
Api manifest path: \apimanifest.json

Hint: use the info command to get the list of dependencies you need to add to your project.
Example: kiota info -d ".\DiscriminatorProblemSampleHierarchy.yaml" -l CSharp

Hint: use the --include-path and --exclude-path options with glob patterns to filter the paths generated.
Example: kiota generate --include-path "**/foo" -d ".\DiscriminatorProblemSampleHierarchy.yaml"

</details>


### Other information

I believe the exact commit which started failing to be `707d36ca05080c6170a4beae8af14ad011590e02`

Metadata

Metadata

Assignees

No one assigned

    Labels

    CsharpPull requests that update .net codetype:bugA broken experience

    Type

    No type

    Projects

    • Status
      Done ✔️

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions