Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# issue-2470: an external file defines a discriminated oneOf union that is later
# pulled into an allOf from another package via import-mapping.
openapi: "3.0.4"
info:
title: Diff
version: "0.0.1"
paths: {}
components:
schemas:
GitDiffFile:
type: object
properties:
diff_type:
type: string
patch:
type: string
required: [diff_type]
AddedImageDiffFile:
type: object
properties:
diff_type:
type: string
image_url:
type: string
required: [diff_type]
DiffFile:
oneOf:
- $ref: "#/components/schemas/GitDiffFile"
- $ref: "#/components/schemas/AddedImageDiffFile"
discriminator:
propertyName: diff_type
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# yaml-language-server: $schema=../../../../../configuration-schema.json
package: api
generate:
models: true
import-mapping:
./common/spec.yaml: github.com/oapi-codegen/oapi-codegen/v2/internal/test/references/multipackage/discriminated_union_allof/gen/common
output: gen/api/api.gen.go
output-options:
skip-prune: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# yaml-language-server: $schema=../../../../../configuration-schema.json
package: common
generate:
models: true
output: gen/common/common.gen.go
output-options:
skip-prune: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package discriminatedunionallof

//go:generate go run github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen --config=config.common.yaml common/spec.yaml
//go:generate go run github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen --config=config.api.yaml spec.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package discriminatedunionallof

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/oapi-codegen/oapi-codegen/v2/internal/test/references/multipackage/discriminated_union_allof/gen/api"
"github.com/oapi-codegen/oapi-codegen/v2/internal/test/references/multipackage/discriminated_union_allof/gen/common"
)

// TestValueByDiscriminatorExternalRef verifies that PRFile.ValueByDiscriminator()
// dispatches to the correct As* helper when the discriminated union is pulled in
// from another package via an external $ref inside an allOf.
//
// Before the fix, ValueByDiscriminator() interpolated the raw discriminator
// mapping value (externalRef0.GitDiffFile) into the method name, generating
// t.AsexternalRef0.GitDiffFile() — which does not compile. This package simply
// existing (its generated code compiling) is the primary regression guard; the
// round-trip below additionally exercises the dispatch at runtime.
//
// See https://github.com/oapi-codegen/oapi-codegen/issues/2470
func TestValueByDiscriminatorExternalRef(t *testing.T) {
patch := "@@ -1 +1 @@"
var f api.PRFile
require.NoError(t, f.FromExternalRef0GitDiffFile(common.GitDiffFile{
DiffType: "GitDiffFile",
Patch: &patch,
}))

v, err := f.ValueByDiscriminator()
require.NoError(t, err)

got, ok := v.(common.GitDiffFile)
require.True(t, ok, "expected common.GitDiffFile, got %T", v)
require.NotNil(t, got.Patch)
assert.Equal(t, patch, *got.Patch)
}
Loading