镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

[typescript-angular] error TS1345: An expression of type 'void' cannot be tested for truthiness #8836

Closed

Description

@itsTeknas
Description

The generated typescript client fails in angular 7 build when there seems to be any sort of file field.

Typescript version

"typescript": "^3.1.3"

Swagger-codegen version

The latest online version

Swagger declaration file content or url
parameters:
        - name: attachment1
          type: file
          in: formData
          required: false

Produces:

if (attachment1 !== undefined) {
            formParams = formParams.append('attachment1', <any>attachment1) || formParams;
}

The definition of append tracks down to

let formParams: { append(param: string, value: any): void; };

screenshot 2018-10-22 at 2 13 46 pm

Steps to reproduce

Generate a new client with angular-typescript selected.

Activity

  1. artwel commented on Oct 24, 2018

    @artwel

    Same problem in our environment, after updating angular from v6 to v7 it is not possible to compile the genereated swagger services with the same error as seen above.
    tried swagger-codegen-cli 2.4.0-SNAPSHOT and 3.0.2

  2. Dviejopomata commented on Oct 25, 2018

    @Dviejopomata

    Also happens with

    import { Observable } from 'rxjs/Observable'
    

    Which has to be :

    import { Observable } from 'rxjs'
    

    As a workaround you can use some bash after generating the code

    sed -i 's/rxjs\/Observable/rxjs/' ./src/app/swagger/api/api.service.ts 
    sed -i 's/formParams = formParams/formParams/' ./src/app/swagger/api/api.service.ts 
    sed -i 's/ || formParams//' ./src/app/swagger/api/api.service.ts 
  3. itsTeknas commented on Oct 26, 2018

    @itsTeknas
    Author

    @Dviejopomata yeah this has been the case after updating the rxjs version to 6.0.0

  4. itsTeknas commented on Oct 29, 2018

    @itsTeknas
    Author

    @Dviejopomata , this actually has been implemented, you just need to pass the ngversion parameter while generating. The online generator is generating for angular 4.3 as a default.

  5. awalland commented on Oct 29, 2018

    @awalland

    @Dviejopomata , this actually has been implemented, you just need to pass the ngversion parameter while generating. The online generator is generating for angular 4.3 as a default.

    do you have any pointers to what to set it? i tried with --additional-properties ngVersion=6.0.0 but that didn't seem to do anything.

  6. Dviejopomata commented on Oct 30, 2018

    @Dviejopomata

    @awalland Make sure you are using v3.0.0 at least, i had to change from v2.3.1 to v3.0.0 and it worked fine:

    Since i'm using docker this is what my command looks like:

    docker run --rm -v ${PWD}/src/app/swagger:/local swaggerapi/swagger-codegen-cli:unstable generate -i http://192.168.1.47:5070/swagger/v1/swagger.json -l typescript-angular -o /local --additional-properties ngVersion=6.0.0
    
  7. jkarun commented on Nov 12, 2018

    @jkarun

    I'm facing same problem, after I updating angular from v5 to v7. I used all version of swagger cli jars. I used following command to generate swagger code

    java -jar ./swagger-codegen-cli-3.0.0-20180710.190537-87.jar generate -i http:///swagger/v1/swagger.json -l typescript-angular -o ./local --additional-properties ngVersion=7.0.0

  8. TvrtkoM commented on Dec 3, 2018

    @TvrtkoM

    With swagger-codegen-cli-3.0.3.jar I get invalid petApi.service.ts

    petApi.service.ts(364,1): error TS1005: '}' expected.

    Used command is
    java -jar ./swagger-codegen-cli-3.0.3.jar generate -i petstore.yaml -l typescript-angular -o ./local --additional-prop erties ngVersion=7.0.0

    Same if I use ngVersion 7.1.0

    Also all codegen-cli (jars) are generating invalid typescript 3 code. Errors are related to rxjs Imports and An expression of type 'void' cannot be tested for truthiness

    Problematic code is

        let formParams: { append(param: string, value: any): void; };
        let useForm = false;
        let convertFormParamsToString = false;
        if (useForm) {
            formParams = new FormData();
        } else {
            formParams = new HttpParams({encoder: new CustomHttpUrlEncodingCodec()});
        }
        if (name !== undefined) {
            formParams = formParams.append('name', <any>name) || formParams;
        }
        if (status !== undefined) {
            formParams = formParams.append('status', <any>status) || formParams;
        }
    

    useForm boolean is basically hardcoded and FormData constructor is never used.
    formParams should be of this type and formParams=new FormData() line removed (i thinlk):
    let formParams: { append(param: string, value: any): HttpParams; };

  9. plathub commented on Jan 3, 2019

    @plathub

    Proposed fix: change the line

    let formParams: { append(param: string, value: any): void; };
    

    to

    let formParams: { append(param: string, value: any): void | HttpParams; };
    

    in the template file api.service.mustache

  10. tilowestermann commented on Jan 9, 2019

    @tilowestermann

    Is there any update on this issue (or the proposed PRs)?

  11. emmanuelgautier commented on Jan 17, 2019

    @emmanuelgautier

    @plathub This fix does not work because it is not only a typing issue, formParams.append could really return nothing or a new HttpParams with appended value.

  12. itsTeknas commented on Apr 14, 2019

    @itsTeknas
    Author

    I have resorted to running my own sanitise-swagger.sh after each generation

    find ./src/swagger/api/*.service.ts -type f -exec sed -i '' -e 's/rxjs\//rxjs/' {} \;
    find ./src/swagger/api/*.service.ts -type f -exec sed -i '' -e 's/formParams = formParams/formParams/' {} \;
    find ./src/swagger/api/*.service.ts -type f -exec sed -i '' -e 's/ || formParams//' {} \;
    

    This does the corrections on all files under /src/swagger which is where i keep my generated client.

  13. 3 remaining items

  14. gbertoncelli commented on Jul 24, 2019

    @gbertoncelli

    I haven't found a valid workaround amongst the comments above... This problem appears also with openapi-generator every time an endpoint function has to use FormData or HttpParam. My solution was to define differently the formData variable so it can return void or something like a new HttpParam:

            // HERE: if we use HttpParams the append function returns a new HttpParams object
            // instead if we use FormData the append function will not return anything
            let formParams: { append(param: string, value: any): void | HttpParams; };
            let useForm = false;
            let convertFormParamsToString = false;
            if (useForm) {
                formParams = new FormData();
            } else {
                formParams = new HttpParams({ encoder: new CustomHttpUrlEncodingCodec() });
            }
    
            if (newPassword !== undefined) {
                // here there is no more the syntax error
                formParams = formParams.append('newPassword', <any>newPassword) || formParams;
            }
            // here nothing changes
            return this.httpClient.post<any>(`${this.basePath}/accounts/reset-password`,
                convertFormParamsToString ? formParams.toString() : formParams,
                {
                    withCredentials: this.configuration.withCredentials,
                    headers: headers,
                    observe: observe,
                    reportProgress: reportProgress
                }
            );
    

    By using this everytime we use FormData/HttpParams the HTTP requests result valid.

  15. Hobart2967 commented on Sep 21, 2019

    @Hobart2967

    Still valid for v3.0.11. Is this gonna be resolved any soon?

  16. added this to the v3.0.12 milestone on Sep 21, 2019
  17. self-assigned this
    on Sep 21, 2019
  18. HugoMario commented on Sep 21, 2019

    @HugoMario
    Contributor

    hi @Hobart2967 , just assign it to myself, sadly i cant guarantee solve it soon, but once i have a chance i'll work on it

  19. timdows commented on Oct 1, 2019

    @timdows

    Just like @itsTeknas I have used a script (powershell) to solve this for the time being

    java -jar .\swagger-codegen-cli-2.4.1.jar generate -i http://localhost:5100/swagger/v1/swagger.json -l typescript-angular -o ../src/api
    
    $file = '..\src\api\api\thexyz.service.ts'
    $find = 'let formParams: { append(param: string, value: any): void; };'
    $replace = 'let formParams: { append(param: string, value: any): void | HttpParams; };'
    
    (Get-Content $file).replace($find, $replace) | Set-Content $file
  20. aaahrens commented on Nov 16, 2019

    @aaahrens

    any news on this?

  21. JeanMeche commented on Dec 17, 2019

    @JeanMeche

    I would love to see a fix too !

  22. amenski commented on Apr 19, 2020

    @amenski

    As a temporary fix I used @itsTeknas solution on windows MinGw with a small dummy script launched on start up.

    
    var gen = 'java -jar src/swagger/swagger-codegen-cli-2.4.13.jar generate -i src/swagger/swagger-spec.yml -l typescript-angular -o src/app/generated';
    var rxFix = 'sed -i \'s/rxjs\\/Observable/rxjs/\' ./src/app/generated/api/*.service.ts';
    var frmFix = 'sed -i "s/ \|\| formParams//"  ./src/app/generated/api/*.service.ts';
    var frmFix2 = 'sed -i \'s/formParams = formParams/formParams/\' ./src/app/generated/api/*.service.ts';
    
    runCommand(gen, (error, stdout, stderr) => {
      console.log('Output: ' + stdout)
      if(error !== null){
        console.log("Error: " + error);
        return;
      }
      if(stderr !== null){
        console.log("Stderr: " + stderr);
      }
    });
    
    runCommand(rxFix, (error, stdout, stderr) => {
      console.log('Output: ' + stdout)
      if(error !== null){
        console.log("Error: " + error);
        return;
      }
      if(stderr !== null){
        console.log("Stderr: " + stderr);
      }
    });
    
    runCommand(frmFix, (error, stdout, stderr) => {
      console.log('Output: ' + stdout)
      if(error !== null){
        console.log("Error: " + error);
        return;
      }
      if(stderr !== null){
        console.log("Stderr: " + stderr);
      }
    });
    
    runCommand(frmFix2, (error, stdout, stderr) => {
      console.log('Output: ' + stdout)
      if(error !== null){
        console.log("Error: " + error);
        return;
      }
      if(stderr !== null){
        console.log("Stderr: " + stderr);
      }
    });
    
  23. alainkaiser commented on Jun 2, 2020

    @alainkaiser

    Any news on this? Still facing the same issue...

  24. modified the milestones: v3.0.12, M5 on Jun 17, 2020
  25. HugoMario commented on Jul 16, 2020

    @HugoMario
    Contributor

    this has been fixed by #9065 thanks everyone for help and sorry for delay.

  26. de-don commented on Aug 7, 2020

    @de-don

    Still have this issue with array type form-data parameters.
    Version - latest

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions