Skip to content

Commit

Permalink
fix no default
Browse files Browse the repository at this point in the history
  • Loading branch information
Alberto committed Jan 7, 2025
1 parent 079713a commit bbaccf4
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public final class ClientOptionsGenerator extends AbstractFileGenerator {
private final FieldSpec environmentField;
private final GeneratedJavaFile requestOptionsFile;
private final ClientGeneratorContext clientGeneratorContext;
private final FieldSpec apiVersionsField;
private final FieldSpec apiVersionField;

public ClientOptionsGenerator(
ClientGeneratorContext clientGeneratorContext,
Expand All @@ -100,7 +100,7 @@ public ClientOptionsGenerator(
.build();
this.requestOptionsFile = requestOptionsFile;
this.clientGeneratorContext = clientGeneratorContext;
this.apiVersionsField = FieldSpec.builder(
this.apiVersionField = FieldSpec.builder(
clientGeneratorContext.getPoetClassNameFactory().getApiVersionsClassName(),
"version",
Modifier.PRIVATE,
Expand Down Expand Up @@ -164,35 +164,40 @@ public GeneratedClientOptions generateFile() {
apiVersionScheme.visit(new ApiVersionScheme.Visitor<Void>() {
@Override
public Void visitHeader(HeaderApiVersionScheme headerApiVersionScheme) {
constructorBuilder.addParameter(ParameterSpec.builder(
ParameterizedTypeName.get(
ClassName.get(Optional.class),
clientGeneratorContext
.getPoetClassNameFactory()
.getApiVersionsClassName()),
apiVersionsField.name)
.build());

if (headerApiVersionScheme.getValue().getDefault().isPresent()) {
constructorBuilder.addParameter(ParameterSpec.builder(
ParameterizedTypeName.get(
ClassName.get(Optional.class),
clientGeneratorContext
.getPoetClassNameFactory()
.getApiVersionsClassName()),
apiVersionField.name)
.build());
constructorBuilder.addStatement(
"this.$L = $L.orElse($L)",
apiVersionsField.name,
apiVersionsField.name,
apiVersionField.name,
apiVersionField.name,
CodeBlock.of(
"$T.$L",
clientGeneratorContext
.getPoetClassNameFactory()
.getApiVersionsClassName(),
ApiVersionConstants.CURRENT_API_VERSION));
} else {
constructorBuilder.addStatement("this.$L = $L", apiVersionsField.name, apiVersionsField.name);
constructorBuilder.addParameter(ParameterSpec.builder(
clientGeneratorContext
.getPoetClassNameFactory()
.getApiVersionsClassName(),
apiVersionField.name)
.build());
constructorBuilder.addStatement("this.$L = $L", apiVersionField.name, apiVersionField.name);
}

constructorBuilder.addStatement(
"this.$L.put($S,$L)",
HEADERS_FIELD.name,
headerApiVersionScheme.getHeader().getName().getWireValue(),
CodeBlock.of("this.$L.toString()", apiVersionsField.name));
CodeBlock.of("this.$L.toString()", apiVersionField.name));

return null;
}
Expand Down Expand Up @@ -221,8 +226,8 @@ public Void _visitUnknown(Object _o) {
.addMethod(headersFromRequestOptions);

if (clientGeneratorContext.getIr().getApiVersion().isPresent()) {
clientOptionsBuilder.addField(apiVersionsField);
versionsGetter = Optional.of(createGetter(apiVersionsField));
clientOptionsBuilder.addField(apiVersionField);
versionsGetter = Optional.of(createGetter(apiVersionField));
clientOptionsBuilder.addMethod(versionsGetter.get());
}

Expand Down Expand Up @@ -368,7 +373,7 @@ private TypeSpec createBuilder(ClientGeneratorContext context, Map<VariableId, F
ParameterizedTypeName.get(
ClassName.get(Optional.class),
context.getPoetClassNameFactory().getApiVersionsClassName()),
apiVersionsField.name,
apiVersionField.name,
Modifier.PRIVATE)
.build());
builder.addMethod(getVersionBuilder());
Expand Down Expand Up @@ -401,13 +406,13 @@ private MethodSpec getHeaderBuilder() {
}

private MethodSpec getVersionBuilder() {
return MethodSpec.methodBuilder(apiVersionsField.name)
return MethodSpec.methodBuilder(apiVersionField.name)
.addModifiers(Modifier.PUBLIC)
.returns(builderClassName)
.addParameter(
clientGeneratorContext.getPoetClassNameFactory().getApiVersionsClassName(),
apiVersionsField.name)
.addStatement("this.$L = $T.of($L)", apiVersionsField.name, Optional.class, apiVersionsField.name)
apiVersionField.name)
.addStatement("this.$L = $T.of($L)", apiVersionField.name, Optional.class, apiVersionField.name)
.addStatement("return this")
.build();
}
Expand Down Expand Up @@ -480,7 +485,7 @@ private MethodSpec getBuildMethod(ClientGeneratorContext context, Map<VariableId
String returnString = "return new $T($L, $L, $L, $L, this.timeout";

if (context.getIr().getApiVersion().isPresent()) {
argsBuilder.add(apiVersionsField.name);
argsBuilder.add(apiVersionField.name);
returnString = "return new $T($L, $L, $L, $L, this.timeout, $L";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
import com.fern.ir.model.auth.OAuthScheme;
import com.fern.ir.model.commons.NameAndWireValue;
import com.fern.ir.model.http.HttpHeader;
import com.fern.java.AbstractGeneratorContext;
import com.fern.ir.model.ir.ApiVersionScheme;
import com.fern.ir.model.ir.HeaderApiVersionScheme;
import com.fern.java.client.ClientGeneratorContext;
import com.fern.java.generators.AbstractFileGenerator;
import com.fern.java.output.GeneratedJavaFile;
import com.squareup.javapoet.ClassName;
Expand Down Expand Up @@ -56,16 +58,18 @@ public final class RequestOptionsGenerator extends AbstractFileGenerator {

private final List<HttpHeader> additionalHeaders;
private final ClassName builderClassName;
private final ClientGeneratorContext clientGeneratorContext;

public RequestOptionsGenerator(AbstractGeneratorContext<?, ?> generatorContext, ClassName className) {
public RequestOptionsGenerator(ClientGeneratorContext generatorContext, ClassName className) {
this(generatorContext, className, Collections.emptyList());
}

public RequestOptionsGenerator(
AbstractGeneratorContext<?, ?> generatorContext, ClassName className, List<HttpHeader> additionalHeaders) {
ClientGeneratorContext generatorContext, ClassName className, List<HttpHeader> additionalHeaders) {
super(className, generatorContext);
this.builderClassName = className.nestedClass("Builder");
this.additionalHeaders = additionalHeaders;
this.clientGeneratorContext = generatorContext;
}

@Override
Expand All @@ -80,6 +84,41 @@ public GeneratedJavaFile generateFile() {
"$T headers = new $T<>()",
ParameterizedTypeName.get(Map.class, String.class, String.class),
HashMap.class);

FieldSpec apiVersionField = FieldSpec.builder(
ParameterizedTypeName.get(
ClassName.get(Optional.class),
clientGeneratorContext.getPoetClassNameFactory().getApiVersionsClassName()),
"version",
Modifier.PRIVATE,
Modifier.FINAL)
.build();

if (clientGeneratorContext.getIr().getApiVersion().isPresent()) {
ApiVersionScheme apiVersionScheme =
clientGeneratorContext.getIr().getApiVersion().get();

apiVersionScheme.visit(new ApiVersionScheme.Visitor<Void>() {
@Override
public Void visitHeader(HeaderApiVersionScheme headerApiVersionScheme) {
getHeadersCodeBlock
.beginControlFlow("if (this.$N.isPresent())", apiVersionField)
.addStatement(
"headers.put($S,$L)",
headerApiVersionScheme.getHeader().getName().getWireValue(),
CodeBlock.of("this.$L.get().toString()", apiVersionField.name))
.endControlFlow();

return null;
}

@Override
public Void _visitUnknown(Object _o) {
throw new IllegalArgumentException("Received unknown API versioning schema type in IR.");
}
});
}

HeaderHandler headerHandler = new HeaderHandler(requestOptionsTypeSpec, builderTypeSpec, getHeadersCodeBlock);
AuthSchemeHandler authSchemeHandler =
new AuthSchemeHandler(requestOptionsTypeSpec, builderTypeSpec, getHeadersCodeBlock, headerHandler);
Expand Down Expand Up @@ -127,6 +166,31 @@ public GeneratedJavaFile generateFile() {
builderTypeSpec,
fields);

if (clientGeneratorContext.getIr().getApiVersion().isPresent()) {
ApiVersionScheme apiVersionScheme =
clientGeneratorContext.getIr().getApiVersion().get();

apiVersionScheme.visit(new ApiVersionScheme.Visitor<Void>() {
@Override
public Void visitHeader(HeaderApiVersionScheme headerApiVersionScheme) {
createRequestOptionField(
"getVersion",
apiVersionField.toBuilder(),
CodeBlock.of("$T.empty()", Optional.class),
requestOptionsTypeSpec,
builderTypeSpec,
fields);

return null;
}

@Override
public Void _visitUnknown(Object _o) {
throw new IllegalArgumentException("Received unknown API versioning schema type in IR.");
}
});
}

FieldSpec timeoutField = timeoutFieldBuilder.build();
FieldSpec timeUnitField = timeoutTimeUnitFieldBuilder.build();
builderTypeSpec.addMethod(MethodSpec.methodBuilder(timeoutField.name)
Expand All @@ -147,6 +211,18 @@ public GeneratedJavaFile generateFile() {
.returns(builderClassName)
.build());

if (clientGeneratorContext.getIr().getApiVersion().isPresent()) {
builderTypeSpec.addMethod(MethodSpec.methodBuilder(apiVersionField.name)
.addModifiers(Modifier.PUBLIC)
.addParameter(
clientGeneratorContext.getPoetClassNameFactory().getApiVersionsClassName(),
apiVersionField.name)
.addStatement("this.$L = Optional.of($L)", apiVersionField.name, apiVersionField.name)
.addStatement("return this")
.returns(builderClassName)
.build());
}

String constructorArgs =
fields.stream().map(field -> field.builderField.name).collect(Collectors.joining(", "));
builderTypeSpec.addMethod(MethodSpec.methodBuilder("build")
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.

Loading

0 comments on commit bbaccf4

Please sign in to comment.