[confmap] Struct fields silently ignored implementing Unmarshaler with mapstructure:",squash"

Author: kennytrytek-wfCreated Sep 18, 2026Updated Sep 30, 2026
Labelsbugarea:confmap

Component(s)

confmap

What happened?

Describe the bug

When a struct embeds another struct with mapstructure:",squash" and the embedded type implements confmap.Unmarshaler, the outer struct's own fields are silently ignored during unmarshaling — they retain zero/default values regardless of what the source map contains.

Steps to reproduce

// Outer struct embeds a type that implements confmap.Unmarshaler.
type Config struct {
    confighttp.ServerConfig `mapstructure:",squash"` // implements confmap.Unmarshaler
    Path string `mapstructure:"path"`
}

conf := confmap.NewFromStringMap(map[string]any{
    "endpoint": "0.0.0.0:4327",
    "path":     "/events",
})
cfg := &Config{}
_ = conf.Unmarshal(cfg)

fmt.Println(cfg.Path)             // ""
fmt.Println(cfg.NetAddr.Endpoint) // "0.0.0.0:4327"

What did you expect to see?

cfg.Path == "/events" — the outer struct's fields decoded from the source map alongside the embedded struct's fields.

What did you see instead?

cfg.Path == "" — the outer struct's own fields are silently dropped. unmarshalerEmbeddedStructsHookFunc calls the embedded struct's Unmarshal (which correctly decodes endpoint into ServerConfig), but the subsequent decode of the outer struct's remaining fields does not run correctly, leaving Path at its zero value.

Collector version

v0.160.0, go.opentelemetry.io/collector/[email protected]

Environment information

No response

OpenTelemetry Collector configuration

yaml

Log output

bash

Workaround (in a custom receiver)

go
// Unmarshal implements confmap.Unmarshaler so that Config's own fields are
// decoded correctly alongside the embedded confighttp.ServerConfig, which also
// implements Unmarshaler. See https://github.com/open-telemetry/opentelemetry-collector/issues/15985
func (c *Config) Unmarshal(conf *confmap.Conf) error {
	if err := c.ServerConfig.Unmarshal(conf); err != nil {
		return err
	}
	if v := conf.Get("path"); v != nil {
		if s, ok := v.(string); ok {
			c.Path = s
		}
	}
	if v := conf.Get("read_timeout"); v != nil {
		if s, ok := v.(string); ok {
			c.ReadTimeout = s
		}
	}
	if v := conf.Get("read_header_timeout"); v != nil {
		if s, ok := v.(string); ok {
			c.ReadHeaderTimeout = s
		}
	}
	if v := conf.Get("write_timeout"); v != nil {
		if s, ok := v.(string); ok {
			c.WriteTimeout = s
		}
	}
	return nil
}

Tip

No response

Source: open-telemetry/opentelemetry-collector