oapi-codegen
oapi-codegen copied to clipboard
External URL ref and oneOf broken generated code
Version
master (SHA fec990c8f82)
Config
package: openapi
generate:
gorilla-server: true
client: true
models: true
output: pkg/openapi/generated.go
import-mapping:
'https://raw.githubusercontent.com/deepmap/oapi-codegen/master/examples/petstore-expanded/petstore-expanded.yaml': github.com/deepmap/oapi-codegen/examples/petstore-expanded
Spec
openapi: "3.0.0"
info:
version: 1.0.0
title: Example broken generation with oneOf and external remote ref
paths:
/check:
get:
summary: Return broken example
responses:
'200':
description: Ok
content:
application/json:
schema:
items:
$ref: '#/components/schemas/exampleSchema'
components:
schemas:
exampleSchema:
properties:
item:
type: object
oneOf:
- $ref: 'https://raw.githubusercontent.com/deepmap/oapi-codegen/master/examples/petstore-expanded/petstore-expanded.yaml#/components/schemas/NewPet'
required:
- item
Problems
Running command oapi-codegen --config config.yaml spec.yaml failed with error
Error openapi.go:61:51: expected '(', found '.' (and 2 more errors)
If watch output, we'll see broken generated methods As/From/Merge for exampleSchema.item: generator put union type name as is with dot in name externalRef\d+.SchemaName:
// AsexternalRef0.NewPet returns the union data inside the ExampleSchema_Item as a externalRef0.NewPet
func (t ExampleSchema_Item) AsexternalRef0.NewPet() (externalRef0.NewPet, error) {
var body externalRef0.NewPet
err := json.Unmarshal(t.union, &body)
return body, err
}
// FromexternalRef0.NewPet overwrites any union data inside the ExampleSchema_Item as the provided externalRef0.NewPet
func (t *ExampleSchema_Item) FromexternalRef0.NewPet (v externalRef0.NewPet) error {
b, err := json.Marshal(v)
t.union = b
return err
}
// MergeexternalRef0.NewPet performs a merge with any union data inside the ExampleSchema_Item, using the provided externalRef0.NewPet
func (t *ExampleSchema_Item) MergeexternalRef0.NewPet (v externalRef0.NewPet) error {
b, err := json.Marshal(v)
if err != nil {
return err
}
merged, err := runtime.JsonMerge(b, t.union)
t.union = merged
return err
}
Generated code with enabled gorilla-server does not generate import gorilla/mux
This just can be fixed by adding
additional-imports:
- package: github.com/gorilla/mux
But... seems like it must be automatically added by enabling gorilla generator.
I ran into this problem too and I fixed it in my private build. However I ran into another problem of oneOf external ref that itself referencing an external type #729.