Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
23 changes: 23 additions & 0 deletions pkg/codegen/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,29 @@ func GenerateGoSchema(sref *openapi3.SchemaRef, path []string) (Schema, error) {
return Schema{}, fmt.Errorf("error merging schemas: %w", err)
}
mergedSchema.OAPISchema = schema

// Check x-go-type, which will completely override the definition of this
// schema with the provided type.
if extension, ok := schema.Extensions[extPropGoType]; ok {
typeName, err := extTypeName(extension)
if err != nil {
return outSchema, fmt.Errorf("invalid value for %q: %w", extPropGoType, err)
}
mergedSchema.GoType = typeName
mergedSchema.DefineViaAlias = true

return mergedSchema, nil
}
Comment thread
mromaszewicz marked this conversation as resolved.
Outdated

// Check x-go-type-skip-optional-pointer, which will override if the type
// should be a pointer or not when the field is optional.
if extension, ok := schema.Extensions[extPropGoTypeSkipOptionalPointer]; ok {
skipOptionalPointer, err := extParsePropGoTypeSkipOptionalPointer(extension)
if err != nil {
return outSchema, fmt.Errorf("invalid value for %q: %w", extPropGoTypeSkipOptionalPointer, err)
}
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