Skip to content
This repository was archived by the owner on Jun 28, 2022. It is now read-only.

Add ConfigV2 Validator #2672

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.api.codegen.util;

import com.google.protobuf.InvalidProtocolBufferException;
import com.google.protobuf.util.JsonFormat;
import com.google.protobuf.util.JsonFormat.Parser;
import com.google.protobuf.util.JsonFormat.Printer;
import com.google.protobuf.util.JsonFormat.TypeRegistry;
import javax.annotation.Nonnull;

public class ConfigNextVersionValidator {
Copy link
Contributor

Choose a reason for hiding this comment

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

This name is a bit odd. What about just ConfigVersionValidator?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sure


public static String CONFIG_V2_MAJOR_VERSION = "2";
public static String CONFIG_V2_VERSION = CONFIG_V2_MAJOR_VERSION + ".0.0"; // "2.0.0"

/**
* Throw {@link IllegalStateException} iff the given input contains fields unknown to the {@link
* com.google.api.codegen.v2.ConfigProto} schema.
*/
public void checkIsNextVersionConfig(@Nonnull com.google.api.codegen.ConfigProto configV1Proto)
Copy link
Contributor

Choose a reason for hiding this comment

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

What about something like validateV2Config? I don't think it is desirable to make the function name version-agnostic (unless the implementation is also version-agnostic)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok!

throws IllegalStateException {
if (!configV1Proto.getConfigSchemaVersion().startsWith(CONFIG_V2_MAJOR_VERSION + ".")
&& !configV1Proto.getConfigSchemaVersion().equals(CONFIG_V2_MAJOR_VERSION)) {
throw new IllegalStateException(
String.format(
"Provided ConfigProto version is %s but should be >= %s",
configV1Proto.getConfigSchemaVersion(), CONFIG_V2_VERSION));
}

try {
// Serializing utils for Config V2.
TypeRegistry typeRegistryV2 =
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it necessary to use a TypeRegistry and json formatting functions? Can we not just use toByteArray, parseFrom(byte[]), and check for unknown fields?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If we use the toByteArray and parseFrom functions, those will still preserve the unknown fields. The thing with the unknown fields is that they can exist at every single level of the protobuf object tree, and I don't feel like writing a tree search function to parse through every child message type and looking for unknown types (i've already been burned trying to implement a BFS haha)

Copy link
Contributor

Choose a reason for hiding this comment

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

What about using a DiscardUnknownFieldsParser? I don't know, but my intuition is that there is an almost-one-liner that does this, something like:

configBytes = config.toByteArray();
configV2Bytes = DiscardUnknownFieldsParser.wrap(ConfigV2.parser())
  .parseFrom(configBytes)
  .toByteArray();
// compare byte arrays ...

Copy link
Contributor

Choose a reason for hiding this comment

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

But - maybe I'm just wrong? Like, does the above fail because of unknown fields other than v1/v2 differences?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oh i didn't see that! i heard that Go had the same thing, but i didn't look hard enough for a java version

Copy link
Contributor Author

Choose a reason for hiding this comment

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

much shorter now :) i must have been looking at stackoverflow posts that are outdated now for help on this

JsonFormat.TypeRegistry.newBuilder()
.add(com.google.api.codegen.v2.ConfigProto.getDescriptor())
.build();
Parser parserV2 = JsonFormat.parser().usingTypeRegistry(typeRegistryV2);
Printer printerV2 = JsonFormat.printer().usingTypeRegistry(typeRegistryV2);

// Serialize and deserialize the Config v1 proto under the Config v2 schema to remove fields
// unknown to Config v2 schema.
com.google.api.codegen.v2.ConfigProto.Builder v2ProtoBuilder =
com.google.api.codegen.v2.ConfigProto.newBuilder();
parserV2.merge(printerV2.print(configV1Proto), v2ProtoBuilder);

// Serializing utils for Config V2.
TypeRegistry typeRegistryV1 =
JsonFormat.TypeRegistry.newBuilder()
.add(com.google.api.codegen.ConfigProto.getDescriptor())
.build();
Printer printerV1 = JsonFormat.printer().usingTypeRegistry(typeRegistryV1);

String v1String = printerV2.print(v2ProtoBuilder.build());
String v2String = printerV1.print(configV1Proto);

// Compare the v1-serialized and v2-serialized strings of the same config proto object.
if (!v1String.equals(v2String)) {
throw new IllegalStateException(
String.format(
"Unknown fields in to ConfigProto v2 in configProto: %s",
configV1Proto.toString()));
}
} catch (InvalidProtocolBufferException e) {
throw new IllegalStateException(e);
}
}
}
66 changes: 66 additions & 0 deletions src/test/java/com/google/api/codegen/util/ConfigValidatorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.api.codegen.util;

import com.google.api.codegen.ConfigProto;
import com.google.api.codegen.FlatteningConfigProto;
import com.google.api.codegen.FlatteningGroupProto;
import com.google.api.codegen.InterfaceConfigProto;
import com.google.api.codegen.MethodConfigProto;
import com.google.protobuf.BoolValue;
import org.junit.Test;

public class ConfigValidatorTest {
private static final ConfigProto validV2Proto =
ConfigProto.newBuilder()
.setConfigSchemaVersion("2.0.0")
// This is a valid field for both Config v1 and Config v2.
.setEnableStringFormatFunctionsOverride(BoolValue.of(true))
.build();

private static final ConfigNextVersionValidator validator = new ConfigNextVersionValidator();

@Test(expected = IllegalStateException.class)
public void testProtoIsNotConfigNextVersion() {
ConfigProto smallProto = validV2Proto.toBuilder().setConfigSchemaVersion("1.0.0").build();
validator.checkIsNextVersionConfig(smallProto);
}

@Test(expected = IllegalStateException.class)
public void testProtoIsNotValid() {
ConfigProto smallProto =
validV2Proto
.toBuilder()
.addInterfaces(
InterfaceConfigProto.newBuilder()
.addMethods(
MethodConfigProto.newBuilder()
.setFlattening(
FlatteningConfigProto.newBuilder()
.addGroups(
FlatteningGroupProto.newBuilder()
.addParameters("flattenedParam")
.build())
.build()))
.build())
.build();
validator.checkIsNextVersionConfig(smallProto);
}

@Test
public void testProtoIsValid() {
validator.checkIsNextVersionConfig(validV2Proto);
}
}