OpenAPITools / OpenAPITools/openapi-generator

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

Open
#20,527 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the C# generator's default .mustache template and the generated OpenAPI schema shown in the reproduction. Trace how a string with format byte is classified for equality versus property typing. Done means the generated Equals method compares the bytes value by sequence rather than reference equality, with the existing byte[] property generation preserved.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, openapi
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
40/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.