Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when generating HTTPRequest / HTTPResponse handler body #1709

Open
svennjegac opened this issue Nov 24, 2023 · 1 comment
Open

Error when generating HTTPRequest / HTTPResponse handler body #1709

svennjegac opened this issue Nov 24, 2023 · 1 comment

Comments

@svennjegac
Copy link

svennjegac commented Nov 24, 2023

Describe the bug
Current solution supports defining HTTPRequest and HTTPResponse structs in HTTP handler function definitions.
However, if we define additional struct for our HTTPRequest, e.g. Dependency, and HTTPRequest includes that Dependency as one of the fields, swaggo/swag cli will report an error and won't produce valid yaml.

To Reproduce
Steps to reproduce the behavior:

  1. Create file with this content:
package main

import (
	"net/http"
)

func main() {

}

// HelloWorld says hello to the world.
//
//	@Summary		Hello world.
//	@Description	Hello world.
//	@Id				hello-world
//	@Accept			json
//	@Produce		json
//	@Param			request	body	main.HelloWorld.HTTPRequest	required	"HTTP Request Body"
//	@Success		204
//	@Failure		400
//	@Router			/hello-world [POST]
func HelloWorld() http.HandlerFunc {
	type Dependency = struct {
		Name string `json:"name"`
	}

	type HTTPRequest struct {
		SingleDep    Dependency   `json:"single_dep"`
		Dependencies []Dependency `json:"dependencies"`
	}

	return func(writer http.ResponseWriter, request *http.Request) {
	}
}

  1. Run swag init --v3.1 --parseInternal --ot "json,yaml" --output ./spec

  2. Output will be unexpected (missing fields in HTTPRequest definition)

components:
  schemas:
    main.HelloWorld.HTTPRequest:
      type: object
externalDocs:
  description: ""
  url: ""
info:
  description: Hello world.
  title: ""
  version: ""
openapi: 3.1.0
paths:
  /hello-world:
    post:
      description: Hello world.
      operationId: hello-world
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/main.HelloWorld.HTTPRequest'
        description: HTTP Request Body
        required: true
      responses:
        "204":
          description: No Content
        "400":
          description: Bad Request
      summary: Hello world.

Expected behavior
Expected yaml would contain appropriate HTTPRequest:

components:
  schemas:
    main.HelloWorld.HTTPRequest:
      properties:
        dependencies:
          items:
            properties:
              name:
                type: string
            type: object
          type: array
          uniqueItems: false
        single_dep:
          properties:
            name:
              type: string
          type: object
      type: object
externalDocs:
  description: ""
  url: ""
info:
  description: Hello world.
  title: ""
  version: ""
openapi: 3.1.0
paths:
  /hello-world:
    post:
      description: Hello world.
      operationId: hello-world
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/main.HelloWorld.HTTPRequest'
        description: HTTP Request Body
        required: true
      responses:
        "204":
          description: No Content
        "400":
          description: Bad Request
      summary: Hello world.

Your swag version
Swag version: v2.0.0

Your go version
go version go1.21.4 darwin/arm64

Desktop (please complete the following information):

  • OS: MacOS Ventura 13.6.1
  • Browser: Chrome
  • Version: 119.0.6045.159
@svennjegac
Copy link
Author

Here is an example of PR which fixes it: svennjegac#1
(I opened it on my fork, will open it on this original repo if you agree)

cc @Nerzal

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant