Skip to content

same x-go-type in aresponse and schemas results in double entry in imports #70

Description

@davidn

Test case:

foji.yaml:

files:
  api:
    files:
      - "api.yaml"
processes:
  openAPI:
    params:
      Package: "main"
    OpenAPIFile:
      handlers_gen.go: foji/openapi/handler.go.tpl

api.yaml:

openapi: 3.0.1
paths:
  /:
    get:
      operationId: get
      responses:
        200:
          content:
            application/json:
              schema:
                type: object
                x-go-type: encoding/json.RawMessage
components:
  schemas:
    s:
      type: object
      properties:
        value:
          type: object
          x-go-type: encoding/json.RawMessage

Results in the following handlers_gen.go:

// Code generated by foji, template: foji/openapi/handler.go.tpl; DO NOT EDIT.

package main

import (
	"context"
	"encoding/json"
	"net/http"

	"encoding/json"

	"github.com/bir/iken/httputil"
	"github.com/bir/iken/logctx"
)

type Operations interface {
	Get(ctx context.Context) (*json.RawMessage, error)
}

type OpenAPIHandlers struct {
	ops Operations
}

type Mux interface {
	Handle(pattern string, handler http.Handler)
}

func RegisterHTTP(ops Operations, r Mux) *OpenAPIHandlers {
	s := OpenAPIHandlers{
		ops: ops,
	}

	r.Handle("GET /", http.HandlerFunc(s.Get))

	return &s
}

// Get
func (h OpenAPIHandlers) Get(w http.ResponseWriter, r *http.Request) {
	var err error

	logctx.SetOperation(r.Context(), "get")
	logctx.AddStrToContext(r.Context(), "op", "get")

	response, err := h.ops.Get(r.Context())
	if err != nil {
		httputil.ErrorHandler(w, r, err)

		return
	}

	httputil.JSONWrite(w, r, 200, response)
}

Note that "encoding/json" appears twice in the import list. This causes go builds to fail.

Workaround: run a fomatter to tidy up imports (goimports is not sufficient) after foji but before any build or mockery using the output.

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