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

[BUG] Buggy .Equal() method on c# aspnetcore3 when using TypeSpec's byte data type #20527

Open
AnReZa opened this issue Jan 22, 2025 · 0 comments

Comments

@AnReZa
Copy link

AnReZa commented Jan 22, 2025

We're using Microsoft TypeSpec to auto-generate c# classes. However, there is a problem with the auto-generated .Equals method. In the default .mustache file of the openapi generator, there's a check for #isContainer which determins, if a regular comparison or .SequenceEqual should be used:

/// <summary>
/// Returns true if {{classname}} instances are equal
/// </summary>
/// <param name="other">Instance of {{classname}} to be compared</param>
/// <returns>Boolean</returns>
public bool Equals({{classname}}? other)
{
    if (other is null) return false;
    if (ReferenceEquals(this, other)) return true;

    return {{#vars}}{{^isContainer}}
        (
            {{name}} == other.{{name}} ||
            {{^vendorExtensions.x-is-value-type}}{{name}} != null &&{{/vendorExtensions.x-is-value-type}}
            {{name}}.Equals(other.{{name}})
        ){{^-last}} && {{/-last}}{{/isContainer}}{{#isContainer}}
        (
            {{name}} == other.{{name}} ||
            {{^vendorExtensions.x-is-value-type}}{{name}} != null &&
            other.{{name}} != null &&
            {{/vendorExtensions.x-is-value-type}}{{name}}.SequenceEqual(other.{{name}})
        ){{^-last}} && {{/-last}}{{/isContainer}}{{/vars}}{{^vars}}false{{/vars}};
}

However, contrary to my expectation for the TypeSpec bytes data type (defined as an array of bytes), it doesn't use the SequenceEqual comparison, but the regular == one, which doesn't work for byte arrays in c#. Why is that?

bytes gets converted into a type: string, format: byte data type, which seems odd to me. Is that right?

Right now, it correctly generates a property of type byte[] in c#, but the equals method doesn't work, as it uses .Equals instead of the correct SequenceEqual.

Reproduction

model Test {
  test: bytes
}
openapi: 3.0.0
info:
  title: (title)
  version: 0.0.0
tags: []
paths: {}
components:
  schemas:
    Test:
      type: object
      required:
        - test
      properties:
        test:
          type: string
          format: byte
@AnReZa AnReZa changed the title [BUG] Description [BUG] Buggy .Equal() method on c# aspnetcore3 when using TypeSpec's byte data type Jan 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant