Skip to content

OpenAPIResponse int responseCode #279

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
public @interface OpenAPIResponse {

/** the http status code of this response */
String responseCode();
int responseCode();

/**
* The description of the return value. By default uses the @return javadoc of the method as the
Expand Down
13 changes: 9 additions & 4 deletions http-client/src/main/java/io/avaje/http/client/HttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,16 @@ interface Builder {
Builder baseUrl(String baseUrl);

/**
* Set the base URL to use for requests created from the context.
* <p>
* Note that the base url can be replaced via {@link HttpClientRequest#url(String)}.
* Set the default mapper to be used to transform {@link HttpException} into a different kind of
* exception. Individual requests can override with their own mapper.
*
* <p>When set, all {@link HttpException} that are thrown by this client will be caught and
* rethrown with the given function by default.
*
* @param errorMapper The function to map the httpException
* @return The request being built
*/
Builder globalErrorMapper(Function<HttpException, RuntimeException> handler);
Builder globalErrorMapper(Function<HttpException, RuntimeException> errorMapper);

/**
* Set the connection timeout to use.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,15 @@ default HttpClientRequest queryParam(String name, Collection<String> values) {
*/
HttpClientRequest body(HttpRequest.BodyPublisher body);

/**
* Set the mapper used to transform {@link HttpException} into a different kind of exception.
*
* <p>When set, all {@link HttpException} that are thrown by this request will be caught and
* transformed into more specific exception with the given function by default.
*
* @param errorMapper function to map the httpException
* @return The request being built
*/
HttpClientRequest errorMapper(Function<HttpException, RuntimeException> errorMapper);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void build() {
operation.setDeprecated(true);
}

PathItem pathItem = ctx.pathItem(methodReader.fullPath());
final PathItem pathItem = ctx.pathItem(methodReader.fullPath());
switch ((CoreWebMethod) methodReader.webMethod()) {
case GET:
pathItem.setGet(operation);
Expand All @@ -68,20 +68,20 @@ public void build() {
break;
}

var securityRequirements = methodReader.securityRequirements();
for (SecurityRequirementPrism p : securityRequirements) {
var o = new SecurityRequirement().addList(p.name(), p.scopes());
final var securityRequirements = methodReader.securityRequirements();
for (final SecurityRequirementPrism p : securityRequirements) {
final var o = new SecurityRequirement().addList(p.name(), p.scopes());
operation.addSecurityItem(o);
}

for (MethodParam param : methodReader.params()) {
for (final MethodParam param : methodReader.params()) {
param.buildApiDocumentation(this);
}

ApiResponses responses = new ApiResponses();
final ApiResponses responses = new ApiResponses();
operation.setResponses(responses);

ApiResponse response = new ApiResponse();
final ApiResponse response = new ApiResponse();
response.setDescription(javadoc.getReturnDescription());

final var produces = methodReader.produces();
Expand All @@ -106,7 +106,7 @@ public void build() {
}

// if user wants to define their own 2xx status code
if (responseAnnotation.responseCode().startsWith("2")) {
if (responseAnnotation.responseCode().toString().startsWith("2")) {
newResponse.setContent(response.getContent());
override2xx = !hasProducesStatus;
}
Expand All @@ -116,7 +116,7 @@ public void build() {
newResponse.setContent(ctx.createContent(returnType, contentMediaType));
}

responses.addApiResponse(responseAnnotation.responseCode(), newResponse);
responses.addApiResponse(responseAnnotation.responseCode().toString(), newResponse);
}
if (!override2xx) responses.addApiResponse(String.valueOf(methodReader.statusCode()), response);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
@Info(
title = "Example service showing off the Path extension method of controller",
description = ""))
@OpenAPIResponse(responseCode = "403", description = "Not Authorized")
@OpenAPIResponse(responseCode = 403, description = "Not Authorized")
public interface HealthController {
/**
* Standard Get
Expand All @@ -29,6 +29,6 @@ public interface HealthController {
@Get("/health")
@Produces(MediaType.TEXT_PLAIN)
@Tag(name = "tag1", description = "it's somethin")
@OpenAPIResponse(responseCode = "500", type = ErrorResponse.class)
@OpenAPIResponse(responseCode = 500, type = ErrorResponse.class)
String health();
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,13 @@
description = "Example Javalin controllers with Java and Maven"))
@Controller
@Path("openapi/")
@SecurityScheme(type = SecuritySchemeType.APIKEY, in = SecuritySchemeIn.QUERY, name = "JWT", paramName = "access_token", description = "JSON Web Tokens are an open, industry standard RFC 7519 method for representing claims securely between two parties.")
@SecurityScheme(
type = SecuritySchemeType.APIKEY,
in = SecuritySchemeIn.QUERY,
name = "JWT",
paramName = "access_token",
description =
"JSON Web Tokens are an open, industry standard RFC 7519 method for representing claims securely between two parties.")
public class OpenAPIController {

/**
Expand All @@ -38,7 +44,7 @@ public class OpenAPIController {
*/
@Get("/get")
@Produces(MediaType.TEXT_PLAIN)
@OpenAPIResponse(responseCode = "200", type = String.class)
@OpenAPIResponse(responseCode = 200, type = String.class)
void ctxEndpoint(Context ctx) {
ctx.contentType(MediaType.TEXT_PLAIN).result("healthlmao");
}
Expand All @@ -51,13 +57,13 @@ void ctxEndpoint(Context ctx) {
*/
@Post("/post")
@Tag(name = "tag1", description = "this is added to openapi tags")
@OpenAPIResponse(responseCode = "200", description = "overrides @return javadoc description")
@OpenAPIResponse(responseCode = "201")
@OpenAPIResponse(responseCode = 200, description = "overrides @return javadoc description")
@OpenAPIResponse(responseCode = 201)
@OpenAPIResponse(
responseCode = "400",
responseCode = 400,
description = "User not found (Will not have an associated response schema)")
@OpenAPIResponse(
responseCode = "500",
responseCode = 500,
description = "Some other Error (Will have this error class as the response class)",
type = ErrorResponse.class)
Person testPost(Person b) {
Expand All @@ -73,9 +79,9 @@ Person testPost(Person b) {
@Deprecated
@Post("/post1")
@OpenAPIResponses({
@OpenAPIResponse(responseCode = "400", description = "User not found"),
@OpenAPIResponse(responseCode = 400, description = "User not found"),
@OpenAPIResponse(
responseCode = "500",
responseCode = 500,
description = "Some other Error",
type = ErrorResponse.class)
})
Expand All @@ -85,7 +91,7 @@ Person testPostList(List<Person> m) {

@Put("/put")
@Produces(value = MediaType.TEXT_PLAIN, statusCode = 203)
@OpenAPIResponse(responseCode = "204", type = String.class)
@OpenAPIResponse(responseCode = 204, type = String.class)
String testDefaultStatus(Context ctx) {
if (ctx.contentType().equals(MediaType.APPLICATION_PDF)) {
ctx.status(204);
Expand Down