mtennoe / swagger-typescript-codegen

A Swagger Codegenerator tailored for typescript.
Apache License 2.0
141 stars 52 forks source link

[BUG] Exception if there's a root path in spec #108

Closed VentyCZ closed 4 years ago

VentyCZ commented 4 years ago

Just tried to integrate this library into my project and I just found a case where the library fails.

There is an issue with the getPathToMethodName function.

When I have a root path (/) in the paths object of the spec, the library crashes: "Cannot read property 'toUpperCase' of undefined".

It's caused by line 116, but originates from line above.

If the result variable is an empty string it throws.

I "fixed" it by doing this before calling the getTypescriptCode function:

if (spec.paths.hasOwnProperty('/')) {
    let prop = spec.paths['/'];
    delete spec.paths['/'];
    spec.paths['/index'] = prop;
}

This solution fixes the issue partially, because now the target path in the generated code is wrong.

The solution to the issue might be using an alias for the / path, like index for example.

sjwilczynski commented 4 years ago

Created a PR for that.

VentyCZ commented 4 years ago

@sjwilczynski Thanks for the fix! So it will generate methods like getroot(), right ?

mtennoe commented 4 years ago

Thanks for reporting @VentyCZ, and thanks for the PR @sjwilczynski - Will take a look today

mtennoe commented 4 years ago

Merged and published as 3.0.3. Thanks @sjwilczynski! Please reopen if it doesnt solve the problem for you @VentyCZ

sjwilczynski commented 4 years ago

@sjwilczynski Thanks for the fix! So it will generate methods like getroot(), right ?

@VentyCZ After some changes we decided on methods names like rootEndpoint_${lowerCasedHttpVerb} - the http method comes after so that there is no conflict when someone has an endpoint called "rootEnpoint"