Skip to content

Cannot reference other specs in the same directory #400

@ifross89

Description

@ifross89

I am trying to split my specs into multiple files in order be able to produce different output files within the same package. As oapi-codegen considers any external file reference to be an "externalRef" I must supply an import mapping for this. As the generated files are in the same package this leads to attempting to import the current package which is not allowed in go.

Here is an example that reproduces the issue:

  • a.yaml:
openapi: 3.0.0
info:
  title: a
  version: 0.0.1
paths: {}
components:
  schemas:
    A:
      properties:
        foo:
          type: string
  • b.yaml
openapi: 3.0.0
info:
  title: b
  version: 0.0.1
paths: {}
components:
  schemas:
    B:
      $ref: a.yaml#/components/schemas/A

Without import mapping supplied:

$ oapi-codegen -package scratch -generate types,skip-prune b.yaml > b.gen.go
error generating code: error generating type definitions: error generating Go types for component schemas: error converting Schema B to Go type: error turning reference (a.yaml#/components/schemas/A) into a Go type: unrecognized external reference 'a.yaml'; please provide the known import for this reference using option --import-mapping

With import mapping supplied:

$ oapi-codegen -import-mapping a.yaml:scratch -package scratch -generate types,skip-prune b.yaml > b.gen.go
$ go build
package scratch
        imports scratch: import cycle not allowed

I was able to get this working modifying codegen/utils.go to check whether a remote component referred to a file within the same directory & treating it as a local ref. I was wondering if you would like me to make a PR to fix this.
Thanks

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions