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

couldn't find a swagger spec #2580

Closed
santosh opened this issue May 26, 2021 · 20 comments
Closed

couldn't find a swagger spec #2580

santosh opened this issue May 26, 2021 · 20 comments

Comments

@santosh
Copy link

santosh commented May 26, 2021

Problem statement

Getting couldn't find a swagger spec when doing swagger generate server -A todo-list -f ./swagger.yml.
I am following https://goswagger.io/tutorial/todo-list.html.

Steps to reproduce

  1. Go to https://goswagger.io/tutorial/todo-list.html and copy the complete spec to a file called swagger.yml.
  2. Run swagger generate server -A todo-list -f ./swagger.yml from the directory of the file.

Environment

swagger version: version: v0.27.0
go version: go1.15.8
OS: Amazon Linux 2

@Planck1858
Copy link

Same problem

@youyuanwu
Copy link
Member

Is couldn't find a swagger spec the entire swagger output in terminal?

@santosh
Copy link
Author

santosh commented Jun 2, 2021

@youyuanwu yes, correct.

@youyuanwu
Copy link
Member

Cannot reproduce

bash:~/temp$ ls
swagger.yml
bash:~/temp$ cat swagger.yml 
swagger: "2.0"
info:
  description: From the todo list tutorial on goswagger.io
  title: A Todo list application
  version: 1.0.0
consumes:
- application/io.goswagger.examples.todo-list.v1+json
produces:
- application/io.goswagger.examples.todo-list.v1+json
schemes:
- http
paths:
  /:
    get:
      tags:
        - todos
      parameters:
        - name: since
          in: query
          type: integer
          format: int64
        - name: limit
          in: query
          type: integer
          format: int32
          default: 20
      responses:
        200:
          description: list the todo operations
          schema:
            type: array
            items:
              $ref: "#/definitions/item"
        default:
          description: generic error response
          schema:
            $ref: "#/definitions/error"
definitions:
  item:
    type: object
    required:
      - description
    properties:
      id:
        type: integer
        format: int64
        readOnly: true
      description:
        type: string
        minLength: 1
      completed:
        type: boolean
  error:
    type: object
    required:
      - message
    properties:
      code:
        type: integer
        format: int64
      message:
        type: string
bash:~/temp$ swagger generate server -f swagger.yml 
2021/06/02 00:13:02 validating spec /home/user1/temp/swagger.yml
2021/06/02 00:13:02 preprocessing spec with option:  minimal flattening
2021/06/02 00:13:02 building a plan for generation
2021/06/02 00:13:02 generation target ./
2021/06/02 00:13:02 target must reside inside a location in the $GOPATH/src or be a module

@santosh
Copy link
Author

santosh commented Jun 2, 2021

Following these steps:

ec2-user at ip-172-31-45-160 in /efs $ cd /efs/repos/swaggerapi/
ec2-user at ip-172-31-45-160 in /efs/repos/swaggerapi $ cat swagger.yml 
consumes:
- application/io.goswagger.examples.todo-list.v1+json
info:
  description: From the todo list tutorial on goswagger.io
  title: A Todo list application
  version: 1.0.0
paths: {}
produces:
- application/io.goswagger.examples.todo-list.v1+json
schemes:
- http
swagger: "2.0"
---
definitions:
  item:
    type: object
    required:
      - description
    properties:
      id:
        type: integer
        format: int64
        readOnly: true
      description:
        type: string
        minLength: 1
      completed:
        type: boolean
  error:
    type: object
    required:
      - message
    properties:
      code:
        type: integer
        format: int64
      message:
        type: string
---
paths:
  /:
    get:
      tags:
        - todos
      parameters:
        - name: since
          in: query
          type: integer
          format: int64
        - name: limit
          in: query
          type: integer
          format: int32
          default: 20
      responses:
        200:
          description: list the todo operations
          schema:
            type: array
            items:
              $ref: "#/definitions/item"
        default:
          description: generic error response
          schema:
            $ref: "#/definitions/error"
ec2-user at ip-172-31-45-160 in /efs/repos/swaggerapi $ swagger generate server -A todo-list -f ./swagger.yml
couldn't find a swagger spec
ec2-user at ip-172-31-45-160 in /efs/repos/swaggerapi $

@youyuanwu
Copy link
Member

Seems like the error is from here:

return "", errors.New("couldn't find a swagger spec")

Not sure how the spec file name is not picked up.

Can you try run swagger generate server without any argument in the dir with the spec file?

@santosh
Copy link
Author

santosh commented Jun 5, 2021

I get the same result.

@casualjim
Copy link
Member

is this in or out of the go path, does the project have a go.mod file if it isn't?

@casualjim
Copy link
Member

$ cd $(mktemp -d)   

/tmp/tmp.4HoE96p3Rl $ swagger generate server -f ./swagger.yaml 

2021/06/05 12:54:48 validating spec /tmp/tmp.4HoE96p3Rl/swagger.yaml
2021/06/05 12:54:49 preprocessing spec with option:  minimal flattening
2021/06/05 12:54:49 building a plan for generation
2021/06/05 12:54:49 generation target ./
2021/06/05 12:54:49 target must reside inside a location in the $GOPATH/src or be a module

/tmp/tmp.4HoE96p3Rl $ swagger generate server -f swagger.yaml

2021/06/05 12:54:55 validating spec /tmp/tmp.4HoE96p3Rl/swagger.yaml
2021/06/05 12:54:56 preprocessing spec with option:  minimal flattening
2021/06/05 12:54:56 building a plan for generation
2021/06/05 12:54:56 generation target ./
2021/06/05 12:54:56 target must reside inside a location in the $GOPATH/src or be a module

/tmp/tmp.4HoE96p3Rl $ go mod init goswagger.io/example/todo-list

go: creating new go.mod: module goswagger.io/example/todo-list

/tmp/tmp.4HoE96p3Rl $ swagger generate server -f swagger.yaml   

2021/06/05 12:55:16 validating spec /tmp/tmp.4HoE96p3Rl/swagger.yaml
2021/06/05 12:55:16 preprocessing spec with option:  minimal flattening
2021/06/05 12:55:16 building a plan for generation
2021/06/05 12:55:16 generation target ./
2021/06/05 12:55:16 planning definitions (found: 2)
2021/06/05 12:55:16 planning operations (found: 1)
2021/06/05 12:55:16 grouping operations into packages (packages: 1)
2021/06/05 12:55:16 operations for package packages "todos" (found: 1)
2021/06/05 12:55:16 planning meta data and facades
2021/06/05 12:55:16 rendering 2 models
2021/06/05 12:55:16 rendering 1 templates for model error
2021/06/05 12:55:16 name field error
2021/06/05 12:55:16 package field models
2021/06/05 12:55:16 creating generated file "error.go" in "models" as definition
2021/06/05 12:55:16 executed template asset:model
2021/06/05 12:55:16 rendering 1 templates for model item
2021/06/05 12:55:16 name field item
2021/06/05 12:55:16 package field models
2021/06/05 12:55:16 creating generated file "item.go" in "models" as definition
2021/06/05 12:55:16 executed template asset:model
2021/06/05 12:55:16 rendering 1 operation groups (tags)
2021/06/05 12:55:16 rendering 1 operations for todos
2021/06/05 12:55:16 rendering 4 templates for operation ATodoListApplication
2021/06/05 12:55:16 name field Get
2021/06/05 12:55:16 package field todos
2021/06/05 12:55:16 creating generated file "get_parameters.go" in "restapi/operations/todos" as parameters
2021/06/05 12:55:16 executed template asset:serverParameter
2021/06/05 12:55:16 name field Get
2021/06/05 12:55:16 package field todos
2021/06/05 12:55:16 creating generated file "get_urlbuilder.go" in "restapi/operations/todos" as urlbuilder
2021/06/05 12:55:16 executed template asset:serverUrlbuilder
2021/06/05 12:55:16 name field Get
2021/06/05 12:55:16 package field todos
2021/06/05 12:55:16 creating generated file "get_responses.go" in "restapi/operations/todos" as responses
2021/06/05 12:55:16 executed template asset:serverResponses
2021/06/05 12:55:16 name field Get
2021/06/05 12:55:16 package field todos
2021/06/05 12:55:16 creating generated file "get.go" in "restapi/operations/todos" as handler
2021/06/05 12:55:16 executed template asset:serverOperation
2021/06/05 12:55:16 rendering 0 templates for operation group ATodoListApplication
2021/06/05 12:55:16 rendering support
2021/06/05 12:55:16 rendering 6 templates for application ATodoListApplication
2021/06/05 12:55:16 name field ATodoListApplication
2021/06/05 12:55:16 package field operations
2021/06/05 12:55:16 creating generated file "main.go" in "cmd/a-todo-list-application-server" as main
2021/06/05 12:55:16 executed template asset:serverMain
2021/06/05 12:55:16 name field ATodoListApplication
2021/06/05 12:55:16 package field operations
2021/06/05 12:55:16 creating generated file "embedded_spec.go" in "restapi" as embedded_spec
2021/06/05 12:55:16 executed template asset:swaggerJsonEmbed
2021/06/05 12:55:16 name field ATodoListApplication
2021/06/05 12:55:16 package field operations
2021/06/05 12:55:16 creating generated file "server.go" in "restapi" as server
2021/06/05 12:55:16 executed template asset:serverServer
2021/06/05 12:55:16 name field ATodoListApplication
2021/06/05 12:55:16 package field operations
2021/06/05 12:55:16 creating generated file "a_todo_list_application_api.go" in "restapi/operations" as builder
2021/06/05 12:55:16 executed template asset:serverBuilder
2021/06/05 12:55:16 name field ATodoListApplication
2021/06/05 12:55:16 package field operations
2021/06/05 12:55:16 creating generated file "doc.go" in "restapi" as doc
2021/06/05 12:55:16 executed template asset:serverDoc
2021/06/05 12:55:16 name field ATodoListApplication
2021/06/05 12:55:16 package field operations
2021/06/05 12:55:16 creating generated file "configure_a_todo_list_application.go" in "restapi" as configure
2021/06/05 12:55:16 executed template asset:serverConfigureapi
2021/06/05 12:55:16 Generation completed!

For this generation to compile you need to have some packages in your GOPATH:

	* github.com/go-openapi/runtime
	* github.com/jessevdk/go-flags

You can get these now with: go get -u -f ./...

@santosh
Copy link
Author

santosh commented Jun 6, 2021

I did setup go modules, but didn't work out:

ec2-user at ip-172-31-45-160 in /efs/repos/swaggerapi $ go mod init gitlab.com/sntshk/swaggerapigo: creating new go.mod: module gitlab.com/sntshk/swaggerapi
ec2-user at ip-172-31-45-160 in /efs/repos/swaggerapi $ ls
go.mod  swagger.yml
ec2-user at ip-172-31-45-160 in /efs/repos/swaggerapi $ swagger generate server
couldn't find a swagger spec
ec2-user at ip-172-31-45-160 in /efs/repos/swaggerapi $ swagger generate server -f ./swagger.yaml 
couldn't find a swagger spec
ec2-user at ip-172-31-45-160 in /efs/repos/swaggerapi $ swagger generate server -f swagger.yaml 
couldn't find a swagger spec

@youyuanwu
Copy link
Member

Is ther filename "swagger.yml" or "swagger.yaml"?

@santosh
Copy link
Author

santosh commented Jun 7, 2021

Sorry that I was using the wrong file name, but even after the correct extension, I don't see a difference:

ec2-user at ip-172-31-45-160 in /efs/repos/swaggerapi $ swagger generate server -f ./swagger.yml 
couldn't find a swagger spec
ec2-user at ip-172-31-45-160 in /efs/repos/swaggerapi $ swagger generate server -f swagger.yml 
couldn't find a swagger spec
ec2-user at ip-172-31-45-160 in /efs/repos/swaggerapi $ swagger generate server 
couldn't find a swagger spec
ec2-user at ip-172-31-45-160 in /efs/repos/swaggerapi $

@youyuanwu
Copy link
Member

I suggest you try generate under home directory, or try on another machine. It is unclear why swagger cannot read the file.

@santosh
Copy link
Author

santosh commented Jun 12, 2021

Unfortunately the same outcome is there in the home directory:

ec2-user at ip-172-31-45-160 in ~ $ ll | grep swag
-rw-rw-r-- 1 ec2-user ec2-user  1291 Jun 12 05:42 swagger.yml
ec2-user at ip-172-31-45-160 in ~ $ swagger generate server 
couldn't find a swagger spec
ec2-user at ip-172-31-45-160 in ~ $ swagger generate server -f swagger.yml 
couldn't find a swagger spec
ec2-user at ip-172-31-45-160 in ~ $ swagger generate server -f ./swagger.yml 
couldn't find a swagger spec

@dinever
Copy link

dinever commented Jun 24, 2021

I have the same error

$ ls -l swagger.json
-rw-r--r--  1 dinever  staff    49K Jun 24 17:09 swagger.json
$ swagger generate markdown
couldn't find a swagger spec

@dinever
Copy link

dinever commented Jun 24, 2021

Hi @santosh,

I feel like this might have something to do with running swagger from the Docker container, as volumes need to be mounted for the files to be read in the container. Are you running swagger from Docker?

You could try using the static binary https://goswagger.io/install.html#static-binary and see if it resolves the issue.

@zhaozhaoyi
Copy link

i think the init path is not right
alias swagger="docker run --rm -it --user $(id -u):$(id -g) -e GOPATH=$HOME/go:/go -v $HOME:$HOME -w $(pwd) quay.io/goswagger/swagger"
swagger version

swagger validate ./swagger/swagger.yml

open ./swagger/swagger.yml: no such file or directory

@casualjim
Copy link
Member

i think the init path is not right
alias swagger="docker run --rm -it --user $(id -u):$(id -g) -e GOPATH=$HOME/go:/go -v $HOME:$HOME -w $(pwd) quay.io/goswagger/swagger"
swagger version

swagger validate ./swagger/swagger.yml

open ./swagger/swagger.yml: no such file or directory

try with:

alias swagger="docker run --rm -it --user $(id -u):$(id -g) -e GOPATH=$HOME/go:/go -v $HOME:$HOME -v $PWD:$PWD -w $PWD quay.io/goswagger/swagger"

@shubinmi
Copy link

shubinmi commented Oct 7, 2021

looks like -f ./spec/swagger.yaml should be at the end of the command.
like //go:generate swagger generate server -A myApp -s restapi -t ./gen -f ./spec/swagger.yaml

@santosh
Copy link
Author

santosh commented Nov 29, 2021

I'm closing this issue as I'm able to run the same command without any issue in an environment different than Amazon Linux 2.

I was able to run this on below environment:

swagger version: v0.28.0
go version: go1.17.3
OS: Ubuntu 20.04 (WSL 2 | Docker 20.10.10)

@santosh santosh closed this as completed Nov 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants