com.google.protobuf.Descriptors.DescriptorValidationException Java Examples

The following examples show how to use com.google.protobuf.Descriptors.DescriptorValidationException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ServiceResolver.java    From grpc-swagger with MIT License 6 votes vote down vote up
/**
 * Recursively constructs file descriptors for all dependencies of the supplied proto and returns
 * a {@link FileDescriptor} for the supplied proto itself. For maximal efficiency, reuse the
 * descriptorCache argument across calls.
 */
private static FileDescriptor descriptorFromProto(
        FileDescriptorProto descriptorProto,
        ImmutableMap<String, FileDescriptorProto> descriptorProtoIndex,
        Map<String, FileDescriptor> descriptorCache) throws DescriptorValidationException {
    // First, check the cache.
    String descriptorName = descriptorProto.getName();
    if (descriptorCache.containsKey(descriptorName)) {
        return descriptorCache.get(descriptorName);
    }

    // Then, fetch all the required dependencies recursively.
    ImmutableList.Builder<FileDescriptor> dependencies = ImmutableList.builder();
    for (String dependencyName : descriptorProto.getDependencyList()) {
        if (!descriptorProtoIndex.containsKey(dependencyName)) {
            throw new IllegalArgumentException("Could not find dependency: " + dependencyName);
        }
        FileDescriptorProto dependencyProto = descriptorProtoIndex.get(dependencyName);
        dependencies.add(descriptorFromProto(dependencyProto, descriptorProtoIndex, descriptorCache));
    }

    // Finally, construct the actual descriptor.
    FileDescriptor[] empty = new FileDescriptor[0];
    return FileDescriptor.buildFrom(descriptorProto, dependencies.build().toArray(empty));
}
 
Example #2
Source File: ServiceResolver.java    From milkman with MIT License 6 votes vote down vote up
/** Creates a resolver which searches the supplied {@link FileDescriptorSet}. */
public static ServiceResolver fromFileDescriptorSet(FileDescriptorSet descriptorSet) {
  ImmutableMap<String, FileDescriptorProto> descriptorProtoIndex =
      computeDescriptorProtoIndex(descriptorSet);
  Map<String, FileDescriptor> descriptorCache = new HashMap<>();

  ImmutableList.Builder<FileDescriptor> result = ImmutableList.builder();
  for (FileDescriptorProto descriptorProto : descriptorSet.getFileList()) {
    try {
      result.add(descriptorFromProto(descriptorProto, descriptorProtoIndex, descriptorCache));
    } catch (DescriptorValidationException e) {
      logger.warn("Skipped descriptor " + descriptorProto.getName() + " due to error", e);
      continue;
    }
  }
  return new ServiceResolver(result.build());
}
 
Example #3
Source File: ServiceResolver.java    From milkman with MIT License 6 votes vote down vote up
/**
 * Recursively constructs file descriptors for all dependencies of the supplied proto and returns
 * a {@link FileDescriptor} for the supplied proto itself. For maximal efficiency, reuse the
 * descriptorCache argument across calls.
 */
private static FileDescriptor descriptorFromProto(
    FileDescriptorProto descriptorProto,
    ImmutableMap<String, FileDescriptorProto> descriptorProtoIndex,
    Map<String, FileDescriptor> descriptorCache) throws DescriptorValidationException {
  // First, check the cache.
  String descritorName = descriptorProto.getName();
  if (descriptorCache.containsKey(descritorName)) {
    return descriptorCache.get(descritorName);
  }

  // Then, fetch all the required dependencies recursively.
  ImmutableList.Builder<FileDescriptor> dependencies = ImmutableList.builder();
  for (String dependencyName : descriptorProto.getDependencyList()) {
    if (!descriptorProtoIndex.containsKey(dependencyName)) {
      throw new IllegalArgumentException("Could not find dependency: " + dependencyName);
    }
    FileDescriptorProto dependencyProto = descriptorProtoIndex.get(dependencyName);
    dependencies.add(descriptorFromProto(dependencyProto, descriptorProtoIndex, descriptorCache));
  }

  // Finally, construct the actual descriptor.
  FileDescriptor[] empty = new FileDescriptor[0];
  return FileDescriptor.buildFrom(descriptorProto, dependencies.build().toArray(empty));
}
 
Example #4
Source File: ServiceResolver.java    From grpc-swagger with MIT License 6 votes vote down vote up
/**
 * Creates a resolver which searches the supplied {@link FileDescriptorSet}.
 */
public static ServiceResolver fromFileDescriptorSet(FileDescriptorSet descriptorSet) {
    ImmutableMap<String, FileDescriptorProto> descriptorProtoIndex =
            computeDescriptorProtoIndex(descriptorSet);
    Map<String, FileDescriptor> descriptorCache = new HashMap<>();

    ImmutableList.Builder<FileDescriptor> result = ImmutableList.builder();
    for (FileDescriptorProto descriptorProto : descriptorSet.getFileList()) {
        try {
            result.add(descriptorFromProto(descriptorProto, descriptorProtoIndex, descriptorCache));
        } catch (DescriptorValidationException e) {
            logger.warn("Skipped descriptor " + descriptorProto.getName() + " due to error", e);
        }
    }
    return new ServiceResolver(result.build());
}
 
Example #5
Source File: ServiceResolver.java    From grpc-swagger with MIT License 6 votes vote down vote up
/**
 * Recursively constructs file descriptors for all dependencies of the supplied proto and returns
 * a {@link FileDescriptor} for the supplied proto itself. For maximal efficiency, reuse the
 * descriptorCache argument across calls.
 */
private static FileDescriptor descriptorFromProto(
        FileDescriptorProto descriptorProto,
        ImmutableMap<String, FileDescriptorProto> descriptorProtoIndex,
        Map<String, FileDescriptor> descriptorCache) throws DescriptorValidationException {
    // First, check the cache.
    String descriptorName = descriptorProto.getName();
    if (descriptorCache.containsKey(descriptorName)) {
        return descriptorCache.get(descriptorName);
    }

    // Then, fetch all the required dependencies recursively.
    ImmutableList.Builder<FileDescriptor> dependencies = ImmutableList.builder();
    for (String dependencyName : descriptorProto.getDependencyList()) {
        if (!descriptorProtoIndex.containsKey(dependencyName)) {
            throw new IllegalArgumentException("Could not find dependency: " + dependencyName);
        }
        FileDescriptorProto dependencyProto = descriptorProtoIndex.get(dependencyName);
        dependencies.add(descriptorFromProto(dependencyProto, descriptorProtoIndex, descriptorCache));
    }

    // Finally, construct the actual descriptor.
    FileDescriptor[] empty = new FileDescriptor[0];
    return FileDescriptor.buildFrom(descriptorProto, dependencies.build().toArray(empty));
}
 
Example #6
Source File: DynamicProtoUtil.java    From rejoiner with Apache License 2.0 6 votes vote down vote up
/**
 * Encodes the data portion of an ExecutionResult as ByteString.
 *
 * <p>The FileDescriptorSet must contain a message with the name "{operationName}Response". This
 * message will be populated with data from the execution result and encoded as a ByteString.
 */
public static ByteString encodeResponse(
    String operationName, FileDescriptorSet fileDescriptorSet, ExecutionResult executionResult) {
  try {
    // TODO: Support multiple FileDescriptors in FileDescriptorSet
    FileDescriptor fileDescriptor =
        FileDescriptor.buildFrom(fileDescriptorSet.getFileList().get(0), new FileDescriptor[] {});

    Descriptor messageType = fileDescriptor.findMessageTypeByName(operationName + "Response");

    Message message = DynamicMessage.parseFrom(messageType, ByteString.EMPTY);
    Message responseData = QueryResponseToProto.buildMessage(message, executionResult.getData());

    return responseData.toByteString();
  } catch (DescriptorValidationException | InvalidProtocolBufferException e) {
    e.printStackTrace();
    throw new RuntimeException(e);
  }
}
 
Example #7
Source File: ServiceResolver.java    From grpc-swagger with MIT License 6 votes vote down vote up
/**
 * Creates a resolver which searches the supplied {@link FileDescriptorSet}.
 */
public static ServiceResolver fromFileDescriptorSet(FileDescriptorSet descriptorSet) {
    ImmutableMap<String, FileDescriptorProto> descriptorProtoIndex =
            computeDescriptorProtoIndex(descriptorSet);
    Map<String, FileDescriptor> descriptorCache = new HashMap<>();

    ImmutableList.Builder<FileDescriptor> result = ImmutableList.builder();
    for (FileDescriptorProto descriptorProto : descriptorSet.getFileList()) {
        try {
            result.add(descriptorFromProto(descriptorProto, descriptorProtoIndex, descriptorCache));
        } catch (DescriptorValidationException e) {
            logger.warn("Skipped descriptor " + descriptorProto.getName() + " due to error", e);
        }
    }
    return new ServiceResolver(result.build());
}
 
Example #8
Source File: DescriptorsTest.java    From travelguide with Apache License 2.0 6 votes vote down vote up
public void testInvalidPublicDependency() throws Exception {
  FileDescriptorProto fooProto = FileDescriptorProto.newBuilder()
      .setName("foo.proto") .build();
  FileDescriptorProto barProto = FileDescriptorProto.newBuilder()
      .setName("boo.proto")
      .addDependency("foo.proto")
      .addPublicDependency(1)  // Error, should be 0.
      .build();
  FileDescriptor fooFile = Descriptors.FileDescriptor.buildFrom(fooProto,
      new FileDescriptor[0]);
  try {
    Descriptors.FileDescriptor.buildFrom(barProto,
        new FileDescriptor[] {fooFile});
    fail("DescriptorValidationException expected");
  } catch (DescriptorValidationException e) {
    assertTrue(
        e.getMessage().indexOf("Invalid public dependency index.") != -1);
  }
}
 
Example #9
Source File: DynamicSchema.java    From protobuf-dynamic with Apache License 2.0 5 votes vote down vote up
/**
 * Builds a dynamic schema
 * 
 * @return the schema object
 * @throws DescriptorValidationException
 */
public DynamicSchema build() throws DescriptorValidationException {
	FileDescriptorSet.Builder fileDescSetBuilder = FileDescriptorSet.newBuilder();
	fileDescSetBuilder.addFile(mFileDescProtoBuilder.build());
	fileDescSetBuilder.mergeFrom(mFileDescSetBuilder.build());
	return new DynamicSchema(fileDescSetBuilder.build());
}
 
Example #10
Source File: DynamicSchema.java    From protobuf-dynamic with Apache License 2.0 5 votes vote down vote up
private DynamicSchema(FileDescriptorSet fileDescSet) throws DescriptorValidationException {
	mFileDescSet = fileDescSet;
	Map<String,FileDescriptor> fileDescMap = init(fileDescSet);
	
	Set<String> msgDupes = new HashSet<String>();
	Set<String> enumDupes = new HashSet<String>();
	for (FileDescriptor fileDesc : fileDescMap.values()) {
		for (Descriptor msgType : fileDesc.getMessageTypes()) addMessageType(msgType, null, msgDupes, enumDupes);			
		for (EnumDescriptor enumType : fileDesc.getEnumTypes()) addEnumType(enumType, null, enumDupes);						
	}
	
	for (String msgName : msgDupes) mMsgDescriptorMapShort.remove(msgName);
	for (String enumName : enumDupes) mEnumDescriptorMapShort.remove(enumName);
}
 
Example #11
Source File: DynamicSchema.java    From protobuf-dynamic with Apache License 2.0 5 votes vote down vote up
/**
 * Parses a serialized schema descriptor (from input stream; closes the stream)
 * 
 * @param schemaDescIn the descriptor input stream
 * @return the schema object
 * @throws DescriptorValidationException
 * @throws IOException
 */
public static DynamicSchema parseFrom(InputStream schemaDescIn) throws DescriptorValidationException, IOException {
	try {
		int len;
		byte[] buf = new byte[4096];
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		while ((len = schemaDescIn.read(buf)) > 0) baos.write(buf, 0, len);
		return parseFrom(baos.toByteArray());
	}
	finally {
		schemaDescIn.close();
	}
}
 
Example #12
Source File: DescriptorsTest.java    From travelguide with Apache License 2.0 5 votes vote down vote up
public void testHiddenDependency() throws Exception {
  FileDescriptorProto barProto = FileDescriptorProto.newBuilder()
      .setName("bar.proto")
      .addMessageType(DescriptorProto.newBuilder().setName("Bar"))
      .build();
  FileDescriptorProto forwardProto = FileDescriptorProto.newBuilder()
      .setName("forward.proto")
      .addDependency("bar.proto")
      .build();
  FileDescriptorProto fooProto = FileDescriptorProto.newBuilder()
      .setName("foo.proto")
      .addDependency("forward.proto")
      .addMessageType(DescriptorProto.newBuilder()
          .setName("Foo")
          .addField(FieldDescriptorProto.newBuilder()
              .setLabel(FieldDescriptorProto.Label.LABEL_OPTIONAL)
              .setTypeName("Bar")
              .setName("bar")
              .setNumber(1)))
      .build();
  FileDescriptor barFile = Descriptors.FileDescriptor.buildFrom(
      barProto, new FileDescriptor[0]);
  FileDescriptor forwardFile = Descriptors.FileDescriptor.buildFrom(
      forwardProto, new FileDescriptor[] {barFile});

  try {
    Descriptors.FileDescriptor.buildFrom(
        fooProto, new FileDescriptor[] {forwardFile});
    fail("DescriptorValidationException expected");
  } catch (DescriptorValidationException e) {
    assertTrue(e.getMessage().indexOf("Bar") != -1);
    assertTrue(e.getMessage().indexOf("is not defined") != -1);
  }
}
 
Example #13
Source File: DescriptorsTest.java    From travelguide with Apache License 2.0 5 votes vote down vote up
/**
 * Tests that the DescriptorValidationException works as intended.
 */
public void testDescriptorValidatorException() throws Exception {
  FileDescriptorProto fileDescriptorProto = FileDescriptorProto.newBuilder()
    .setName("foo.proto")
    .addMessageType(DescriptorProto.newBuilder()
    .setName("Foo")
      .addField(FieldDescriptorProto.newBuilder()
        .setLabel(FieldDescriptorProto.Label.LABEL_OPTIONAL)
        .setType(FieldDescriptorProto.Type.TYPE_INT32)
        .setName("foo")
        .setNumber(1)
        .setDefaultValue("invalid")
        .build())
      .build())
    .build();
  try {
    Descriptors.FileDescriptor.buildFrom(fileDescriptorProto,
        new FileDescriptor[0]);
    fail("DescriptorValidationException expected");
  } catch (DescriptorValidationException e) {
    // Expected; check that the error message contains some useful hints
    assertTrue(e.getMessage().indexOf("foo") != -1);
    assertTrue(e.getMessage().indexOf("Foo") != -1);
    assertTrue(e.getMessage().indexOf("invalid") != -1);
    assertTrue(e.getCause() instanceof NumberFormatException);
    assertTrue(e.getCause().getMessage().indexOf("invalid") != -1);
  }
}
 
Example #14
Source File: ProtobufStorageDescriptionHelper.java    From sql-layer with GNU Affero General Public License v3.0 5 votes vote down vote up
static ProtobufRowConverter buildRowConverter(HasStorage object, FileDescriptorProto fileProto) {
    Group group = (Group) object;
    FileDescriptor fileDescriptor;
    try {
        fileDescriptor = FileDescriptor.buildFrom(fileProto, DEPENDENCIES);
    }
    catch (DescriptorValidationException ex) {
        throw new ProtobufBuildException(ex);
    }
    return ProtobufRowConverter.forGroup(group, fileDescriptor);
}
 
Example #15
Source File: ProtobufStorageDescriptionHelper.java    From sql-layer with GNU Affero General Public License v3.0 5 votes vote down vote up
static ProtobufRowDataConverter buildRowDataConverter(HasStorage object,
                                               FileDescriptorProto fileProto) {
    Group group = (Group)object;
    FileDescriptor fileDescriptor;
    try {
        fileDescriptor = FileDescriptor.buildFrom(fileProto, DEPENDENCIES);
    }
    catch (DescriptorValidationException ex) {
        throw new ProtobufBuildException(ex);
    }
    return ProtobufRowDataConverter.forGroup(group, fileDescriptor);
}
 
Example #16
Source File: DynamicProtoUtil.java    From rejoiner with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a Map containing data from a ByteString that is parsed using the Proto Descriptor.
 *
 * <p>The FileDescriptorSet must contain a message with the name "{operationName}Request". This
 * message will be used to parse the ByteString, and the resulting message will be transformed and
 * returned as a Map.
 */
public static Map<String, Object> decodeVariables(
    String operationName, FileDescriptorSet fileDescriptorSet, ByteString encodedRequest) {
  try {
    // TODO: Support multiple FileDescriptors in FileDescriptorSet
    FileDescriptor fileDescriptor =
        FileDescriptor.buildFrom(fileDescriptorSet.getFileList().get(0), new FileDescriptor[] {});
    Descriptor messageType = fileDescriptor.findMessageTypeByName(operationName + "Request");
    Message message = DynamicMessage.parseFrom(messageType, encodedRequest);
    return ProtoToMap.messageToMap(message);
  } catch (DescriptorValidationException | InvalidProtocolBufferException e) {
    e.printStackTrace();
    throw new RuntimeException(e);
  }
}
 
Example #17
Source File: ProtobufStorageDescriptionHelper.java    From sql-layer with GNU Affero General Public License v3.0 4 votes vote down vote up
static FileDescriptorProto validateAndGenerate(HasStorage object,
                                               ProtobufRowFormat.Type formatType,
                                               FileDescriptorProto fileProto,
                                               AISValidationOutput output) {
    if (!(object instanceof Group)) {
        output.reportFailure(new AISValidationFailure(new StorageDescriptionInvalidException(object, "is not a Group and cannot use Protocol Buffers")));
        return null;
    }
    Group group = (Group)object;
    if (formatType == ProtobufRowFormat.Type.SINGLE_TABLE) {
        if (!group.getRoot().getChildJoins().isEmpty()) {
            output.reportFailure(new AISValidationFailure(new StorageDescriptionInvalidException(object, "has more than one table")));
            return null;
        }
    }
    int currentVersion = sumTableVersions(group.getRoot());
    if (fileProto != null) {
        int storedVersion = fileProto.getOptions()
            .getExtension(CustomOptions.GroupOptions.fdbsql).getVersion();
        if (storedVersion == currentVersion) {
            return fileProto;
        }
    }
    FileDescriptorSet set = null;
    if (fileProto != null) {
        FileDescriptorSet.Builder builder = FileDescriptorSet.newBuilder();
        builder.addFile(fileProto);
        set = builder.build();
    }
    AISToProtobuf ais2p = new AISToProtobuf(formatType, set);
    ais2p.addGroup(group);
    set = ais2p.build();
    fileProto = set.getFile(0); // Only added one group.
    // Make sure it will build before committing to this format.
    try {
        FileDescriptor.buildFrom(fileProto, DEPENDENCIES);
    }
    catch (DescriptorValidationException ex) {
        output.reportFailure(new AISValidationFailure(new ProtobufBuildException(ex)));
    }
    return fileProto;
}
 
Example #18
Source File: DynamicSchema.java    From protobuf-dynamic with Apache License 2.0 2 votes vote down vote up
/**
 * Parses a serialized schema descriptor (from byte array)
 * 
 * @param schemaDescBuf the descriptor byte array
 * @return the schema object
 * @throws DescriptorValidationException
 * @throws IOException
 */
public static DynamicSchema parseFrom(byte[] schemaDescBuf) throws DescriptorValidationException, IOException {
	return new DynamicSchema(FileDescriptorSet.parseFrom(schemaDescBuf));
}