swagger-api / swagger-api/swagger-codegen

[Bug]: Client serialization does not preserve the API's expected JSON property names and enum representation

Open
#12,764 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

The OpenAPI specification defines the request body using camelCase property names and the lowercase enum value "available". The generated client produces a different representation.

Swagger Codegen Version

3.0.82

Language / Generator

  • csharp

OpenAPI/Swagger Spec

{
    "openapi": "3.0.0",
    "info": {
        "title": "Serializer Test API",
        "version": "0.0.0"
    },
    "tags": [
        {
            "name": "test",
            "description": "Serializer test operations"
        }
    ],
    "paths": {
        "/test": {
            "post": {
                "tags": [
                    "test"
                ],
                "operationId": "post_test_xcuctqa",
                "summary": "Test model serialization",
                "requestBody": {
                    "required": true,
                    "content": {
                        "application/json": {
                            "schema": {
                                "$ref": "#/components/schemas/TestModel"
                            }
                        }
                    }
                },
                "responses": {
                    "200": {
                        "description": "OK"
                    }
                }
            }
        }
    },
    "components": {
        "schemas": {
            "TestModel": {
                "type": "object",
                "properties": {
                    "id": {
                        "type": "integer",
                        "format": "int64"
                    },
                    "firstName": {
                        "type": "string"
                    },
                    "optionalValue": {
                        "type": "string"
                    },
                    "status": {
                        "type": "string",
                        "enum": [
                            "available",
                            "pending"
                        ]
                    },
                    "tags": {
                        "type": "array",
                        "items": {
                            "$ref": "#/components/schemas/Tag"
                        }
                    }
                },
                "additionalProperties": false
            },
            "Tag": {
                "type": "object",
                "properties": {
                    "id": {
                        "type": "integer",
                        "format": "int64"
                    },
                    "name": {
                        "type": "string"
                    }
                },
                "additionalProperties": false
            }
        }
    }
}

Command Line Used

I am using a script that uses the basic command

docker run --rm \
    --user "$(id -u):$(id -g)" \
    -v "$(pwd)":/local \
    -v "$PARENT_DIR":/output \
    swaggerapi/swagger-codegen-cli-v3:3.0.82 \
    generate \
    -i /local/spec/openapi.json \
    -l "$LANG" \
    -o "/output/$OUT_NAME"

and in this case uses 'csharp' for $LANG

Steps to Reproduce

  1. Generate a client using the provided specification
  2. Send a request using
var body = new TestModel
{
    Id = 1,
    FirstName = "test",
    Status = TestModel.StatusEnum.Available
};

await api.PostTestXcuctqaAsync(body);

  1. Observe the outgoing request body

Expected Behavior

The serialized request body should use the property names and enum values defined by the OpenAPI specification:

{
"id": 1,
"firstName": "test",
"status": "available"
}

Actual Behavior

The generated client serializes the model as:

{
"Status": "Available",
"Id": 1,
"FirstName": "test",
"OptionalValue": null,
"Tags": null
}

Related Issues / Repos

Environment

  • OS: Fedora 43

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 by generating the C# client with the supplied OpenAPI specification and inspect the generated model serialization path. Compare the outgoing JSON with the expected camelCase property names, lowercase enum values, and omitted null fields; done means the reproduced request matches the specification.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.