oapi-codegen
oapi-codegen copied to clipboard
`allOf` code generation does not work with types from other packages
I have some standardized error struct which is used for all parts of my API, and put it into a separate package to make it easily reuasble
components:
schemas:
Error:
type: object
properties:
message:
type: string
details:
type: array
items:
$ref: "#/components/schemas/ErrorDetail"
ErrorDetail:
type: object
properties:
message:
type: string
target:
type: string
This all works fine, but in some scenarios I'd like to have an example error message. So I use allOf to merge this struct with additional details:
allOf:
- $ref: "#/components/schemas/Error" # I imported the errors.yaml in this components/schema to make errors.Error available.
- type: object
properties:
message:
example: "File non-existing.txt not found"
This results in the follow code:
type FileNotFound struct {
// Optional list of additional error details.
Details *[]ErrorDetail `json:"details,omitempty"`
Message interface{} `json:"message"`
}
The problem is that ErrorDetail is not defined (it is defined in another package containing the generated code of errors.yaml). Error itself is working as the code generator created this line
type Error = externalRef0.Error
Workaround
Import ErrorDetail in the yaml file as well:
ErrorDetail:
# This is needed for workaround a code generation bug
$ref: "./errors.yaml#/components/schemas/ErrorDetail"
and it generates
type ErrorDetail = externalRef0.ErrorDetail
Unfortunately, linter detect that the ErrorDetail object is not used/referenced, so it issues a warning (in my IDE)
Related to #648 ?