alibaba/nacos · error · IllegalArgumentException

typeName must not be empty

Error message

typeName must not be empty

What it means

Third guard in NacosJsonSubtype constructor: the wire typeName must be non-null and non-empty. The typeName is the discriminator written into JSON, so an empty one would make polymorphic deserialization ambiguous.

Source

Thrown at api/src/main/java/com/alibaba/nacos/api/utils/json/NacosJsonSubtype.java:49

    
    private final String typeName;
    
    /**
     * Create a new subtype registration.
     *
     * @param baseType base type
     * @param subtype subtype class
     * @param typeName wire type name
     */
    public NacosJsonSubtype(Class<?> baseType, Class<?> subtype, String typeName) {
        if (baseType == null) {
            throw new IllegalArgumentException("baseType must not be null");
        }
        if (subtype == null) {
            throw new IllegalArgumentException("subtype must not be null");
        }
        if (typeName == null || typeName.length() == 0) {
            throw new IllegalArgumentException("typeName must not be empty");
        }
        this.baseType = baseType;
        this.subtype = subtype;
        this.typeName = typeName;
    }
    
    /**
     * Return base type.
     *
     * @return base type
     */
    public Class<?> getBaseType() {
        return baseType;
    }
    
    /**
     * Return subtype.
     *

View on GitHub (pinned to 9b989acdf1)

Solutions

  1. Provide a non-empty discriminator string unique within the base type.
  2. Derive typeName from a stable constant and fail fast if blank at config load time.

Example fix

// before
new NacosJsonSubtype(Base.class, Sub.class, config.get("type"));  // missing key -> null -> throws 553

// after
String t = config.get("type");
if (t == null || t.isEmpty()) throw new IllegalStateException("missing type discriminator");
new NacosJsonSubtype(Base.class, Sub.class, t);
Defensive patterns

Strategy: validation

Validate before calling

if (typeName == null || typeName.isEmpty()) {
    throw new IllegalStateException("type discriminator must be configured");
}

Prevention

When it happens

Trigger: Registering a subtype with a null or empty discriminator string ("").

Common situations: Config key for the discriminator missing; auto-generated typeName returned empty from a lookup; refactoring that dropped the typeName constant.

Related errors


AI-assisted analysis of alibaba/nacos@9b989acdf1 (2026-08-14). Data as JSON: /api/errors/d9c4d42c67c49420. Report an issue: GitHub.