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

Support for aliases

Open mr-tron opened this issue 5 years ago • 4 comments

I have swagger file

openapi: 3.0.1
info:
  title: test
  description: API description in Markdown.
  version: 0.1.0
servers: []
paths:
  /test:
    get:
      summary: test
      responses:
        200:
          description: OK
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/B'
  /test2:
    $ref: '#/paths/~1test'


components:
  schemas:
    B:
      type: object
      properties:
        A:
          type: string

command oapi-codegen --generate types,server -package api example.yaml generates code

// Package api provides primitives to interact the openapi HTTP API.
//
// Code generated by github.com/deepmap/oapi-codegen DO NOT EDIT.
package api

import (
	"github.com/labstack/echo/v4"
)

// B defines model for B.
type B struct {
	A *string `json:"A,omitempty"`
}

// ServerInterface represents all server handlers.
type ServerInterface interface {
	// test
	// (GET /test)
	GetTest(ctx echo.Context) error
	// test
	// (GET /test2)
	GetTest(ctx echo.Context) error
}

// ServerInterfaceWrapper converts echo contexts to parameters.
type ServerInterfaceWrapper struct {
	Handler ServerInterface
}

// GetTest converts echo context to params.
func (w *ServerInterfaceWrapper) GetTest(ctx echo.Context) error {
	var err error

	// Invoke the callback with all the unmarshalled arguments
	err = w.Handler.GetTest(ctx)
	return err
}

// GetTest converts echo context to params.
func (w *ServerInterfaceWrapper) GetTest(ctx echo.Context) error {
	var err error

	// Invoke the callback with all the unmarshalled arguments
	err = w.Handler.GetTest(ctx)
	return err
}

// This is a simple interface which specifies echo.Route addition functions which
// are present on both echo.Echo and echo.Group, since we want to allow using
// either of them for path registration
type EchoRouter interface {
	CONNECT(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
	DELETE(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
	GET(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
	HEAD(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
	OPTIONS(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
	PATCH(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
	POST(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
	PUT(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
	TRACE(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
}

// RegisterHandlers adds each server route to the EchoRouter.
func RegisterHandlers(router EchoRouter, si ServerInterface) {

	wrapper := ServerInterfaceWrapper{
		Handler: si,
	}

	router.GET("/test", wrapper.GetTest)
	router.GET("/test2", wrapper.GetTest)

}

but should

// Package api provides primitives to interact the openapi HTTP API.
//
// Code generated by github.com/deepmap/oapi-codegen DO NOT EDIT.
package api

import (
	"github.com/labstack/echo/v4"
)

// B defines model for B.
type B struct {
	A *string `json:"A,omitempty"`
}

// ServerInterface represents all server handlers.
type ServerInterface interface {
	// test
	// (GET /test)
	GetTest(ctx echo.Context) error
}

// ServerInterfaceWrapper converts echo contexts to parameters.
type ServerInterfaceWrapper struct {
	Handler ServerInterface
}

// GetTest converts echo context to params.
func (w *ServerInterfaceWrapper) GetTest(ctx echo.Context) error {
	var err error

	// Invoke the callback with all the unmarshalled arguments
	err = w.Handler.GetTest(ctx)
	return err
}


// This is a simple interface which specifies echo.Route addition functions which
// are present on both echo.Echo and echo.Group, since we want to allow using
// either of them for path registration
type EchoRouter interface {
	CONNECT(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
	DELETE(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
	GET(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
	HEAD(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
	OPTIONS(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
	PATCH(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
	POST(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
	PUT(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
	TRACE(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
}

// RegisterHandlers adds each server route to the EchoRouter.
func RegisterHandlers(router EchoRouter, si ServerInterface) {

	wrapper := ServerInterfaceWrapper{
		Handler: si,
	}

	router.GET("/test", wrapper.GetTest)
	router.GET("/test2", wrapper.GetTest)

}

mr-tron avatar Aug 25 '20 14:08 mr-tron

Aliases can be usefull for example for fixing missprints in url without breaking working urls.

mr-tron avatar Aug 25 '20 14:08 mr-tron

Interesting. I wasn't aware this kind of aliasing existed.

It shouldn't be too difficult to add.

deepmap-marcinr avatar Sep 08 '20 17:09 deepmap-marcinr

Apologies for the delay on this one! I think we'll make this an opt-in feature - to avoid breaking existing code - that'll deduplicate handlers if there are $refs for the same path :+1:

jamietanna avatar Mar 30 '24 14:03 jamietanna

Going to move this to the v2.5.0, as it'll require a bit more tweaking based on what's already present in https://github.com/oapi-codegen/oapi-codegen/pull/606

jamietanna avatar Sep 20 '24 10:09 jamietanna