-
Notifications
You must be signed in to change notification settings - Fork 129
Add ConfigV2 Validator #2672
Add ConfigV2 Validator #2672
Changes from 2 commits
8212282
98fe47a
fe7464a
ab64503
9e5281a
56b0d28
617fc9a
43a9a6c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 { | ||
|
||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about something like There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
} | ||
} |
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); | ||
} | ||
} |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure