swagger-codegen icon indicating copy to clipboard operation
swagger-codegen copied to clipboard

[Typescript] Utilise Typescript object spread vs custom extendObj method

Open damienpontifex opened this issue 9 years ago • 9 comments

Description

Typescript 2.1 allows object spread (see https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-1.html#object-spread-and-rest). The Typescript templates currently define an extendObj method to achieve this same feature e.g. https://github.com/swagger-api/swagger-codegen/blob/master/modules/swagger-codegen/src/main/resources/typescript-angular2/api.mustache#L44-L51

Swagger-codegen version

2.2.1

Suggest a Fix

Remove extendObj in api.mustache files and just utilise object rest operator.

I can provide a fix with a PR unless there's any reason not to...

damienpontifex avatar Dec 16 '16 06:12 damienpontifex

@damienpontifex I definitely welcome enhancements to leverage new features in a programming language.

Your suggestion is related to https://github.com/swagger-api/swagger-codegen/issues/4375

Is my understanding correct that there's no way to make this particular change so that the output is compatible with both 2.1.4 and 2.1.1?

wing328 avatar Dec 16 '16 10:12 wing328

Yes, my solution would resolve that issue in my preliminary testing.

I thought the feature was available in typescript 2.1.x, but looking at this issue on the Typescript repo, it has a milestone of 2.1.4 so maybe it was only finally included with the minor 2.1 release

damienpontifex avatar Dec 16 '16 10:12 damienpontifex

@damienpontifex OK. Please kindly submit a PR so that we can review the enhancement.

wing328 avatar Dec 16 '16 10:12 wing328

Locally, I'm using Object.assign to fix this issue, and it's working so far - at least it's adding my extra request params. Object.assign should be backwards compatible, too, so I thought I'd throw this out there. Here's the solution:

requestOptions = Object.assign(requestOptions, extraHttpRequestParams);

todrules avatar Dec 30 '16 16:12 todrules

@todrules I also like backward compatible solution so may I know if you've time to submit a PR with your enhancement?

wing328 avatar Jan 03 '17 15:01 wing328

@wing328 you mentioned in #4409 about changing that PR to v2.3. Would we do a backwards compatible change in 2.2 and then the spread operator in 2.3? If both achieve the same thing, would a second change in 2.3 that isn't backwards compatible add any value?

Just wondering if we abandon the spread operator change and just adopt Object.assign as @todrules suggested. This would also remove the changes to use @types for typing definitions in the samples.

damienpontifex avatar Jan 04 '17 00:01 damienpontifex

@damienpontifex both your approach and the approach suggested by @todrules are ok with me. Personally I prefer 1 solution without introducing breaking changes (which require TS version upgrade) if possible.

wing328 avatar Jan 09 '17 03:01 wing328

I'll make the modifications and downgrade the sample ts versions to align with the suggestion by @todrules

damienpontifex avatar Jan 11 '17 23:01 damienpontifex

@todrules the PR by @damienpontifex has been merged into master. Please pull the latest master to give it a try.

wing328 avatar Jan 17 '17 02:01 wing328