typescript/angular2
codegenが、エラー処理に関連する奇妙なコードを生成することに気づきました。
/**
* Add user
*
* <strong i="8">@param</strong> user username
* <strong i="9">@param</strong> 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
*
* <strong i="10">@param</strong> 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();
}
});
}
codegen がresponse.status === 204
かどうかだけをチェックするのはなぜですか? 他のエラーコードはどうですか?
私のスワッガーの定義は次のようなものです。
<strong i="16">@PUT</strong>
@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(...
<strong i="17">@GET</strong>
@ApiOperation(value = "User exists", response = Boolean.class)
@ApiResponses(value = {
@ApiResponse(code = 400, message = "Invalid user information specified")
})
public abstract Response exists(
2.2.3-スナップショット
swagger: '2.0'
info:
description: desc
version: 1.0.2
title: Living API
termsOfService: 'http://swagger.io/terms/'
contact:
name: [email protected]
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
java -jar ".\swagger-codegen-cli\target\swagger-codegen-cli.jar" 生成 -i http://localhost :8082/commty/cmng/swagger.json -l typescript-angular2 -v
@jeusdi HTTPステータスコード204は「コンテンツなし」を意味し(参照:https: undefined
が返される理由を説明しています。
@ wing328私の質問は周りにありました:なぜcodegenは@ApiResponses
アノテーションコンテンツを取得する代わりに1つのタイプのエラー応答のみを処理するのですか?
@ApiResponses(value = {
@ApiResponse(code = 400、message = "指定された 'ユーザー名'はLivingCommtyに既に存在します。")、
@ApiResponse(code = 401、message = "指定された 'ユーザー名'には有効なメールフォームがありません。")、
@ApiResponse (コード = 402、メッセージ = "指定された 'passwd' が短すぎます。")
})
さらに、 create
およびexists
メソッドによってスローされる例外についてはどうでしょうか。
@Path(value = "/users")
@Produces({"application/json"})
@Api(value = "/users")
public interface IUserCommtyEndpoint {
<strong i="16">@PUT</strong>
@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(
@HeaderParam("user")
@ApiParam(value = "username", required = true)
@NotNull(message = "user.username.notnull")
@Pattern(regexp="[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?", message="{user.username.email}")
String username,
@HeaderParam("passwd")
@ApiParam(value = "passwd", required = true)
@NotNull(message = "user.passwd.notnull")
@Size(min = 6, max = 20, message = "{username.passwd.size}")
String passwd
) throws UsernameAlreadyExistsCommtyException, RepositorySystemException;
<strong i="17">@GET</strong>
@ApiOperation(value = "User exists", response = Boolean.class)
@ApiResponses(value = {
@ApiResponse(code = 400, message = "Invalid user information specified")
})
public abstract Response exists(
@HeaderParam("user")
@ApiParam(value = "username", required = true)
@NotNull(message = "user.username.notnull")
@Pattern(regexp="[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?", message="{user.username.email}")
String username
);
}
これらの例外は、 UsernameAlreadyExistsCommtyException
、 RepositorySystemException
、 javax.validation.ValidationException
です。 それらはすべてJAXRSマッパーによって処理されます。 例えば:
<strong i="24">@Provider</strong>
public class CommtyExceptionMapper implements ExceptionMapper<UsernameAlreadyExistsCommtyException> {
<strong i="25">@Override</strong>
public Response toResponse(UsernameAlreadyExistsCommtyException exception) {
return Response
.status(Response.Status.BAD_REQUEST.getStatusCode())
.type(MediaType.APPLICATION_JSON)
.entity(ErrorMessage.create(exception.getCode(), exception.getMessage()))
.build();
}
}
ご覧のとおり、本体にErrorMessage {code, message}
を埋め込み、 typescript\angular2
コードで処理したいと思います...
それを処理するためのアプローチはありますか?
応答のstatus
を取得したいという問題に遭遇しました。 具体的には、401がいつ取得されたか知りたいのですが。 私はこれを体から判断することはできません。 この問題は私の問題と一致しているようでした。
204はエラーではないため、これはエラーだけではなく、応答へのアクセスに関するものであることに注意してください。 区別は、応答のステータスコードを使用して行うことができます。 これを強く型付けするために、私は次のことを提案したいと思います。
InlineBody200
に加えてInlineResponse200
InlineBody200
を生成する方が正しいのではないでしょうか。 InlineBody200
はスキーマを表し、 InlineResponse200
は応答自体です。
応答自体は、angular2のReponse
に似ていますが、 .json()
がInlineBody200
返す点が異なります。
さらに、可能な応答の和集合であるタイプを生成できます。
type InlineResponse = InlineResponse200 | InlineResponse401 | etc
でコールDefaultApi
返すことができますObservable<InlineResponse>
。
関数はInlineResponse200 | InlineResponse401 | etc
返します。
そうすれば、angular2が提供するすべての情報を入手できますが、それでも強く型付けされています。