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

feat(java): Respect content-type header from IR v53 #5386

Merged
merged 8 commits into from
Dec 11, 2024
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
4 changes: 4 additions & 0 deletions fern/pages/changelogs/java-sdk/2024-12-11.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
## 2.7.0
**`(feat):`** Apply Content-Type header from endpoint definition in SDK generator.


Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,41 @@ public final HttpEndpointMethodSpecs generate() {
boolean sendContentType = httpEndpoint.getRequestBody().isPresent()
|| (httpEndpoint.getResponse().isPresent()
&& httpEndpoint.getResponse().get().getBody().isPresent());
String contentType = httpEndpoint
.getRequestBody()
.flatMap(body -> body.visit(new HttpRequestBody.Visitor<Optional<String>>() {
@Override
public Optional<String> visitInlinedRequestBody(InlinedRequestBody inlinedRequestBody) {
return inlinedRequestBody.getContentType();
}

@Override
public Optional<String> visitReference(HttpRequestBodyReference httpRequestBodyReference) {
return httpRequestBodyReference.getContentType();
}

@Override
public Optional<String> visitFileUpload(FileUploadRequest fileUploadRequest) {
// N.B. File upload headers are obtained from request configuration.
return Optional.empty();
}

@Override
public Optional<String> visitBytes(BytesRequest bytesRequest) {
return bytesRequest.getContentType();
}

@Override
public Optional<String> _visitUnknown(Object o) {
throw new IllegalArgumentException("Unknown request type.");
}
}))
.orElse(AbstractEndpointWriter.APPLICATION_JSON_HEADER);
CodeBlock requestInitializer = getInitializeRequestCodeBlock(
clientOptionsField,
generatedClientOptions,
httpEndpoint,
contentType,
generatedObjectMapper,
generatedHttpUrl.inlinableBuild(),
sendContentType);
Expand Down Expand Up @@ -334,6 +365,7 @@ public abstract CodeBlock getInitializeRequestCodeBlock(
FieldSpec clientOptionsMember,
GeneratedClientOptions clientOptions,
HttpEndpoint endpoint,
String contentType,
GeneratedObjectMapper objectMapper,
CodeBlock inlineableHttpUrl,
boolean sendContentType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@
package com.fern.java.client.generators.endpoint;

import com.fern.ir.model.commons.ErrorId;
import com.fern.ir.model.http.HttpEndpoint;
import com.fern.ir.model.http.HttpMethod;
import com.fern.ir.model.http.HttpService;
import com.fern.ir.model.http.SdkRequest;
import com.fern.ir.model.http.*;
import com.fern.java.client.ClientGeneratorContext;
import com.fern.java.client.GeneratedClientOptions;
import com.fern.java.client.GeneratedEnvironmentsClass;
Expand Down Expand Up @@ -86,6 +83,7 @@ public CodeBlock getInitializeRequestCodeBlock(
FieldSpec clientOptionsMember,
GeneratedClientOptions clientOptions,
HttpEndpoint httpEndpoint,
String contentType,
GeneratedObjectMapper generatedObjectMapper,
CodeBlock inlineableHttpUrl,
boolean sendContentType) {
Expand All @@ -111,10 +109,7 @@ public CodeBlock getInitializeRequestCodeBlock(
ClientOptionsGenerator.HEADERS_METHOD_NAME,
REQUEST_OPTIONS_PARAMETER_NAME);
if (sendContentType) {
builder.add(
".addHeader($S, $S)\n",
AbstractEndpointWriter.CONTENT_TYPE_HEADER,
AbstractEndpointWriter.APPLICATION_JSON_HEADER);
builder.add(".addHeader($S, $S)\n", AbstractEndpointWriter.CONTENT_TYPE_HEADER, contentType);
}
return builder.add(".build();\n").unindent().build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ public CodeBlock getInitializeRequestCodeBlock(
FieldSpec clientOptionsMember,
GeneratedClientOptions clientOptions,
HttpEndpoint endpoint,
String contentType,
GeneratedObjectMapper generatedObjectMapper,
CodeBlock inlineableHttpUrl,
boolean sendContentType) {
Expand All @@ -192,10 +193,7 @@ public CodeBlock getInitializeRequestCodeBlock(

@Override
public Void visitTypeReference(HttpRequestBodyReference typeReference) {
builder.add(
".addHeader($S, $S)\n",
AbstractEndpointWriter.CONTENT_TYPE_HEADER,
AbstractEndpointWriter.APPLICATION_JSON_HEADER);
builder.add(".addHeader($S, $S)\n", AbstractEndpointWriter.CONTENT_TYPE_HEADER, contentType);
return null;
}

Expand Down Expand Up @@ -228,10 +226,7 @@ public Void _visitUnknown(Object unknownType) {
clientOptionsMember.name,
ClientOptionsGenerator.HEADERS_METHOD_NAME,
REQUEST_OPTIONS_PARAMETER_NAME);
builder.add(
".addHeader($S, $S)\n",
AbstractEndpointWriter.CONTENT_TYPE_HEADER,
AbstractEndpointWriter.APPLICATION_JSON_HEADER);
builder.add(".addHeader($S, $S)\n", AbstractEndpointWriter.CONTENT_TYPE_HEADER, contentType);
return builder.add(".build();\n").unindent().build();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@

import com.fern.ir.model.commons.ErrorId;
import com.fern.ir.model.commons.NameAndWireValue;
import com.fern.ir.model.http.FileProperty;
import com.fern.ir.model.http.HttpEndpoint;
import com.fern.ir.model.http.HttpMethod;
import com.fern.ir.model.http.HttpService;
import com.fern.ir.model.http.SdkRequest;
import com.fern.ir.model.http.*;
import com.fern.java.client.ClientGeneratorContext;
import com.fern.java.client.GeneratedClientOptions;
import com.fern.java.client.GeneratedEnvironmentsClass;
Expand Down Expand Up @@ -139,6 +135,7 @@ public CodeBlock getInitializeRequestCodeBlock(
FieldSpec clientOptionsMember,
GeneratedClientOptions clientOptions,
HttpEndpoint _unused,
String contentType,
GeneratedObjectMapper generatedObjectMapper,
CodeBlock inlineableHttpUrl,
boolean sendContentType) {
Expand Down Expand Up @@ -201,10 +198,7 @@ public CodeBlock getInitializeRequestCodeBlock(
clientOptionsMember.name,
ClientOptionsGenerator.HEADERS_METHOD_NAME,
AbstractEndpointWriter.REQUEST_OPTIONS_PARAMETER_NAME)
.add(
".addHeader($S, $S);\n",
AbstractEndpointWriter.CONTENT_TYPE_HEADER,
AbstractEndpointWriter.APPLICATION_JSON_HEADER);
.add(".addHeader($S, $S);\n", AbstractEndpointWriter.CONTENT_TYPE_HEADER, contentType);
} else {
requestBodyCodeBlock.add(
".headers($T.of($L.$L($L)));\n",
Expand Down
7 changes: 7 additions & 0 deletions generators/java/sdk/versions.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
- changelogEntry:
- summary: |
Apply Content-Type header from endpoint definition in SDK generator.
type: feat
createdAt: '2024-12-11'
irVersion: 53
version: 2.7.0
- changelogEntry:
- summary: |
Don't generate pagination with nonempty path. Fixes pagination seed tests breaking.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1220,16 +1220,16 @@ exports[`fern api update unioned > fern api update unioned 1`] = `
"type": "string",
"description": "The starting station of the trip",
"examples": [
"Berlin Hauptbahnhof",
"Paris Gare du Nord"
"efdbb9d1-02c2-4bc3-afb7-6788d8782b1e",
"b2e783e1-c824-4d63-b37a-d8d698862f1d"
]
},
"destination": {
"type": "string",
"description": "The destination station of the trip",
"examples": [
"Paris Gare du Nord",
"Berlin Hauptbahnhof"
"b2e783e1-c824-4d63-b37a-d8d698862f1d",
"efdbb9d1-02c2-4bc3-afb7-6788d8782b1e"
]
},
"departure_time": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1220,16 +1220,16 @@ exports[`fern api update > fern api update 1`] = `
"type": "string",
"description": "The starting station of the trip",
"examples": [
"Berlin Hauptbahnhof",
"Paris Gare du Nord"
"efdbb9d1-02c2-4bc3-afb7-6788d8782b1e",
"b2e783e1-c824-4d63-b37a-d8d698862f1d"
]
},
"destination": {
"type": "string",
"description": "The destination station of the trip",
"examples": [
"Paris Gare du Nord",
"Berlin Hauptbahnhof"
"b2e783e1-c824-4d63-b37a-d8d698862f1d",
"efdbb9d1-02c2-4bc3-afb7-6788d8782b1e"
]
},
"departure_time": {
Expand Down

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.

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.

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.

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

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

Loading