Skip to content

[C#] Properties are escaped with _, which causes deserialization to fail #12757

Description

@piber-omicron

Description

Consider the following api model:

ErrorMessage:
      type: object
      required:
        - error_id
        - error_message
      properties:
        error_id:
          type: string
        error_message:
          type: string

The C# codegen renames error_message to ErrorMessage to be consistent with codestyle.
Due to this rename, the member now conflicts with the class name, so it again renames it to _ErrorMessage.
So far so good.

The problem is then that this rename is not properly applied to all the relevant names.
Newtonsoft.Json uses the constructor argument name to match the parameter to the property.
But in the generated code, the parameter is still named errorMessage (not _errorMessage), so the parameter is left as the default value null and not deserialized:

[DataContract]
    internal partial class ErrorMessage :  IEquatable<ErrorMessage>
{
    public ErrorMessage(string errorId = default(string), string errorMessage = default(string), string additionalDetails = default(string))
    {
        // Removed for brevity
    }
    
    [DataMember(Name="error_id", EmitDefaultValue=false)]
    public string ErrorId { get; set; }
    [DataMember(Name="error_message", EmitDefaultValue=false)]
    public string _ErrorMessage { get; set; }

Swagger Codegen Version

3.0.81

Language / Generator

csharp

OpenAPI/Swagger Spec

components:
  schemas:
    ErrorMessage:
      type: object
      required:
        - error_id
        - error_message
      properties:
        error_id:
          type: string
        error_message:
          type: string

Command Line Used

docker run --rm -v ./local:/local swaggerapi/swagger-codegen-cli-v3 generate `
    -i /local/swagger.json `
    -l csharp `
    -o /local/out `
    --additional-properties `
    "targetFramework=v5.0" `
    useDateTimeOffset=true `
    nonPublicApi=true `
    optionalAssemblyInfo=false `
    optionalProjectFile=false

Steps to Reproduce

Expected Behavior

Object should deserialize

Actual Behavior

Property is empty, so constructor throws an exception (since it is marked as required).

Related Issues / Repos

Environment

  • OS:
  • Java Version:
  • Build Tool:
  • Swagger Codegen CLI version:

Additional Context

Checklist

  • I have searched the existing issues to make sure this is not a duplicate.
  • I have included a minimal and reproducible spec example.
  • I have explained how to reproduce the issue.
  • I have specified which generator/language is affected.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions