swagger-api / swagger-api/swagger-codegen

[Typescript-angular2] Date in path is not correctly formatted for typescript-angular2 client

Open
#4,813 1 comment 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

The generated client does not format any date-time that is part of an operation path.

For an endpoint with this path:

/api/value/{date}/{id}

An example request (as generated by the client) would look like this:

http://localhost:5000/api/value/Thu%20Feb%2016%202017%2020:57:11%20GMT+0000%20(GMT%20Standard%20Time)/1

Swagger-codegen version

2.2.2-SNAPSHOT with HEAD as 0d14496bd6fbdbd763844f239b34e198459fe4d9

Swagger declaration file content or url

{"swagger":"2.0","info":{"version":"v1","title":"API V1"},"basePath":"/","paths":{"/api/category":{"get":{"tags":["Tracker"],"operationId":"ApiCategoryGet","consumes":[],"produces":["application/json"],"responses":{"200":{"description":"Success","schema":{"type":"array","items":{"$ref":"#/definitions/Category"}}}},"deprecated":false}},"/api/metric":{"get":{"tags":["Tracker"],"operationId":"ApiMetricGet","consumes":[],"produces":["application/json"],"responses":{"200":{"description":"Success","schema":{"type":"array","items":{"$ref":"#/definitions/Metric"}}}},"deprecated":false},"post":{"tags":["Tracker"],"operationId":"ApiMetricPost","consumes":["application/json"],"produces":["application/json"],"parameters":[{"name":"metric","in":"body","required":false,"schema":{"$ref":"#/definitions/Metric"}}],"responses":{"200":{"description":"Success","schema":{"format":"int64","type":"integer"}}},"deprecated":false}},"/api/value/{date}/{metricId}":{"put":{"tags":["Tracker"],"operationId":"ApiValueByDateByMetricIdPut","consumes":["application/json"],"produces":["application/json"],"parameters":[{"name":"metricId","in":"path","required":true,"type":"integer","format":"int64"},{"name":"date","in":"path","required":true,"type":"string","format":"date-time"},{"name":"value","in":"body","required":false,"schema":{"format":"double","type":"number"}}],"responses":{"200":{"description":"Success","schema":{"format":"int64","type":"integer"}}},"deprecated":false}}},"definitions":{"Category":{"type":"object","properties":{"categoryId":{"format":"int32","type":"integer"},"name":{"type":"string"}}},"Metric":{"type":"object","properties":{"metricId":{"format":"int32","type":"integer"},"name":{"type":"string"},"categoryId":{"format":"int32","type":"integer"},"type":{"type":"string"},"text":{"type":"string"}}}},"securityDefinitions":{}}

Command line used for generation

generate -l typescript-angular2 -o src/services/api/ -i http://localhost:5000/swagger/v1/swagger.json

Steps to reproduce

Generate the client then call the method "apiValueByDateByMetricIdPutWithHttpInfo" within TrackApi.

Related issues
Suggest a Fix

The problem is being caused by how a date-time is formatted within the generated code. So for the example above, the client looks like this:

public apiValueByDateByMetricIdPutWithHttpInfo(metricId: number, date: Date, value?: number, extraHttpRequestParams?: any): Observable<Response> {
        const path = this.basePath + `/api/value/${date}/${metricId}`;

The problems is that ${date} returns the human readable string. What we want is the machine readable string format which can be obtained with ${date.toISOString()}.

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 typescript-angular2 generator output for TrackApi and the apiValueByDateByMetricIdPutWithHttpInfo method, using the supplied Swagger declaration and generation command to reproduce the request. Inspect how the path is assembled and verify that a date-time path parameter is emitted in machine-readable form rather than as a human-readable Date string.

Written by the indexing model from the issue text.

Assessment

Tech stack
angular, typescript
Domain
api
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.