Skip to content

Commit

Permalink
fix(java): Add accept header if endpoint has errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Alberto committed Feb 3, 2025
1 parent 2cef612 commit 4f82a63
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1689,6 +1689,26 @@ public Boolean _visitUnknown(Object unknownType) {
}
}

public static Optional<CodeBlock> maybeAcceptsHeader(HttpEndpoint httpEndpoint, boolean withSemiColon) {
String ending = withSemiColon ? ";\n" : "\n";

Set<String> contentTypes = new HashSet<>();

// TODO: We'll need to get error content types from the IR once they're available.
if (!httpEndpoint.getErrors().get().isEmpty()) {
contentTypes.add(APPLICATION_JSON_HEADER);
}

responseContentType(httpEndpoint.getResponse()).ifPresent(contentTypes::add);

if (contentTypes.isEmpty()) {
return Optional.empty();
}

String headerValue = String.join("; ", contentTypes);
return Optional.of(CodeBlock.of(".addHeader($S, $S)" + ending, ACCEPT_HEADER, headerValue));
}

public static Optional<String> responseContentType(Optional<HttpResponse> response) {
if (response.isEmpty()) {
return Optional.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,7 @@ public CodeBlock getInitializeRequestCodeBlock(
if (sendContentType) {
builder.add(".addHeader($S, $S)\n", AbstractEndpointWriter.CONTENT_TYPE_HEADER, contentType);
}
AbstractEndpointWriter.responseContentType(httpEndpoint.getResponse())
.ifPresent(responseContentType ->
builder.add(".addHeader($S, $S)\n", AbstractEndpointWriter.ACCEPT_HEADER, contentType));
AbstractEndpointWriter.maybeAcceptsHeader(httpEndpoint, false).ifPresent(builder::add);
return builder.add(".build();\n").unindent().build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,8 @@ public CodeBlock getInitializeRequestCodeBlock(
@Override
public Void visitTypeReference(HttpRequestBodyReference typeReference) {
builder.add(".addHeader($S, $S)\n", AbstractEndpointWriter.CONTENT_TYPE_HEADER, contentType);
AbstractEndpointWriter.responseContentType(httpEndpoint.getResponse())
.ifPresent(responseContentType -> builder.add(
".addHeader($S, $S)\n", AbstractEndpointWriter.ACCEPT_HEADER, contentType));
AbstractEndpointWriter.maybeAcceptsHeader(httpEndpoint, false)
.ifPresent(builder::add);
return null;
}

Expand Down Expand Up @@ -230,9 +229,7 @@ public Void _visitUnknown(Object unknownType) {
ClientOptionsGenerator.HEADERS_METHOD_NAME,
REQUEST_OPTIONS_PARAMETER_NAME);
builder.add(".addHeader($S, $S)\n", AbstractEndpointWriter.CONTENT_TYPE_HEADER, contentType);
AbstractEndpointWriter.responseContentType(httpEndpoint.getResponse())
.ifPresent(responseContentType ->
builder.add(".addHeader($S, $S)\n", AbstractEndpointWriter.ACCEPT_HEADER, contentType));
AbstractEndpointWriter.maybeAcceptsHeader(httpEndpoint, false).ifPresent(builder::add);
return builder.add(".build();\n").unindent().build();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,9 @@ public CodeBlock getInitializeRequestCodeBlock(
requestBodyCodeBlock.add(
".method($S, $L)\n", httpEndpoint.getMethod().toString(), getOkhttpRequestBodyName());
}
Optional<String> acceptContentType = AbstractEndpointWriter.responseContentType(httpEndpoint.getResponse());
Optional<CodeBlock> maybeAcceptsHeader = AbstractEndpointWriter.maybeAcceptsHeader(httpEndpoint, true);
if (sendContentType && !isFileUpload) {
if (acceptContentType.isPresent()) {
if (maybeAcceptsHeader.isPresent()) {

requestBodyCodeBlock
.add(
Expand All @@ -202,7 +202,7 @@ public CodeBlock getInitializeRequestCodeBlock(
ClientOptionsGenerator.HEADERS_METHOD_NAME,
AbstractEndpointWriter.REQUEST_OPTIONS_PARAMETER_NAME)
.add(".addHeader($S, $S)", AbstractEndpointWriter.CONTENT_TYPE_HEADER, contentType)
.add(".addHeader($S, $S);\n", AbstractEndpointWriter.ACCEPT_HEADER, contentType);
.add(maybeAcceptsHeader.get());
} else {

requestBodyCodeBlock
Expand All @@ -215,7 +215,7 @@ public CodeBlock getInitializeRequestCodeBlock(
.add(".addHeader($S, $S);\n", AbstractEndpointWriter.CONTENT_TYPE_HEADER, contentType);
}
} else {
if (acceptContentType.isPresent()) {
if (maybeAcceptsHeader.isPresent()) {
requestBodyCodeBlock
.add(
".headers($T.of($L.$L($L)));\n",
Expand All @@ -224,7 +224,7 @@ public CodeBlock getInitializeRequestCodeBlock(
ClientOptionsGenerator.HEADERS_METHOD_NAME,
AbstractEndpointWriter.REQUEST_OPTIONS_PARAMETER_NAME)
.add(".addHeader($S, $S)\n", AbstractEndpointWriter.CONTENT_TYPE_HEADER, contentType)
.add(".addHeader($S, $S);\n", AbstractEndpointWriter.ACCEPT_HEADER, contentType);
.add(maybeAcceptsHeader.get());
} else {
requestBodyCodeBlock.add(
".headers($T.of($L.$L($L)));\n",
Expand Down

0 comments on commit 4f82a63

Please sign in to comment.