swagger-api / swagger-api/swagger-codegen

[typescript/angular2] error handling no checks out error codes...

Open
#4,373 3 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Client: TypeScript General: Question
Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

I've realized typescript/angular2 codegen, generates some strange code related with error handling:

/**
     * Add user
     * 
     * @param user username
     * @param passwd passwd
     */
    public create(user: string, passwd: string, extraHttpRequestParams?: any): Observable<{}> {
        return this.createWithHttpInfo(user, passwd, extraHttpRequestParams)
            .map((response: Response) => {
                if (response.status === 204) {    <<<<<<<<<<<<<<<<<
                    return undefined;
                } else {
                    return response.json();
                }
            });
    }

    /**
     * User exists
     * 
     * @param user username
     */
    public exists(user: string, extraHttpRequestParams?: any): Observable<boolean> {
        return this.existsWithHttpInfo(user, extraHttpRequestParams)
            .map((response: Response) => {
                if (response.status === 204) {    <<<<<<<<<<<<<<<<<<<<<
                    return undefined;
                } else {
                    return response.json();
                }
            });
    }

Why is codegen checking only whether response.status === 204? What about other error codes?

My swagger definition is like:

	@PUT
	@ApiOperation(value = "Add user")
	@ApiResponses(value = {
	  @ApiResponse(code = 400, message = "Specified 'username' already exists on Living Commty."),
	  @ApiResponse(code = 401, message = "Specified 'username' has no a valid mail form."),
	  @ApiResponse(code = 402, message = "Specified 'passwd' is too short.")
	})
	public abstract Response create(...

    @GET
	@ApiOperation(value = "User exists", response = Boolean.class)
	@ApiResponses(value = {
	  @ApiResponse(code = 400, message = "Invalid user information specified") 
	})
	public abstract Response exists(
Swagger-codegen version

2.2.3-SNAPSHOT

Swagger declaration file content or url
swagger: '2.0'
info:
  description: desc
  version: 1.0.2
  title: Living API
  termsOfService: 'http://swagger.io/terms/'
  contact:
    name: apiteam@swagger.io
  license:
    name: Apache 2.0
    url: 'http://www.apache.org/licenses/LICENSE-2.0.html'
host: localhost
basePath: /commty/cmng
tags:
  - name: users
schemes:
  - http
paths:
  /users:
    get:
      tags:
        - users
      summary: User exists
      description: ''
      operationId: exists
      produces:
        - application/json
      parameters:
        - name: user
          in: header
          description: username
          required: true
          type: string
      responses:
        '200':
          description: successful operation
          schema:
            type: boolean
        '400':
          description: Invalid user information specified
    put:
      tags:
        - users
      summary: Add user
      description: ''
      operationId: create
      produces:
        - application/json
      parameters:
        - name: user
          in: header
          description: username
          required: true
          type: string
        - name: passwd
          in: header
          description: passwd
          required: true
          type: string
      responses:
        '400':
          description: Invalid user information specified
Command line used for generation

java -jar ".\swagger-codegen-cli\target\swagger-codegen-cli.jar" generate -i http://localhost:8082/commty/cmng/swagger.json -l typescript-angular2 -v

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 invoked by the supplied swagger-codegen-cli command and compare its generated handling of the documented 204, 200, and 400 responses. Use the provided Swagger definition to reproduce the output, then verify that declared error responses are handled consistently rather than only checking status 204.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.