Skip to content

Commit

Permalink
Fix List<T> response body return types (#79)
Browse files Browse the repository at this point in the history
Because List<Foo>.class is not valid, we need to use an
Arrays.asList(...) intermediary.

Co-authored-by: (Jonathan) Luke Green <lukeg496@gmail.com>
Co-authored-by: Hugo Pereira <hdcpereira@protonmail.com>
  • Loading branch information
3 people authored Jun 15, 2023
1 parent 95e6cf1 commit 6bce565
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import java.nio.file.Path;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
Expand Down Expand Up @@ -84,6 +85,19 @@ public PackageName toPackage() {
return packageName.resolve(typeName);
}

/**
* If this FQN describes a List, return an Optional of the List element's FQN, or else an empty
* optional.
*/
public Optional<Fqn> listType() {
if (packageName.toString().equalsIgnoreCase("java.util")
&& typeName.toString().equalsIgnoreCase("List")) {
return Optional.of(typeParameters.get(0));
} else {
return Optional.empty();
}
}

public static class Builder {
private PackageName packageName = PackageName.of("java.lang");
private SimpleName typeName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public java.net.http.HttpResponse<? extends java.io.InputStream> httpResponse()
return this.httpResponse;
}
{{#contentTypeName}}
{{#bodyReturnTypeName}}
/**
* Return the deserialized representation of the response body if possible. The body
* is deserialized lazily; if this method is never called, the body is never
Expand All @@ -49,19 +49,34 @@ public java.net.http.HttpResponse<? extends java.io.InputStream> httpResponse()
* @throws java.io.IOException If the response body cannot be deserialized for any
* reason.
*/
public {{{contentTypeName}}} body() throws java.io.IOException {
return objectMapper.readValue(httpResponse.body(), {{{contentTypeName}}}.class);
public {{{bodyReturnTypeName}}} body() throws java.io.IOException {
{{! List<Foo>.class is not valid java, so we use an Array type intermediary. }}
{{#bodyListReturnTypeName}}
return java.util.Arrays.asList(objectMapper.readValue(httpResponse.body(), {{{bodyListReturnTypeName}}}[].class));
{{/bodyListReturnTypeName}}
{{^bodyListReturnTypeName}}
return objectMapper.readValue(httpResponse.body(), {{{bodyReturnTypeName}}}.class);
{{/bodyListReturnTypeName}}
}
{{/contentTypeName}}
{{/bodyReturnTypeName}}
}
""",
"renderAstResponse",
Map.of(
"packageName", astResponse.name().packageName(),
"typeName", astResponse.name().typeName(),
"interfaceName", astResponse.sumTypeName(),
"contentTypeName",
astResponse.contentName().<Object>map(Fqn::toFqpString).orElse(false)));
"packageName",
astResponse.name().packageName(),
"typeName",
astResponse.name().typeName(),
"interfaceName",
astResponse.sumTypeName(),
"bodyReturnTypeName",
astResponse.contentName().<Object>map(Fqn::toFqpString).orElse(false),
"bodyListReturnTypeName",
astResponse
.contentName()
.flatMap(Fqn::listType)
.<Object>map(Fqn::toFqpString)
.orElse(false)));

return new Source(astResponse.name(), content);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@

import io.github.tomboyo.lily.compiler.LilyExtension.LilyTestSupport;
import java.net.URI;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

/** Compiles an open-source OpenAPI document for a real system to help unearth bugs. */
@Disabled("Will not pass until '#74 - Graceful Failure' is finished.")
@ExtendWith(LilyExtension.class)
class CandlepinApiTest {
@Test
Expand Down

0 comments on commit 6bce565

Please sign in to comment.