OpenAPITools/openapi-generator · error · RuntimeException

Error! anyOf schema is not of the type Schema: {item}

Error message

Error! anyOf schema is not of the type Schema: {item}

What it means

The anyOf counterpart of error [18]: normalizeAnyOf iterates schema.getAnyOf(), skips nulls, requires each element to pass 'item instanceof Schema', replaces it with its normalized form in place, then applies the simplify rules. Any non-Schema element under anyOf aborts with this error printing the item. anyOf often carries inline schemas next to refs, so malformed inline fragments (scalars, booleans, raw maps) are the usual trigger.

Source

Thrown at modules/openapi-generator/src/main/java/org/openapitools/codegen/OpenAPINormalizer.java:1369

    }

    protected Schema normalizeAnyOf(Schema schema, Set<Schema> visitedSchemas) {
        //transform anyOf into enums if needed
        schema = processSimplifyAnyOfEnum(schema);
        if (schema.getAnyOf() == null) {
            return schema;
        }

        for (int i = 0; i < schema.getAnyOf().size(); i++) {
            // normalize anyOf sub schemas one by one
            Object item = schema.getAnyOf().get(i);

            if (item == null) {
                continue;
            }

            if (!(item instanceof Schema)) {
                throw new RuntimeException("Error! anyOf schema is not of the type Schema: " + item);
            }

            // update sub-schema with the updated schema
            schema.getAnyOf().set(i, normalizeSchema((Schema) item, visitedSchemas));
        }

        // process rules here
        schema = processSimplifyAnyOf(schema);

        // last rule to process as the schema may become String schema (not "anyOf") after the completion
        return normalizeSchema(processSimplifyAnyOfStringAndEnumString(schema), visitedSchemas);
    }

    protected Schema normalizeComplexComposedSchema(Schema schema, Set<Schema> visitedSchemas) {
        // loop through properties, if any
        if (ModelUtils.hasProperties(schema)) {
            normalizeProperties(schema, visitedSchemas);
        }

View on GitHub (pinned to fcec517be3)

Solutions

  1. Locate the printed item inside the spec's anyOf list and make it a proper schema object or $ref.
  2. Delete boolean/scalar members from anyOf (invalid in OpenAPI 3.x) and any stray empty placeholders.
  3. Verify inline anyOf fragments each have 'type:' or '$ref:' so they deserialize to Schema instances.
  4. For code-built models, only add Schema instances to anyOf.
  5. Run strict spec validation in CI before generation to catch these member-type errors early.

Example fix

# before
Value:
  anyOf:
    - type: string
    - "number"      # scalar string, not a schema
# after
Value:
  anyOf:
    - type: string
    - type: number
Defensive patterns

Strategy: validation

Validate before calling

// Assert anyOf members are real schema objects
for (Schema s : api.getComponents().getSchemas().values()) {
    if (s.getAnyOf() != null) {
        for (Object m : s.getAnyOf()) {
            if (m == null) continue;
            if (!(m instanceof Schema)) {
                throw new IllegalStateException("Invalid anyOf member: " + m);
            }
        }
    }
}

Type guard

private static boolean anyOfMembersAreSchemas(Schema s) {
    return s.getAnyOf() == null || s.getAnyOf().stream()
            .allMatch(m -> m == null || m instanceof Schema);
}

Try / catch

try {
    generator.opts(input).generate();
} catch (RuntimeException e) {
    if (e.getMessage() != null && e.getMessage().contains("anyOf schema is not of the type Schema")) {
        // fix the printed anyOf member in the spec (no scalars/booleans/placeholders)
        throw new GenerationFailure("Invalid anyOf member", e);
    }
    throw e;
}

Prevention

When it happens

Trigger: anyOf lists containing a scalar or boolean member (e.g. '- true' or '- "string"' from a bad merge/conversion); inline fragments that fail deserialization so a raw LinkedHashMap stays in the list; programmatic spec construction appending non-Schema objects; copy-paste YAML leaving a bare value under anyOf.

Common situations: Nullable/anyOf patterns maintained by hand; JSON Schema imports; template-generated YAML with an empty variable rendering a scalar placeholder.

Related errors


AI-assisted analysis of OpenAPITools/openapi-generator@fcec517be3 (2026-08-22). Data as JSON: /api/errors/5831df98cd6e9cd1. Report an issue: GitHub.