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

No properties generated in json file for swagger:response #1161

Closed
mugdhachauhan opened this issue Sep 7, 2017 · 11 comments
Closed

No properties generated in json file for swagger:response #1161

mugdhachauhan opened this issue Sep 7, 2017 · 11 comments

Comments

@mugdhachauhan
Copy link

mugdhachauhan commented Sep 7, 2017

Problem statement

swagger:response annotation is not generating propertiesin json file.
I am using same example as shown in https://github.com/go-swagger/go-swagger/blob/master/docs/generate/spec/response.md

I am not sure what is missing?
similar issue is faced when using swagger:parmeters example

Steps to reproduce

here is code in test.go file

package main
// A ValidationError is an error that is used when the required input fails validation.
// swagger:response validationError
type ValidationError struct {
	// The error message
	// in: body
	Body struct {
		     // The validation message
		     //
		     // Required: true
		     message string
		     // An optional field name to which this validation applies
		     fieldName string
	     }
}

execute swagger generate spec -o ./swagger.json command

swagger.json generated is

{
  "swagger": "2.0",
  "paths": {},
  "responses": {
    "validationError": {
      "description": "A ValidationError is an error that is used when the required input fails validation.",
      "schema": {
        "type": "object"
      }
    }
  }
}

Environment

swagger version: 2.0
go version: 1.8.3
OS: mac

@casualjim
Copy link
Member

The properties in the body struct need to be exported otherwise it can't generate json for it or read it in from json

@mugdhachauhan
Copy link
Author

Sorry but i am not clear what do you mean by properties in the body struct need to be exported. can you give a example?

@casualjim
Copy link
Member

Their names need to be capitalized:

package main
// A ValidationError is an error that is used when the required input fails validation.
// swagger:response validationError
type ValidationError struct {
	// The error message
	// in: body
	Body struct {
		     // The validation message
		     //
		     // Required: true
		     Message string
		     // An optional field name to which this validation applies
		     FieldName string
	     }
}

@mugdhachauhan
Copy link
Author

But with exporting properties(capitalizing) and running swagger generate spec -o ./swagger.json command i see error unable to determine package

@casualjim
Copy link
Member

you can put the struct in a package that isn't main and it should start to work

@casualjim casualjim reopened this Sep 7, 2017
@mugdhachauhan
Copy link
Author

So here is my test.go

package main

import (
	"testing/validation"
	"fmt"
)
// swagger:route GET /v1/list validationError
//
// Get list of something
//
//     Responses:
//       500: validationError

func main(){
	fmt.Println(validation.ValidationError{})
}

And validation.go

// swagger:response validationError

type ValidationError struct {
	// The error message
	// in: body
	Body struct {
		     // The validation message
		     //
		     // Required: true
		     Message string
		     // An optional field name to which this validation applies
		     FieldName string
	     }
}

and still json generated is

{
  "swagger": "2.0",
  "paths": {
    "/v1/list": {
      "get": {
        "description": "Get list of something",
        "operationId": "validationError",
        "responses": {
          "500": {
            "$ref": "#/responses/validationError"
          }
        }
      }
    }
  }
}

Sorry but i just want one basic example to get working so that i work with other files

@mugdhachauhan
Copy link
Author

Can i get some help with this? properties are still missing in swagger.json file

@mugdhachauhan
Copy link
Author

@casualjim I was able to generate correct json file. Thanks for your help :)

@ianaz
Copy link

ianaz commented Jan 24, 2019

How did you solve this? My swagger:model struct is already in a package that's not main

@ritheshgm
Copy link

+1

@mfolkeseth
Copy link

I had the same problem when using go via homebrew.

What solved it for me was to install go from the go webside https://go.dev/doc/install and unset GOROOT and the path resolved itself

GOROOT=/usr/local/go/bin go env -u GOROOT

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

5 participants