Skip to content
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

fix(java): Add accept header if endpoint has errors #5848

Merged
merged 7 commits into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from 4 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 @@ -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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Discussed offline - we can actually model this in a way where the httpEndpoint has an accept property that represents the set of Accept header values that should be sent in the request. Same idea that you're capturing here, just slightly different implementation.

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
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
{
"version": "1.0.0",
"types": {},
"headers": [],
"endpoints": {
"endpoint_service.endpoint": {
"auth": {
"type": "bearer",
"token": {
"originalName": "token",
"camelCase": {
"unsafeName": "token",
"safeName": "token"
},
"snakeCase": {
"unsafeName": "token",
"safeName": "token"
},
"screamingSnakeCase": {
"unsafeName": "TOKEN",
"safeName": "TOKEN"
},
"pascalCase": {
"unsafeName": "Token",
"safeName": "Token"
}
}
},
"declaration": {
"name": {
"originalName": "endpoint",
"camelCase": {
"unsafeName": "endpoint",
"safeName": "endpoint"
},
"snakeCase": {
"unsafeName": "endpoint",
"safeName": "endpoint"
},
"screamingSnakeCase": {
"unsafeName": "ENDPOINT",
"safeName": "ENDPOINT"
},
"pascalCase": {
"unsafeName": "Endpoint",
"safeName": "Endpoint"
}
},
"fernFilepath": {
"allParts": [
{
"originalName": "service",
"camelCase": {
"unsafeName": "service",
"safeName": "service"
},
"snakeCase": {
"unsafeName": "service",
"safeName": "service"
},
"screamingSnakeCase": {
"unsafeName": "SERVICE",
"safeName": "SERVICE"
},
"pascalCase": {
"unsafeName": "Service",
"safeName": "Service"
}
}
],
"packagePath": [],
"file": {
"originalName": "service",
"camelCase": {
"unsafeName": "service",
"safeName": "service"
},
"snakeCase": {
"unsafeName": "service",
"safeName": "service"
},
"screamingSnakeCase": {
"unsafeName": "SERVICE",
"safeName": "SERVICE"
},
"pascalCase": {
"unsafeName": "Service",
"safeName": "Service"
}
}
}
},
"location": {
"method": "DELETE",
"path": "/container/"
},
"request": {
"type": "body",
"pathParameters": [],
"body": null
},
"response": {
"type": "json"
}
}
},
"environments": null
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
{
"types": {},
"subpackages": {
"subpackage_service": {
"subpackageId": "subpackage_service",
"name": "service",
"endpoints": [
{
"auth": true,
"method": "DELETE",
"id": "endpoint",
"originalEndpointId": "endpoint_service.endpoint",
"name": "Endpoint",
"path": {
"pathParameters": [],
"parts": [
{
"type": "literal",
"value": "/container"
},
{
"type": "literal",
"value": "/"
}
]
},
"queryParameters": [],
"headers": [],
"errorsV2": [
{
"type": {
"type": "alias",
"value": {
"type": "unknown"
}
},
"statusCode": 404,
"description": "Admin not found",
"name": "NotFoundError",
"examples": []
}
],
"examples": [
{
"path": "/container/",
"pathParameters": {},
"queryParameters": {},
"headers": {},
"responseStatusCode": 204,
"codeSamples": []
}
]
}
],
"webhooks": [],
"websockets": [],
"types": [],
"subpackages": []
}
},
"rootPackage": {
"endpoints": [],
"webhooks": [],
"websockets": [],
"types": [],
"subpackages": [
"subpackage_service"
]
},
"auth": {
"type": "bearerAuth",
"tokenName": "token"
},
"snippetsConfiguration": {},
"globalHeaders": []
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions seed/java-sdk/accept-header/accept-header/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading