Skip to content

feat(gax): add StringHttpResponseParser - #14089

Closed
whowes wants to merge 1 commit into
whowes/http-content-supportfrom
whowes/string-http-response-parser
Closed

feat(gax): add StringHttpResponseParser#14089
whowes wants to merge 1 commit into
whowes/http-content-supportfrom
whowes/string-http-response-parser

Conversation

@whowes

@whowes whowes commented Aug 17, 2026

Copy link
Copy Markdown
Contributor

Currently gax-httpjson includes only one HTTP response parser: ProtoMessageResponseParser, which assumes responses can be parsed into protos.

Resumable upload HTTP operations will return non-proto responses so we'll need a more general HTTP parser to be able to use the existing HTTP stack to talk to the upload service.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a new StringHttpResponseParser class and its corresponding unit tests to parse HTTP response bodies as UTF-8 strings. The review feedback suggests making the parser class and its factory method public to allow usage by external packages and client libraries. Additionally, it is recommended to use try-with-resources when parsing from a Reader to ensure the stream is properly closed and prevent potential resource leaks.

Comment on lines +43 to +49
class StringHttpResponseParser implements HttpResponseParser<String> {

private static final StringHttpResponseParser INSTANCE = new StringHttpResponseParser();

static StringHttpResponseParser create() {
return INSTANCE;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

To allow this parser to be used by generated client libraries or other packages (e.g., for resumable uploads in storage or other services), the class and its static factory method create() should be public instead of package-private.

Suggested change
class StringHttpResponseParser implements HttpResponseParser<String> {
private static final StringHttpResponseParser INSTANCE = new StringHttpResponseParser();
static StringHttpResponseParser create() {
return INSTANCE;
}
public class StringHttpResponseParser implements HttpResponseParser<String> {
private static final StringHttpResponseParser INSTANCE = new StringHttpResponseParser();
public static StringHttpResponseParser create() {
return INSTANCE;
}

Comment on lines +68 to +74
public String parse(Reader httpContent, TypeRegistry registry) {
try {
return CharStreams.toString(httpContent);
} catch (IOException e) {
throw new RestSerializationException("Failed to read response body as string", e);
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The Reader passed to parse is not closed. While some callers might close it, it is safer to use a try-with-resources block to ensure the Reader is closed and prevent potential resource leaks, especially since CharStreams.toString does not close the stream.

Suggested change
public String parse(Reader httpContent, TypeRegistry registry) {
try {
return CharStreams.toString(httpContent);
} catch (IOException e) {
throw new RestSerializationException("Failed to read response body as string", e);
}
}
@Override
public String parse(Reader httpContent, TypeRegistry registry) {
try (Reader reader = httpContent) {
return CharStreams.toString(reader);
} catch (IOException e) {
throw new RestSerializationException("Failed to read response body as string", e);
}
}

@whowes whowes closed this Aug 17, 2026
@sonarqubecloud

Copy link
Copy Markdown

@sonarqubecloud

Copy link
Copy Markdown

Quality Gate Failed Quality Gate failed for 'gapic-generator-java-root'

Failed conditions
0.0% Coverage on New Code (required ≥ 80%)

See analysis details on SonarQube Cloud

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant