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): Include version headers in request options #5460

Merged
merged 22 commits into from
Jan 7, 2025
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/2025-01-07.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
## 2.9.0
**`(feat):`** Support version headers in request and client options.


Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.fern.java.client;

import com.fern.ir.model.commons.Name;
import com.fern.ir.model.commons.SafeAndUnsafeString;

public final class ApiVersionConstants {
/** Do not instantiate utils class. */
private ApiVersionConstants() {}

public static final String CURRENT_API_VERSION = "CURRENT";

public static final Name CURRENT_API_VERSION_NAME = Name.builder()
.originalName("CURRENT")
.camelCase(SafeAndUnsafeString.builder()
.unsafeName("current")
.safeName("current")
.build())
.pascalCase(SafeAndUnsafeString.builder()
.unsafeName("Current")
.safeName("Current")
.build())
.snakeCase(SafeAndUnsafeString.builder()
.unsafeName("current")
.safeName("current")
.build())
.screamingSnakeCase(SafeAndUnsafeString.builder()
.unsafeName("CURRENT")
.safeName("CURRENT")
.build())
.build();
}
29 changes: 29 additions & 0 deletions generators/java/sdk/src/main/java/com/fern/java/client/Cli.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@
import com.fern.ir.model.auth.AuthScheme;
import com.fern.ir.model.auth.OAuthScheme;
import com.fern.ir.model.commons.ErrorId;
import com.fern.ir.model.commons.NameAndWireValue;
import com.fern.ir.model.ir.HeaderApiVersionScheme;
import com.fern.ir.model.ir.IntermediateRepresentation;
import com.fern.ir.model.types.EnumTypeDeclaration;
import com.fern.ir.model.types.EnumValue;
import com.fern.java.AbstractGeneratorCli;
import com.fern.java.AbstractPoetClassNameFactory;
import com.fern.java.DefaultGeneratorExecClient;
Expand All @@ -32,6 +36,7 @@
import com.fern.java.client.generators.SuppliersGenerator;
import com.fern.java.client.generators.TestGenerator;
import com.fern.java.generators.DateTimeDeserializerGenerator;
import com.fern.java.generators.EnumGenerator;
import com.fern.java.generators.ObjectMappersGenerator;
import com.fern.java.generators.PaginationCoreGenerator;
import com.fern.java.generators.StreamGenerator;
Expand Down Expand Up @@ -183,6 +188,30 @@ public GeneratedRootClient generateClient(
GeneratedEnvironmentsClass generatedEnvironmentsClass = environmentGenerator.generateFile();
this.addGeneratedFile(generatedEnvironmentsClass);

ir.getApiVersion()
.flatMap(apiVersion -> apiVersion.getHeader().map(HeaderApiVersionScheme::getValue))
.ifPresent(irDeclaration -> {
EnumTypeDeclaration.Builder enumTypeDeclaration =
EnumTypeDeclaration.builder().from(irDeclaration);

irDeclaration.getDefault().ifPresent(defaultValue -> {
enumTypeDeclaration.addValues(EnumValue.builder()
.from(defaultValue)
.name(NameAndWireValue.builder()
.from(defaultValue.getName())
.name(ApiVersionConstants.CURRENT_API_VERSION_NAME)
.build())
.build());
});

EnumGenerator apiVersionsGenerator = new EnumGenerator(
context.getPoetClassNameFactory().getApiVersionClassName(),
context,
enumTypeDeclaration.build());
GeneratedJavaFile generatedApiVersions = apiVersionsGenerator.generateFile();
this.addGeneratedFile(generatedApiVersions);
});

RequestOptionsGenerator requestOptionsGenerator = new RequestOptionsGenerator(
context, context.getPoetClassNameFactory().getRequestOptionsClassName());
GeneratedJavaFile generatedRequestOptions = requestOptionsGenerator.generateFile();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ public ClassName getResponseBodyReaderClassName() {
return ClassName.get(getCorePackage(), "ResponseBodyReader");
}

public ClassName getApiVersionClassName() {
return ClassName.get(getCorePackage(), "ApiVersion");
}

public ClassName getRequestOptionsClassName() {
return ClassName.get(getCorePackage(), "RequestOptions");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.MethodSpec;
import java.util.Map;
import java.util.Optional;
import org.immutables.value.Value;

@Value.Immutable
Expand All @@ -34,6 +35,8 @@ public abstract class GeneratedClientOptions extends AbstractGeneratedJavaFile {

public abstract MethodSpec httpClientWithTimeout();

public abstract Optional<MethodSpec> version();

public abstract Map<VariableId, MethodSpec> variableGetters();

public abstract ClassName builderClassName();
Expand Down
Loading
Loading