grokify/go-ringcentral-client

`client.MessagesApi.ListMessages` Parameter [dateFrom] value is invalid

grokify opened this issue · 3 comments

Description

Setting the dateFrom as follows results in the error below.

  • SDK: client.MessagesApi.ListMessages(context.Background(), "~", "~", map[string]interface{}{"dateTime": time.Now()})
  • API: GET /restapi/v1.0/account/~/extension/~/message-store
Status: 400 Bad Request

{
  "errorCode" : "InvalidParameter",
  "message" : "Parameter [dateFrom] value is invalid",
  "errors" : [ {
    "errorCode" : "CMN-101",
    "message" : "Parameter [dateFrom] value is invalid",
    "parameterName" : "dateFrom"
  } ],
  "parameterName" : "dateFrom"
}

Cause

Investigation shows the time is being converted to:

2018-01-01 00:00:00 +0000 UTC which is not the expected ISO-8601 / RFC-3339 time.

The relevant code in messages_api.go for ListMessages API is:

	if localVarTempParam, localVarOk := localVarOptionals["dateFrom"].(time.Time); localVarOk {
		localVarQueryParams.Add("dateFrom", parameterToString(localVarTempParam, ""))
	if localVarTempParam, localVarOk := localVarOptionals["dateTo"].(time.Time); localVarOk {
		localVarQueryParams.Add("dateTo", parameterToString(localVarTempParam, ""))

api_client.go:func parameterToString(obj interface{}, collectionFormat string) string {

Additional Info

The Swagger spec shows:

          -
            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"

Proposed solution. Update api_client.go:

// 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)
}

Add:

// 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), "[]")
	} else if t, ok := obj.(time.Time); ok {
		return t.Format(time.RFC3339)
	}

	return fmt.Sprintf("%v", obj)
}

2.3.1 post processing fix: 889ca93

Add the following to codegen/swagger_codegen_command.sh

perl -p -i -e 's/(delimiter,\s+-1\),\s+"\[\]"\))/$1\n} else if t, ok := obj.(time.Time); ok {\nreturn t.Format(time.RFC3339)/g' client/api_client.go

To result in api_client.go:

	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)
	}

Permanent fix is here and should be accepted into swagger-codegen

grokify/swagger-codegen@83dad17

Follow this here:

swagger-api/swagger-codegen#8039