External reference to relative file not working.
Hey,
Having problems to reference a schema in a separate file.
paths:
"/domains":
get:
description: List all Domains
operationId: ListDomains
tags:
- domains
responses:
202:
description: Test
content:
application/json:
schema:
$ref: "./schemas/Domain.yaml" #Doesn't work.
# $ref: "#/components/schemas/Domain" #Works
components:
schemas:
Domain:
$ref: "./schemas/Domain.yaml"
Any ideas what I could be doing wrong? Ideally I would like to reference them relatively as I would like to put the paths in a separate folder under /resources. I.e
/resources/domains.yaml
get:
description: List all Domains
operationId: ListDomains
tags:
- domains
responses:
202:
description: List Domain items.
content:
application/json:
schema:
$ref: "../schemas/Domain.yaml"
default:
$ref: '../responses/Error.yaml'
I think this is related to my issue trying to build the client for: https://github.com/SpaceTradersAPI/api-docs/blob/main/reference/SpaceTraders.json
References are in the same project.
To reproduce I did the following:
- git clone [email protected]:SpaceTradersAPI/api-docs.git
- cd api-docs/reference
- oapi-codegen -generate types -o space_traders.types.gen.go -package main api-docs/reference/SpaceTraders.json
- oapi-codegen -generate client -o space_traders.client.gen.go -package main api-docs/reference/SpaceTraders.json
After generation, attempting to use the generated code yields many, many errors; like:
- undefined: N200DataTraitsSymbol
- undefined: N200DataType
Hey, you may be needing to use the import-mapping config from https://github.com/deepmap/oapi-codegen#using-oapi-codegen ?
Coming to this a bit later, but still open, so valid. Also, 👋 @jamietanna
I read the README and the examples, and I still don't quite get how import mapping works.
I think (could be very wrong), that "import mapping" = "map in the config that tells oapi-codegen what to do when it finds an import". So if I hit two imports in $ref, e.g. foo.yaml and ../../bar.yaml`, then I need one entry for each of those in the config yaml.
If that is correct (again, not sure I got it), then there still is a number of things I still don't understand.
- what is the meaning of
-as the value? - what is supposed to be in there in other cases, and what are those cases?
- does it now need separate config for each yaml that references others, as opposed to a single "root"
oapi-codegen.yamlconfig? - why does it need the explicit mapping? I am making a (possibly unfair) assumption that, when I look at admin/api.yaml and I see on this line
$ref: '../common/api.yaml#/components/schemas/User', if I can understand that is one up from my current directory, across to common, readapi.yaml, and then parse, that oapi-codegen can do so, too. So why do I need the explicit "import mapping" section? - why do I need to run the code generation multiple times?
Thanks for the explanation.
I guess I can add to it, that it appears also to have to do with how the generated go code is generated, single file vs multiple files? Multiple files in one package vs single package? I may be falling afoul of my own assumption, which is that, by default, it would follow the path in the yaml, and generate go code in the same directory structure (files and packages) as the OAS3 yaml source?