swagger-api / swagger-api/swagger-codegen

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

Open
#12,757 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug
Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

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.

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 entry point and templates that produce the model constructor and properties, then reproduce the issue with the provided ErrorMessage schema and inspect the generated names. Done means the generated constructor parameter matches the escaped property name and the required error_message value deserializes successfully.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.