Skip to content
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ nosetests.xml
.settings
.DS_Store
.classpath
.vscode

# API key file containing value of GOOGLE_API_KEY for integration tests
api_key
Expand All @@ -79,4 +80,4 @@ monorepo
*.tfstate.*.backup
*.tfstate.lock.info

.jqwik-database
.jqwik-database
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
import com.google.api.generator.gapic.model.Service;
import com.google.api.generator.gapic.utils.JavaStyle;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Strings;
import com.google.common.collect.BiMap;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
Expand Down Expand Up @@ -723,7 +724,7 @@
.build());
}

private Expr createBodyFieldsExtractorClassInstance(

Check failure on line 727 in sdk-platform-java/gapic-generator-java/src/main/java/com/google/api/generator/gapic/composer/rest/HttpJsonServiceStubClassComposer.java

View check run for this annotation

SonarQubeCloud / [gapic-generator-java-root] SonarCloud Code Analysis

Refactor this method to reduce its Cognitive Complexity from 17 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=googleapis_google-cloud-java_generator&issues=AZ3erlvCdkr-LGEBsu0L&open=AZ3erlvCdkr-LGEBsu0L&pullRequest=12940
Method method,
TypeNode extractorReturnType,
Set<HttpBinding> httpBindingFieldNames,
Expand Down Expand Up @@ -763,7 +764,16 @@
// Handle foo.bar cases by descending into the subfields.
MethodInvocationExpr.Builder requestFieldMethodExprBuilder =
MethodInvocationExpr.builder().setExprReferenceExpr(prevExpr);
bodyParamName = JavaStyle.toLowerCamelCase(httpBindingFieldName.name());
// Use explicit json_name if defined in the proto, prioritizing the actual wire name
// over Java-escaped identifiers. Note that trailing underscores (e.g., 'case_') result from:
// 1. protoc-gen-java:
// https://github.com/protocolbuffers/protobuf/blob/cecbbf41e43634c7c5b940dd336aa81b31fd4e5d/src/google/protobuf/compiler/java/names.cc#L189-L195
// 2. gapic-generator-java Keyword implementation:
// com/google/api/generator/engine/lexicon/Keyword.java#L92-L94
bodyParamName =
!Strings.isNullOrEmpty(httpBindingFieldName.jsonName())
? httpBindingFieldName.jsonName()
: JavaStyle.toLowerCamelCase(httpBindingFieldName.name());
String[] descendantFields = httpBindingFieldName.name().split("\\.");
if (asteriskBody && descendantFields.length > 1) {
// This is the `body: "*"` case, do not clean nested body fields as it a very rare, not
Expand Down Expand Up @@ -827,7 +837,7 @@
.build();
}

private Expr createFieldsExtractorClassInstance(

Check failure on line 840 in sdk-platform-java/gapic-generator-java/src/main/java/com/google/api/generator/gapic/composer/rest/HttpJsonServiceStubClassComposer.java

View check run for this annotation

SonarQubeCloud / [gapic-generator-java-root] SonarCloud Code Analysis

Refactor this method to reduce its Cognitive Complexity from 16 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=googleapis_google-cloud-java_generator&issues=AZ3QFk4Qff0uYgqTznWy&open=AZ3QFk4Qff0uYgqTznWy&pullRequest=12940
Method method,
TypeNode extractorReturnType,
Set<HttpBinding> httpBindingFieldNames,
Expand Down Expand Up @@ -926,7 +936,12 @@
paramsPutArgs.add(
ValueExpr.withValue(
StringObjectValue.withValue(
JavaStyle.toLowerCamelCase(httpBindingFieldName.name()))));
// Use explicit json_name if defined in the proto, prioritizing the actual wire
// name over Java-escaped identifiers (e.g., avoiding 'case_' generated to prevent
// keywords conflict).
(httpBindingFieldName.jsonName() != null)
Comment thread
diegomarquezp marked this conversation as resolved.
? httpBindingFieldName.jsonName()
: JavaStyle.toLowerCamelCase(httpBindingFieldName.name()))));
paramsPutArgs.add(requestBuilderExpr);

Expr paramsPutExpr =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ public abstract class Field {
// resolution behavior. For more context, please see the invocation site of the setter method.
public abstract String originalName();

@Nullable
public abstract String jsonName();

public abstract TypeNode type();

// If the field is annotated with google.api.field_behavior = REQUIRED, then this is true. This is
Expand Down Expand Up @@ -92,6 +95,7 @@ public boolean equals(Object o) {
Field other = (Field) o;
return name().equals(other.name())
&& originalName().equals(other.originalName())
&& Objects.equals(jsonName(), other.jsonName())
&& type().equals(other.type())
&& isRequired() == other.isRequired()
&& fieldInfoFormat() == other.fieldInfoFormat()
Expand All @@ -109,6 +113,7 @@ && isProto3Optional() == other.isProto3Optional()
public int hashCode() {
return 17 * name().hashCode()
+ 31 * originalName().hashCode()
+ (jsonName() == null ? 0 : jsonName().hashCode())
+ 19 * type().hashCode()
+ (isMessage() ? 1 : 0) * 23
+ (isEnum() ? 1 : 0) * 29
Expand Down Expand Up @@ -141,6 +146,8 @@ public abstract static class Builder {

public abstract Builder setOriginalName(String originalName);

public abstract Builder setJsonName(String jsonName);

public abstract Builder setType(TypeNode type);

public abstract Builder setIsRequired(boolean isRequired);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import com.google.api.generator.gapic.utils.JavaStyle;
import com.google.auto.value.AutoValue;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableSet;
import java.util.LinkedHashMap;
import java.util.List;
Expand All @@ -42,6 +43,11 @@ public abstract static class HttpBinding implements Comparable<HttpBinding> {

abstract String lowerCamelName();

// The dot-separated json_name of the field.
// e.g. parent.iceberg-catalog-id
@Nullable
public abstract String jsonName();

// An object that contains all info of the leaf level field
@Nullable
public abstract Field field();
Expand Down Expand Up @@ -70,6 +76,8 @@ public abstract static class Builder {

public abstract HttpBindings.HttpBinding.Builder setName(String name);

public abstract HttpBindings.HttpBinding.Builder setJsonName(String jsonName);

public abstract HttpBindings.HttpBinding.Builder setField(Field field);

abstract HttpBindings.HttpBinding.Builder setLowerCamelName(String lowerCamelName);
Expand Down Expand Up @@ -133,9 +141,11 @@ public List<String> lowerCamelAdditionalPatterns() {
private static String lowerCamelPattern(String originalPattern, Set<HttpBinding> pathParameters) {
String lowerCamelPattern = originalPattern;
for (HttpBinding pathParam : pathParameters) {
lowerCamelPattern =
lowerCamelPattern.replaceAll(
"\\{" + pathParam.name(), "{" + JavaStyle.toLowerCamelCase(pathParam.name()));
String replacement =
!Strings.isNullOrEmpty(pathParam.jsonName())
? pathParam.jsonName()
: JavaStyle.toLowerCamelCase(pathParam.name());
lowerCamelPattern = lowerCamelPattern.replaceAll("\\{" + pathParam.name(), "{" + replacement);
}
return lowerCamelPattern;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.google.api.generator.gapic.model.HttpBindings;
import com.google.api.generator.gapic.model.HttpBindings.HttpBinding;
import com.google.api.generator.gapic.model.Message;
import com.google.api.generator.gapic.utils.JavaStyle;
import com.google.api.pathtemplate.PathTemplate;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
Expand All @@ -29,8 +30,10 @@
import com.google.common.collect.Sets;
import com.google.protobuf.DescriptorProtos.MethodOptions;
import com.google.protobuf.Descriptors.MethodDescriptor;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
Expand Down Expand Up @@ -126,7 +129,7 @@
.build();
}

private static Set<HttpBinding> validateAndConstructHttpBindings(

Check failure on line 132 in sdk-platform-java/gapic-generator-java/src/main/java/com/google/api/generator/gapic/protoparser/HttpRuleParser.java

View check run for this annotation

SonarQubeCloud / [gapic-generator-java-root] SonarCloud Code Analysis

Refactor this method to reduce its Cognitive Complexity from 18 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=googleapis_google-cloud-java_generator&issues=AZ3P2XzoMVN7j-fsl9qM&open=AZ3P2XzoMVN7j-fsl9qM&pullRequest=12940
Set<String> paramNames,
Message inputMessage,
Map<String, Message> messageTypes,
Expand All @@ -144,10 +147,24 @@
continue;
}
Message nestedMessage = inputMessage;
List<String> jsonNameParts = new ArrayList<>();
for (int i = 0; i < subFields.length; i++) {
String subFieldName = subFields[i];
Field field = nestedMessage.fieldMap().get(subFieldName);
Preconditions.checkState(
field != null,
"Expected message %s to contain field %s but none found",
nestedMessage.name(),

Check warning on line 157 in sdk-platform-java/gapic-generator-java/src/main/java/com/google/api/generator/gapic/protoparser/HttpRuleParser.java

View check run for this annotation

SonarQubeCloud / [gapic-generator-java-root] SonarCloud Code Analysis

Invoke method(s) only conditionally.

See more on https://sonarcloud.io/project/issues?id=googleapis_google-cloud-java_generator&issues=AZ3P4UEL91BcPujooV-Q&open=AZ3P4UEL91BcPujooV-Q&pullRequest=12940
subFieldName);

// Each component of the JSON name uses the json_name annotation of the field,
// or default to the field name
jsonNameParts.add(
!Strings.isNullOrEmpty(field.jsonName())
? field.jsonName()
: JavaStyle.toLowerCamelCase(field.name()));

if (i < subFields.length - 1) {
Field field = nestedMessage.fieldMap().get(subFieldName);
nestedMessage = messageTypes.get(field.type().reference().fullName());
Preconditions.checkNotNull(
nestedMessage,
Expand All @@ -160,9 +177,12 @@
checkHttpFieldIsValid(subFieldName, nestedMessage, false);
patternSampleValue = patternSampleValues.get(paramName);
}
Field field = nestedMessage.fieldMap().get(subFieldName);
httpBindings.add(
httpBindingBuilder.setValuePattern(patternSampleValue).setField(field).build());
httpBindingBuilder
.setValuePattern(patternSampleValue)
.setField(field)
.setJsonName(String.join(".", jsonNameParts))
.build());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1181,6 +1181,7 @@ private static Field parseField(
return fieldBuilder
.setName(actualFieldName)
.setOriginalName(fieldDescriptor.getName())
.setJsonName(fieldDescriptor.getJsonName())
.setType(TypeParser.parseType(fieldDescriptor))
.setIsMessage(fieldDescriptor.getJavaType() == FieldDescriptor.JavaType.MESSAGE)
.setIsEnum(fieldDescriptor.getJavaType() == FieldDescriptor.JavaType.ENUM)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ public class HttpJsonEchoStub extends EchoStub {
})
.setRequestBodyExtractor(
request ->
ProtoRestSerializer.create().toBody("case_", request.getCase(), false))
ProtoRestSerializer.create().toBody("case", request.getCase(), false))
.build())
.setResponseParser(
ProtoMessageResponseParser.<Case>newBuilder()
Expand Down
Loading
Loading