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

Cannot generate when specification is using URL references in the components section

Open prometherion opened this issue 4 years ago • 2 comments

oapi-codegen is not able to generate Go types from references using URL refs in the components section.

Example specification:

paths:
  "/example":
    post:
      summary: Do something
      requestBody:
        $ref: "#/components/redacted"
      responses:
        "201":
          $ref: "#/components/responses/201CreatedResource"
        "400":
          $ref: "https://raw.githubusercontent.com/redacted/redacted/main/openapi/index.yaml#/components/responses/400BadRequest"
        "409":
          $ref: "#/components/responses/409Conflict"
        "default":
          $ref: "https://raw.githubusercontent.com/redacted/redacted/main/openapi/index.yaml#/components/responses/5xxErrorResponse"

components:
  schemas:
    409Conflict:
      description: >
        User is trying to create resource that already exists on the server
      content:
        application/json:
          schema:
            $ref: "https://raw.githubusercontent.com/redacted/redacted/main/openapi/index.yaml#/components/schemas/Error"

The error returned:

error generating code: error generating type definitions: error generating Go types for component responses: error generating Go type for schema in response 409Conflict: error turning reference (https://raw.githubusercontent.com/redacted/redacted/main/openapi/index.yaml#/components/schemas/Error) into a Go type: unrecognized external reference 'https://raw.githubusercontent.com/redacted/redacted/main/openapi/index.yaml'; please provide the known import for this reference using option --import-mapping

prometherion avatar Jan 17 '22 16:01 prometherion

A solution for this is obviously to bundle the specification using the command bundle offered by swagger-cli that supports OAPIv3, although the name's suggesting not.

prometherion avatar Jan 17 '22 19:01 prometherion

Thanks for the report on this! We've had a private report of this too, and agreed, it looks like using a URL for an import doesn't seem to work.

Example diff to trigger same issue
diff --git internal/test/externalref/spec.yaml internal/test/externalref/spec.yaml
index ba0ae65..fbad90d 100644
--- internal/test/externalref/spec.yaml
+++ internal/test/externalref/spec.yaml
@@ -1,6 +1,11 @@
 openapi: "3.0.0"
 info: { }
-paths: { }
+paths:
+  /ensure-everything-is-referenced:
+    get:
+      responses:
+        200:
+          $ref: https://raw.githubusercontent.com/deepmap/oapi-codegen/master/internal/test/components/components.yaml#/components/responses/ResponseObject
 components:
   schemas:
     Container:

A solution for this is to use import-mapping as such:

import-mapping:
  ./packageA/spec.yaml: github.com/deepmap/oapi-codegen/internal/test/externalref/packageA
  ./packageB/spec.yaml: github.com/deepmap/oapi-codegen/internal/test/externalref/packageB
  https://raw.githubusercontent.com/deepmap/oapi-codegen/master/internal/test/components/components.yaml: ./extref/

This can then use a ./extref/config.yaml:

package: extref
generate:
  models: true
  embedded-spec: true
output-options:
  skip-prune: true
output: externalref.gen.go

Where the source is generated from that remote URL (whether you vendor that file, or download it at build-time).

jamietanna avatar Nov 28 '22 15:11 jamietanna

Closing since fixed!

prometherion avatar Feb 07 '24 19:02 prometherion