protoc-gen-validate icon indicating copy to clipboard operation
protoc-gen-validate copied to clipboard

Generated field missing or undefined

Open jmickey opened this issue 3 years ago • 9 comments

I have the following protobuf definition:

syntax = "proto3";

package reignite.types;

import "validate/validate.proto";

// Kernel represents the configuration for a kernel.
message Kernel {
  // Image is the container image to use.
  string image = 1;
  // Cmdline is the kernel command line args.
  string cmdline = 2;
  // Filename is used to specify the name of the kernel file
  // in the Image.
  optional string filename = 3;
  // AddNetworkConfig if set to true indicates that the network-config kernel argument should be generated.
	 bool add_network_config = 4;
}

I haven't actually added any validation rules yet, but the generated <name>.pb.validate.go file:

// Validate checks the field values on Kernel with the rules defined in the
// proto definition for this message. If any rules are violated, an error is returned.
func (m *Kernel) Validate() error {
	if m == nil {
		return nil
	}

	// no validation rules for Image

	// no validation rules for Cmdline

	// no validation rules for AddNetworkConfig

	switch m.XFilename.(type) {

	case *Kernel_Filename:
		// no validation rules for Filename

	}

	return nil
}

Appears to generate a validation on a field that doesn't exist:

m.XFilename undefined (type *Kernel has no field or method XFilename) (MissingFieldOrMethod)

Is it something to do with the fact it is an optional field? Is there a way around this?

jmickey avatar Oct 13 '21 12:10 jmickey

I also encountered this problem. Have you solved this problem?

gopkg-dev avatar Oct 27 '21 10:10 gopkg-dev

Has anyone solved this problem?

BeatsKitano avatar Nov 13 '21 10:11 BeatsKitano

Looking closer at this, it is almost certainly due to the optional modifier (which is modeled under the hood as a oneof prefixed with X). This is tracked over in: #431

rodaine avatar Nov 16 '21 18:11 rodaine

@rodaine Hi, I notice the pr has been merged into 0.6.3. but this issue(generating X* for optional field) still exits. Please help to take a look.

molinjun avatar Jan 14 '22 09:01 molinjun

I am currently using this pr

gopkg-dev avatar Jan 20 '22 06:01 gopkg-dev

@gopkg-dev could like to share how to use your pr? Is there any plan to merge it in master?

molinjun avatar Jan 24 '22 11:01 molinjun

Any status on this? We're still getting this error.

jrkt avatar Mar 23 '22 14:03 jrkt

Has there been anymore movement on this? The issue(generating X* for optional field) is still preventing us from using this.

jrkt avatar May 24 '22 17:05 jrkt

@jrkt I just updated to v0.6.7 and the issue seems to be fixed at least for the Go implementation. You might want to check it out.

0rax avatar Jun 02 '22 14:06 0rax

Issue confirmed not present in v0.6.8

elliotmjackson avatar Sep 19 '22 18:09 elliotmjackson