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

Using #ref to reuse parameters cause "error unmarshaling JSON"

Open pauloalexandre3d opened this issue 3 years ago • 1 comments

Hi!

I've been refactoring my openapi file to reuse schemas, parameters and responses.

However, I can't generate the golang code anymore.

Here are the steps to reproduce:

My openapi.yaml looks like this:

paths:
  /api/config/onion/{onionID}:
    $ref: "./resources/onion.yaml"

components:
  parameters:
    $ref: "./parameters/_index.yaml"
  schemas:
    $ref: "./schemas/_index.yaml"
  responses:
    $ref: "./responses/_index.yaml"

parameter/_index.yaml looks like this:

onionId:
  $ref: "./path/onionId.yaml"

AnotherParameter:
  $ref: "./path/anotherOne.yaml"

The ./path/onionId.yaml file looks like this:

name: "onionID"
in: "path"
description: "Onion ID"
required: true
schema:
  type: string
  format: uuid

And my resource looks like this

get:
  tags: 
    - org
  summary: Returns an onion
  description: Returns the onion
  operationId: getOnion
  parameters:
    - $ref: '../parameters/path/onionID.yaml'
  responses:
    '200':
      description: Onions
      content:
        application/json:
          schema:
            type: array
            items:
              $ref: "../schemas/Onion.yaml"

I have a generate.go like this:

// Package server serves the API.
package server

//go:generate oapi-codegen -package server -generate types,server -o endpoints.go ../../openapi.yaml

My Makefile runs:

go generate ./...

And I'm getting this error:

error loading swagger spec in ../../openapi.yaml
: error unmarshaling JSON: while decoding JSON: failed to unmarshal property "components" (*openapi3.Components): failed to unmarshal property "parameters" (*openapi3.ParametersMap): failed to unmarshal extension properties: json: cannot unmarshal string into Go value of type map[string]json.RawMessage ("./parameters/_index.yaml")pkg/server/generate.go:4: running "oapi-codegen": exit status 1

I'm following this example for reference: How to split a large OpenAPI document into multiple files

Do you have any idea? It seems I can't use $ref under components/parameters/$ref

Kindest regards

pauloalexandre3d avatar Jan 24 '23 02:01 pauloalexandre3d

V2.1.0

Confirm the problem still exist but different error. I tried use code from this section https://github.com/deepmap/oapi-codegen?tab=readme-ov-file#splitting-large-openapi-specs-across-multiple-packages-aka-import-mapping-or-external-references

Error

Error generating HTTP Fiber code: error generating type definitions: error generating Go types for component schemas: error converting Schema User to Go type: error turning reference (./openapi/v1/api.yaml#/components/schemas/User) into a Go type: unrecognized external reference './openapi/v1/api.yaml'; please provide the known import for this reference using option --import-mapping
exit status 1
conf := codegen.Configuration{
	Generate: codegen.GenerateOptions{
		EchoServer:   false,
		FiberServer:  true,
		Client:       false,
		Models:       true,
		EmbeddedSpec: true,
		Strict:       true,
	},
	PackageName: packageName,
}

image image

boshido avatar May 06 '24 15:05 boshido