oapi-codegen icon indicating copy to clipboard operation
oapi-codegen copied to clipboard

External reference to relative file not working.

Open ainsleyclark opened this issue 2 years ago • 4 comments

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'

ainsleyclark avatar May 11 '23 08:05 ainsleyclark

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

awildeep avatar May 12 '23 03:05 awildeep

Hey, you may be needing to use the import-mapping config from https://github.com/deepmap/oapi-codegen#using-oapi-codegen ?

jamietanna avatar Jun 06 '23 16:06 jamietanna

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.yaml config?
  • 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, read api.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.

deitch avatar Aug 26 '25 12:08 deitch

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?

deitch avatar Aug 26 '25 12:08 deitch