Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C# AnyOf array or string not creating array type #3786

Closed
twodayfbh opened this issue Nov 22, 2023 · 1 comment · Fixed by #3792
Closed

C# AnyOf array or string not creating array type #3786

twodayfbh opened this issue Nov 22, 2023 · 1 comment · Fixed by #3792
Assignees
Labels
generator Issues or improvements relater to generation capabilities. help wanted Issue caused by core project dependency modules or library type:bug A broken experience WIP
Milestone

Comments

@twodayfbh
Copy link
Contributor

When trying to create an anyOf type I get an object type rather than an array.
The FooResponseObject property is not a List<FooResponseObject> as expected.

Yaml to reproduce:

openapi: 3.0.1
info:
  title: Test API
  version: 1.0.0
paths:
  /foo:
    get:
      responses:
        200:
          description: "OK"
          content:
            application/json:
              schema:
                oneOf:         # <--- create a union type
                - type: string # <--- string |
                - type: array  # <--- FooResponseObject[]
                  items:
                    $ref: "#/components/schemas/FooResponseObject"
components:
  schemas:
    FooResponseObject:
      type: object
      properties:
        id:
          type: string
        name:
          type: string

Output from Kiota:

public class FooGetResponse : IComposedTypeWrapper, IParsable {
    /// <summary>Composed type representation for type FooResponseObject</summary>
#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP3_1_OR_GREATER
#nullable enable
    public HrOn.Client.Foo.FooResponseObject? FooResponseObject { get; set; }
#nullable restore
#else
    public HrOn.Client.Foo.FooResponseObject FooResponseObject { get; set; }
#endif
    /// <summary>Composed type representation for type string</summary>
#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP3_1_OR_GREATER
#nullable enable
    public string? String { get; set; }
#nullable restore
#else
    public string String { get; set; }
#endif
    /// <summary>
    /// Creates a new instance of the appropriate class based on discriminator value
    /// </summary>
    /// <param name="parseNode">The parse node to use to read the discriminator value and create the object</param>
    public static FooGetResponse CreateFromDiscriminatorValue(IParseNode parseNode) {
        _ = parseNode ?? throw new ArgumentNullException(nameof(parseNode));
        var mappingValue = parseNode.GetChildNode("")?.GetStringValue();
        var result = new FooGetResponse();
        if("".Equals(mappingValue, StringComparison.OrdinalIgnoreCase)) {
            result.FooResponseObject = new HrOn.Client.Foo.FooResponseObject();
        }
        else if(parseNode.GetStringValue() is string stringValue) {
            result.String = stringValue;
        }
        return result;
    }
    /// <summary>
    /// The deserialization information for the current model
    /// </summary>
    public virtual IDictionary<string, Action<IParseNode>> GetFieldDeserializers() {
        if(FooResponseObject != null) {
            return FooResponseObject.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));
        if(FooResponseObject != null) {
            writer.WriteObjectValue<HrOn.Client.Foo.FooResponseObject>(null, FooResponseObject);
        }
        else if(String != null) {
            writer.WriteStringValue(null, String);
        }
    }
}
@github-project-automation github-project-automation bot moved this to Todo in Kiota Nov 22, 2023
@baywet baywet added type:bug A broken experience generator Issues or improvements relater to generation capabilities. labels Nov 23, 2023
@baywet baywet added this to the Kiota v1.10 milestone Nov 23, 2023
@baywet baywet added the help wanted Issue caused by core project dependency modules or library label Nov 23, 2023
@baywet
Copy link
Member

baywet commented Nov 23, 2023

Hi @twodayfbh
Thanks for your interest in kiota and for reporting this.

This is likely caused because the collection kind is not being set here

TypeDefinition = AddModelDeclarationIfDoesntExist(currentNode, targetSchema, className, shortestNamespace),

You can add a unit test below this one for that specific case

public void UnionOfPrimitiveTypesWorks()

With that information in mind, is this something you'd be willing to submit a pull request for?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
generator Issues or improvements relater to generation capabilities. help wanted Issue caused by core project dependency modules or library type:bug A broken experience WIP
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

2 participants