From 5f46bdcf85ee2e4f4eeed97ae871c84adf9f1d1c Mon Sep 17 00:00:00 2001 From: Mira Leung Date: Fri, 9 Oct 2020 19:38:46 -0700 Subject: [PATCH] [ggj][codegen] fix: sort message fields in asc. index order (#382) * fix: support LRO package.name annotations * feat: add microgenerator rules for cloud/asset * fix: update CircleCI * fix: sort message fields in asc. index order * fix: update parser test method sig order * fix: update circleci config * fix: linter --- .../composer/ServiceStubSettingsClassComposer.java | 3 ++- .../google/api/generator/gapic/protoparser/Parser.java | 8 +++++--- test/integration/BUILD.bazel | 10 +++++----- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/google/api/generator/gapic/composer/ServiceStubSettingsClassComposer.java b/src/main/java/com/google/api/generator/gapic/composer/ServiceStubSettingsClassComposer.java index 06058813f5..5ed2d7e28b 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/ServiceStubSettingsClassComposer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/ServiceStubSettingsClassComposer.java @@ -360,13 +360,14 @@ private static List createPagingStaticAssignExprs( Preconditions.checkState( field != null, String.format("Null field found for message %s", pagedResponseMessage.name())); - if (field.isRepeated()) { + if (field.isRepeated() && !field.isMap()) { // Field is currently a List-type. Preconditions.checkState( !field.type().reference().generics().isEmpty(), String.format("No generics found for field reference %s", field.type().reference())); repeatedResponseType = TypeNode.withReference(field.type().reference().generics().get(0)); repeatedFieldName = field.name(); + break; } } Preconditions.checkNotNull( diff --git a/src/main/java/com/google/api/generator/gapic/protoparser/Parser.java b/src/main/java/com/google/api/generator/gapic/protoparser/Parser.java index ec7d6a83d5..43e7e7c435 100644 --- a/src/main/java/com/google/api/generator/gapic/protoparser/Parser.java +++ b/src/main/java/com/google/api/generator/gapic/protoparser/Parser.java @@ -364,9 +364,11 @@ static String sanitizeDefaultHost(String rawDefaultHost) { } private static List parseFields(Descriptor messageDescriptor) { - return messageDescriptor.getFields().stream() - .map(f -> parseField(f, messageDescriptor)) - .collect(Collectors.toList()); + List fields = new ArrayList<>(messageDescriptor.getFields()); + // Sort by ascending field index order. This is important for paged responses, where the first + // repeated type is taken. + fields.sort((f1, f2) -> f1.getIndex() - f2.getIndex()); + return fields.stream().map(f -> parseField(f, messageDescriptor)).collect(Collectors.toList()); } private static Field parseField(FieldDescriptor fieldDescriptor, Descriptor messageDescriptor) { diff --git a/test/integration/BUILD.bazel b/test/integration/BUILD.bazel index 60a4ca6aa9..af5762b6e8 100644 --- a/test/integration/BUILD.bazel +++ b/test/integration/BUILD.bazel @@ -1,3 +1,8 @@ +load( + "@com_google_googleapis_imports//:imports.bzl", + java_gapic_library = "java_gapic_library2", +) + package(default_visibility = ["//visibility:public"]) #################################################### @@ -5,11 +10,6 @@ package(default_visibility = ["//visibility:public"]) #################################################### # These will eventually go away once more APIs in googleapis have been migrated to the microgenerator. -load( - "@com_google_googleapis_imports//:imports.bzl", - java_gapic_library = "java_gapic_library2", -) - java_gapic_library( name = "asset_java_gapic", srcs = ["@com_google_googleapis//google/cloud/asset/v1:asset_proto_with_info"],