swagger-api / swagger-api/swagger-codegen

[DOTNET] code uses "o" for ISO8601 date formats, which is not quite right

Open
#10,818 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Description

"O"/"o" in .net is a shortcut for yyyy-MM-ddThh:mm:ss.fffffffZ

but ISO8601 only specifies as precise as yyyy-MM-ddThh:mm:ss.fffZ

I expect in 99.9% of APIs that doesn't matter, but NatWest's Open Banking interface rejects dates with 7 DP of precision for the second as not an ISO 8601 Date

Swagger-codegen version

problem is in master

Swagger declaration file content or url
Command line used for generation
Steps to reproduce

Use any API with a date in it and look at the date rendered into the json

Related issues/PRs

searched issue log for ISO8601 and found nothing

Suggest a fix/enhancement

..change "o" here.. to "yyyy-MM-ddThh:mm:ss.fffZ"


modules/swagger-codegen/src/main/resources/csharp/Configuration.mustache:        public const string ISO8601_DATETIME_FORMAT = "o";
modules/swagger-codegen/src/main/resources/csharp-dotnet2/Configuration.mustache:        private const string ISO8601_DATETIME_FORMAT = "o";

and where the serializer is used, you need to specify the format to use...

modules/swagger-codegen/src/main/resources/csharp/ApiClient.mustache:

        private JsonSerializerSettings serializerSettings = new JsonSerializerSettings
        {
            ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor,
            DateFormatString = "yyyy-MM-ddThh:mm:ss.fffZ"
        };

..and fix issue #10461 by including the serializerSettings in teh call to Setialise

        public String Serialize(object obj)
        {
            try
            {
                return obj != null ? JsonConvert.SerializeObject(obj, serializerSettings) : null;
            }
            catch (Exception e)
            {
                throw new ApiException(500, e.Message);
            }
        }

I've not tested this, other than by "hacking" the generated code to prove that's the issue.

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# templates in modules/swagger-codegen/src/main/resources/csharp/Configuration.mustache, csharp-dotnet2/Configuration.mustache, and csharp/ApiClient.mustache. Reproduce generation with an API containing a date, then inspect the generated JSON serialization. Done means generated clients use the requested ISO 8601 precision and pass the reported serialization path without breaking existing behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
api
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.