Skip to content

Struct methods cause schema generation to fail #55

@TommoLeedsy

Description

@TommoLeedsy

Issue Description

Gin handlers that are struct methods cause schema generation to fail.

Steps to Reproduce

  1. Create a main.go with this content:
package main

import (
	"database/sql"

	"github.com/gin-gonic/gin"
	"github.com/ls6-events/astra"
	"github.com/ls6-events/astra/inputs"
	"github.com/ls6-events/astra/outputs"
)

type SharedData struct {
	db *sql.DB
}

func (sd SharedData) GetDatabaseConnectionStats(c *gin.Context) {
	c.JSON(200, sd.db.Stats())
}

func main() {
	r := gin.Default()

	db := InitDB()
	defer db.Close()

	sd := SharedData{db: db}

	r.GET("/stats", sd.GetDatabaseConnectionStats)

	gen := astra.New(inputs.WithGinInput(r), outputs.WithOpenAPIOutput("openapi.generated.yaml"))

	config := astra.Config{
		Title:   "Example API",
		Version: "1.0.0",
		Host:    "localhost",
		Port:    8080,
	}

	gen.SetConfig(&config)

	err := gen.Parse()
	if err != nil {
		panic(err)
	}

	r.Run(":8080")
}
  1. Install dependencies: go get .
  2. Run main.go: go run main.go

Expected Behaviour

Bellow is the expected output in openapi.generated.yaml from running main.go.

openapi: 3.0.0
info:
    title: Example API
    description: Generated by astra
    contact: {}
    license:
        name: ""
    version: 1.0.0
servers:
    - url: http://localhost:8080
paths:
    /stats:
        get:
            operationId: getDatabaseConnectionStats
            responses:
                "200":
                    description: ""
                    content:
                        application/json:
                            schema:
                                $ref: '#/components/schemas/sql.DBStats'
components:
    schemas:
        sql.DBStats:
            type: object
            properties:
                Idle:
                    type: integer
                    format: int32
                InUse:
                    type: integer
                    format: int32
                MaxIdleClosed:
                    type: integer
                    format: int64
                MaxIdleTimeClosed:
                    type: integer
                    format: int64
                MaxLifetimeClosed:
                    type: integer
                    format: int64
                MaxOpenConnections:
                    type: integer
                    format: int32
                OpenConnections:
                    type: integer
                    format: int32
                WaitCount:
                    type: integer
                    format: int64
                WaitDuration:
                    $ref: '#/components/schemas/time.Duration'
            description: DBStats contains database statistics.
        time.Duration:
            enum:
                - 1
            type: integer
            format: int32
            description: |-
                A Duration represents the elapsed time between two instants
                as an int64 nanosecond count. The representation limits the
                largest representable duration to approximately 290 years.

Actual Behaviour

No schema is generated and the bellow error is output in the logs:

Rel: can't make <autogenerated> relative to <path to backend>

Additional Information

  • Go Version: 1.21.1
  • Astra Version: 1.19.0

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