Skip to content

[BUG][go] model_oneof.mustache renders the "no match" error block once per oneOf member instead of once, causing duplicated/unreachable code #24661

Description

@Fyusel

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

Since #24349 ("Fix Golang pattern validation with regex fails on commas", merged 2026-07-19,
first released in v7.24.0), the Go client generator's UnmarshalJSON for oneOf models wraps the
"no match" error in an if err != nil { ... } else { ... } block to include the underlying error.

That block is generated inside a {{#oneOf}}...{{/oneOf}} Mustache section
(model_oneof.mustache#L82-L92):

	} else { // no match
        {{#oneOf}}
        if err != nil {
		   return fmt.Errorf("data failed to match schemas in oneOf({{classname}}): %v", err)
        } else {
           return fmt.Errorf("data failed to match schemas in oneOf({{classname}})")
        }
        {{/oneOf}}
        {{^oneOf}}
        return fmt.Errorf("data failed to match schemas in oneOf({{classname}})")
        {{/oneOf}}
	}

The generated code still compiles (Go doesn't reject unreachable code after a return inside an
if/else where both branches return), so this doesn't break builds, but it produces obviously
wrong/duplicated dead code that shows up in every generated model with a multi-member oneOf.

This also reproduces in the project's own committed sample output from the same PR, e.g.
samples/client/others/go/oneof-anyof-required/model_object.go,
where the Object model has 2 oneOf members:

	} else { // no match
        if err != nil {
		   return fmt.Errorf("data failed to match schemas in oneOf(Object): %v", err)
        } else {
           return fmt.Errorf("data failed to match schemas in oneOf(Object)")
        }
        if err != nil {
		   return fmt.Errorf("data failed to match schemas in oneOf(Object): %v", err)
        } else {
           return fmt.Errorf("data failed to match schemas in oneOf(Object)")
        }
	}
openapi-generator version
Steps to reproduce
openapi: 3.0.3
info:
  title: oneOf duplicate error block repro
  version: "1.0"
paths:
  /ping:
    get:
      operationId: ping
      responses:
        "200":
          description: ok
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/AreaId"
components:
  schemas:
    AreaId:
      description: The identifier (ID) of an area.
      oneOf:
        - $ref: "#/components/schemas/StaticAreaID"
        - type: string
    StaticAreaID:
      type: string
      enum: [eu01, eu02]

Generate with:

openapi-generator-cli generate -g go -i spec.yaml -o out --additional-properties=disallowAdditionalPropertiesIfNotPresent=false
Actual output

out/model_area_id.go contains the duplicated if/else block in UnmarshalJSON (as shown above).

Expected output

The if/else block should be rendered exactly once per model, regardless of how many oneOf members
the schema has, e.g.:

	} else { // no match
		if err != nil {
			return fmt.Errorf("data failed to match schemas in oneOf(AreaId): %v", err)
		} else {
			return fmt.Errorf("data failed to match schemas in oneOf(AreaId)")
		}
	}
Related issues/PRs

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions