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
35 changes: 35 additions & 0 deletions pkg/codegen/codegen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,41 @@ func TestGoTypeImport(t *testing.T) {
}
}

func TestGoAllofTypeOverride(t *testing.T) {
packageName := "api"
opts := Configuration{
PackageName: packageName,
Generate: GenerateOptions{
EchoServer: true,
Models: true,
EmbeddedSpec: true,
},
}
spec := "test_specs/x-go-type-pet-allof.yaml"
swagger, err := util.LoadSwagger(spec)
require.NoError(t, err)

// Run our code generation:
code, err := Generate(swagger, opts)
assert.NoError(t, err)
assert.NotEmpty(t, code)

// Check that we have valid (formattable) code:
_, err = format.Source([]byte(code))
assert.NoError(t, err)

for _, expected := range []string{
"type Cat = cat.Cat",
"type Dog = dog.Dog",
"type Pet = pet.Pet",
"github.com/somepetproject/pkg/cat",
"github.com/somepetproject/pkg/dog",
"github.com/somepetproject/pkg/pet",
} {
assert.Contains(t, code, expected)
}
}

func TestRemoteExternalReference(t *testing.T) {
if testing.Short() {
t.Skip("Skipping test that interacts with the network")
Expand Down
9 changes: 9 additions & 0 deletions pkg/codegen/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,15 @@ func GenerateGoSchema(sref *openapi3.SchemaRef, path []string) (Schema, error) {
return Schema{}, fmt.Errorf("error merging schemas: %w", err)
}
mergedSchema.OAPISchema = schema
// x-go-type on the parent is handled by the early return above
// (combined extensions). For x-go-type-skip-optional-pointer, only
// override the merged value when the parent sets it explicitly —
// otherwise we would clobber the value MergeSchemas computed from
// the decorator idiom (an inline allOf member that carries the
// extension; see merge_schemas.go and issue #1957).
if _, ok := extensions[extPropGoTypeSkipOptionalPointer]; ok {
mergedSchema.SkipOptionalPointer = skipOptionalPointer
}
return mergedSchema, nil
Comment thread
mromaszewicz marked this conversation as resolved.
}

Expand Down
56 changes: 56 additions & 0 deletions pkg/codegen/test_specs/x-go-type-pet-allof.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
paths:
/pets:
patch:
requestBody:
content:
application/json:
schema:
oneOf:
- $ref: '#/components/schemas/Cat'
- $ref: '#/components/schemas/Dog'
discriminator:
propertyName: pet_type
responses:
'200':
description: Updated
components:
schemas:
Pet:
x-go-type: pet.Pet
x-go-type-import:
path: github.com/somepetproject/pkg/pet
type: object
required:
- pet_type
properties:
pet_type:
type: string
discriminator:
propertyName: pet_type
Dog: # "Dog" is a value for the pet_type property (the discriminator value)
x-go-type: dog.Dog
x-go-type-import:
path: github.com/somepetproject/pkg/dog
allOf: # Combines the main `Pet` schema with `Dog`-specific properties
- $ref: '#/components/schemas/Pet'
- type: object
# all other properties specific to a `Dog`
properties:
bark:
type: boolean
breed:
type: string
enum: [Dingo, Husky, Retriever, Shepherd]
Cat: # "Cat" is a value for the pet_type property (the discriminator value)
x-go-type: cat.Cat
x-go-type-import:
path: github.com/somepetproject/pkg/cat
allOf: # Combines the main `Pet` schema with `Cat`-specific properties
- $ref: '#/components/schemas/Pet'
- type: object
# all other properties specific to a `Cat`
properties:
hunts:
type: boolean
age:
type: integer
Loading