Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion generators/java/generator-utils/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
dependencies {
api 'com.squareup:javapoet'
api 'com.fern.fern:irV63'
api 'com.fern.fern:irV64'
api 'com.fern.fern:generator-exec-client'
api 'com.fasterxml.jackson.core:jackson-annotations'
api 'com.fasterxml.jackson.core:jackson-databind'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,20 @@ public ClassName visitCustom(CustomPagination custom) {
.getCustomPaginationClassName();
}

@Override
public ClassName visitUri(UriPagination uri) {
return clientGeneratorContext
.getPoetClassNameFactory()
.getPaginationClassName("SyncPagingIterable");
}

@Override
public ClassName visitPath(PathPagination path) {
return clientGeneratorContext
.getPoetClassNameFactory()
.getPaginationClassName("SyncPagingIterable");
}

@Override
public ClassName _visitUnknown(Object unknownType) {
return clientGeneratorContext
Expand Down Expand Up @@ -403,6 +417,50 @@ public TypeName visitCustom(CustomPagination customPagination) {
.convertToTypeName(true, resultUnderlyingType));
}

@Override
public TypeName visitUri(UriPagination uri) {
SnippetAndResultType resultSnippet = getNestedPropertySnippet(
uri.getResults().getPropertyPath().map(path -> path.stream()
.map(PropertyPathItem::getName)
.collect(Collectors.toList())),
uri.getResults().getProperty(),
body.getResponseBodyType());
com.fern.ir.model.types.ContainerType resultContainerType = resultSnippet
.typeReference
.getContainer()
.orElseThrow(() -> new RuntimeException(
"Unexpected non-container pagination result type"));
com.fern.ir.model.types.TypeReference resultUnderlyingType = resultContainerType.visit(
new TypeReferenceUtils.ContainerTypeToUnderlyingType());
return ParameterizedTypeName.get(
pagerClassName,
clientGeneratorContext
.getPoetTypeNameMapper()
.convertToTypeName(true, resultUnderlyingType));
}

@Override
public TypeName visitPath(PathPagination path) {
SnippetAndResultType resultSnippet = getNestedPropertySnippet(
path.getResults().getPropertyPath().map(pathItems -> pathItems.stream()
.map(PropertyPathItem::getName)
.collect(Collectors.toList())),
path.getResults().getProperty(),
body.getResponseBodyType());
com.fern.ir.model.types.ContainerType resultContainerType = resultSnippet
.typeReference
.getContainer()
.orElseThrow(() -> new RuntimeException(
"Unexpected non-container pagination result type"));
com.fern.ir.model.types.TypeReference resultUnderlyingType = resultContainerType.visit(
new TypeReferenceUtils.ContainerTypeToUnderlyingType());
return ParameterizedTypeName.get(
pagerClassName,
clientGeneratorContext
.getPoetTypeNameMapper()
.convertToTypeName(true, resultUnderlyingType));
}

@Override
public TypeName _visitUnknown(Object o) {
throw new RuntimeException("Unknown pagination type " + o);
Expand Down Expand Up @@ -1771,6 +1829,91 @@ public Void visitCustom(CustomPagination customPagination) {
return null;
}

@Override
public Void visitUri(UriPagination uri) {
return visitUriOrPathPagination(
uri.getNextUri(), uri.getResults());
}

@Override
public Void visitPath(PathPagination path) {
return visitUriOrPathPagination(
path.getNextPath(), path.getResults());
}

private Void visitUriOrPathPagination(
ResponseProperty nextProperty, ResponseProperty resultsProperty) {
SnippetAndResultType nextSnippet = getNestedPropertySnippet(
nextProperty.getPropertyPath().map(path -> path.stream()
.map(PropertyPathItem::getName)
.collect(Collectors.toList())),
nextProperty.getProperty(),
body.getResponseBodyType());
CodeBlock nextBlock = CodeBlock.builder()
.add(
"$T $L = $L",
nextSnippet.typeName,
variables.getStartingAfterVariableName(),
variables.getParsedResponseVariableName())
.add(nextSnippet.codeBlock)
.build();
httpResponseBuilder.addStatement(nextBlock);

CodeBlock hasNextPageBlock;
if (nextSnippet.typeReference.getContainer().isPresent()) {
com.fern.ir.model.types.ContainerType containerType =
nextSnippet.typeReference.getContainer().get();
if (containerType.isOptional()) {
hasNextPageBlock = CodeBlock.of("$L.isPresent()", variables.getStartingAfterVariableName());
} else if (containerType.isNullable()) {
hasNextPageBlock = CodeBlock.of("$L != null", variables.getStartingAfterVariableName());
} else {
throw new IllegalStateException(
"Found non-optional, non-nullable container as next page token. This should be impossible "
+ "due to fern check validation. "
+ getContainerDiagnosticString(containerType));
}
} else if (nextSnippet.typeReference.getPrimitive().isPresent()) {
hasNextPageBlock = ZeroValueUtils.isNonzeroValue(
variables.getStartingAfterVariableName(),
nextSnippet.typeReference.getPrimitive().get());
} else {
throw new IllegalStateException(
"Found non-optional, non-primitive as next page token. This should be impossible "
+ "due to fern check validation.");
}

SnippetAndResultType resultSnippet = getNestedPropertySnippet(
resultsProperty.getPropertyPath().map(path -> path.stream()
.map(PropertyPathItem::getName)
.collect(Collectors.toList())),
resultsProperty.getProperty(),
body.getResponseBodyType());
CodeBlock resultBlock = CodeBlock.builder()
.add(
"$T $L = $L",
resultSnippet.typeName,
variables.getResultVariableName(),
variables.getParsedResponseVariableName())
.add(resultSnippet.codeBlock)
.build();
httpResponseBuilder.addStatement(resultBlock);

TypeName responseType = getResponseType(httpEndpoint, clientGeneratorContext);

CodeBlock paginationConstructor = CodeBlock.of(
"new $T($L, $L, $L, $L)",
responseType,
hasNextPageBlock,
variables.getResultVariableName(),
variables.getParsedResponseVariableName(),
getNextPageGetter(endpointName, methodParameters));

handleSuccessfulResult(httpResponseBuilder, paginationConstructor);
endpointMethodBuilder.returns(responseType);
return null;
}

@Override
public Void _visitUnknown(Object unknownType) {
throw new RuntimeException("Unknown pagination type " + unknownType);
Expand Down
8 changes: 4 additions & 4 deletions generators/java/versions.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ com.atlassian.commonmark:commonmark:0.12.1 (1 constraints: 36052a3b)
com.fasterxml.jackson:jackson-bom:2.18.2 (9 constraints: 399efb2b)
com.fasterxml.jackson.core:jackson-annotations:2.18.2 (8 constraints: 307798b9)
com.fasterxml.jackson.core:jackson-core:2.18.2 (8 constraints: f48739fc)
com.fasterxml.jackson.core:jackson-databind:2.18.2 (12 constraints: e7af4b21)
com.fasterxml.jackson.datatype:jackson-datatype-jdk8:2.18.2 (8 constraints: 0866b6c2)
com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.18.2 (3 constraints: 941c125b)
com.fasterxml.jackson.core:jackson-databind:2.18.2 (12 constraints: e8af2d22)
com.fasterxml.jackson.datatype:jackson-datatype-jdk8:2.18.2 (8 constraints: 09666dc3)
com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.18.2 (3 constraints: 951c1d5b)
com.fern.fern:generator-exec-client:0.0.806 (1 constraints: 70059d40)
com.fern.fern:generator-exec-model:0.0.806 (1 constraints: 760ff78e)
com.fern.fern:irV63:63.1.0 (1 constraints: 3c05573b)
com.fern.fern:irV64:64.0.0 (1 constraints: 3c05593b)
com.google.code.findbugs:annotations:3.0.1 (5 constraints: 0b486b6a)
com.google.code.findbugs:jsr305:3.0.2 (6 constraints: 3e53d684)
com.google.errorprone:error_prone_annotations:2.18.0 (5 constraints: 824496e2)
Expand Down
2 changes: 1 addition & 1 deletion generators/java/versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ org.immutables:* = 2.8.8
org.apache.commons:commons-lang3 = 3.18.0

# fern
com.fern.fern:irV63 = 63.1.0
com.fern.fern:irV64 = 64.0.0
com.fern.fern:generator-exec-client = 0.0.806

# testing
Expand Down
Loading