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
- Locate the printed item inside the spec's anyOf list and make it a proper schema object or $ref.
- Delete boolean/scalar members from anyOf (invalid in OpenAPI 3.x) and any stray empty placeholders.
- Verify inline anyOf fragments each have 'type:' or '$ref:' so they deserialize to Schema instances.
- For code-built models, only add Schema instances to anyOf.
- 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
- Give inline anyOf fragments an explicit type: or $ref: so they deserialize as Schema.
- Lint templated specs for empty placeholders rendering as scalars under anyOf.
- Validate the assembled document, not source fragments, before every generation run.
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
- Unknown schema type found in normalizer: {schema}
- Error! allOf schema is not of the type Schema: {item}
- Error! oneOf schema is not of the type Schema: {item}
- Could not process model '{name}'.Please make sure that your
- Could not process operation: Tag: {tag} Operation: {oper
AI-assisted analysis of OpenAPITools/openapi-generator@fcec517be3 (2026-08-22).
Data as JSON: /api/errors/5831df98cd6e9cd1.
Report an issue: GitHub.