swagger-api / swagger-api/swagger-codegen

[Golang] `date-time` query parameter does not produce a RFC 3339 time per spec definition

Open
#8,039 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Description

The type: "string" and format: "date-time" parameter type is supposed to be a RFC 3339 time per the Swagger spec:

https://swagger.io/docs/specification/data-models/data-types/

  • date-time – the date-time notation as defined by RFC 3339, section 5.6, for example, 2017-07-21T17:32:28Z

Using a query parameter with type: "string" and format: "date-time" results in a proper time.Time struct, however, the parameterToString() function produces as incorrect string format such as:

2018-01-01 00:00:00 +0000 UTC

This occurs because parameterToString() processes time.Time using fmt.Sprintf("%v", obj) per the code below:

// parameterToString convert interface{} parameters to string, using a delimiter if format is provided.
func parameterToString(obj interface{}, collectionFormat string) string {
	var delimiter string

	switch collectionFormat {
	case "pipes":
		delimiter = "|"
	case "ssv":
		delimiter = " "
	case "tsv":
		delimiter = "\t"
	case "csv":
		delimiter = ","
	}

	if reflect.TypeOf(obj).Kind() == reflect.Slice {
		return strings.Trim(strings.Replace(fmt.Sprint(obj), " ", delimiter, -1), "[]")
	}

	return fmt.Sprintf("%v", obj)
}
Swagger-codegen version

2.3.1

https://github.com/grokify/go-ringcentral/blob/master/codegen/swagger_codegen_version.txt

Swagger declaration file content or url

https://github.com/grokify/go-ringcentral/blob/master/codegen/swagger_spec.yaml

Here is the excerpt with irrelevant parameters omitted:

    /restapi/v1.0/account/{accountId}/extension/{extensionId}/message-store:
      get:
        tags:
          - "Messages"
        summary: "Get Message List"
        operationId: "listMessages"
        consumes:
          - "application/json"
        produces:
          - "application/json"
        parameters:
          -
            name: "dateFrom"
            in: "query"
            description: "The start datetime for resulting messages in ISO 8601 format including timezone, for example 2016-03-10T18:07:52.534Z. The default value is dateTo minus 24 hours"
            required: false
            type: "string"
            format: "date-time"
          -
            name: "dateTo"
            in: "query"
            description: "The end datetime for resulting messages in ISO 8601 format including timezone, for example 2016-03-10T18:07:52.534Z. The default value is current time"
            required: false
            type: "string"
            format: "date-time"
Command line used for generation

$ java -jar swagger-codegen-cli.jar generate -c swagger_codegen_config.json -i swagger_spec.yaml -l go -o client

Steps to reproduce
  1. Build the client
  2. Make an API call to the endpoint with a time.Time struct.
Related issues/PRs

https://github.com/grokify/go-ringcentral/issues/19

Suggest a fix/enhancement

In parameterToString() add a check for time.Time and format as time.RFC3339 using the else clause below.

https://github.com/grokify/swagger-codegen/commit/83dad17d54778cdf5b28182459cb1345a28afb28

	if reflect.TypeOf(obj).Kind() == reflect.Slice {
		return strings.Trim(strings.Replace(fmt.Sprint(obj), " ", delimiter, -1), "[]")
	} else if t, ok := obj.(time.Time); ok {
		return t.Format(time.RFC3339)
	}

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 at the generated Go client's parameterToString() function and review how time.Time values are converted for query parameters. Reproduce the date-time query call described in the issue, then verify that the resulting value uses RFC 3339 formatting and the client still builds successfully.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.