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

unsupported type "invalid type" error when using Linux binary #2379

Open
ahobbs-cb opened this issue Aug 12, 2020 · 19 comments
Open

unsupported type "invalid type" error when using Linux binary #2379

ahobbs-cb opened this issue Aug 12, 2020 · 19 comments
Labels
generate spec Related to spec generation from code install & setup scanner

Comments

@ahobbs-cb
Copy link

ahobbs-cb commented Aug 12, 2020

Problem statement

I'm getting a unsupported type "invalid type" when generating a spec using the v0.25.0 linux binary. I don't get this error when using the darwin binary. It's also fixed if I remove any references to a schema outside of the package.

Swagger specification

// SyncStatusResult represents the status of a request
//
// swagger:model SyncStatusResult
type SyncStatusResult struct {
	// Enum: queued,success,failure
	SyncStatus ss.SyncStatus `json:"sync_status"`
	Status string `json:"status"`
}

Steps to reproduce

  • download v0.25.0 linux binary and darwin binary
  • run swagger.sh generate spec -m -o swagger.json with above code and linux binary and get unsupported type "invalid type" error
  • remove reference to ss package in above code and don't get errors
  • run with darwin binary with above code and get no errors

Environment

swagger version: v0.25.0
go version: 1.14.4
OS: linux

@casualjim
Copy link
Member

To investigate this it would be great to have a more complete reproduction case.
Have you tried this in a docker container/clean env?

@ahobbs-cb
Copy link
Author

I apologize for leaving out some details. I'm running this in a ubuntu:20.04 docker container and the go version is 1.14.4.

@fredbi fredbi added the generate spec Related to spec generation from code label Aug 16, 2020
@fredbi fredbi added the needs testing Needs more testing for issue confirmation/qualification label Nov 18, 2020
@ShriprasadM
Copy link

ShriprasadM commented Dec 7, 2020

Any updates here? I am also facing the same issue with the alpine image.

/prj # cat /etc/*-release
3.12.0
NAME="Alpine Linux"
ID=alpine
VERSION_ID=3.12.0
PRETTY_NAME="Alpine Linux v3.12"
HOME_URL="https://alpinelinux.org/"
BUG_REPORT_URL="https://bugs.alpinelinux.org/"
/prj # uname
Linux
/prj # swagger --log-output=/tmp/swagger.log generate spec -o ./app-resources/swagger.json -w .
unsupported type "invalid type"
/dm # swagger version
version: v0.25.0
commit: f032690
/prj #

my go version is 1.12.5

@fredbi
Copy link
Contributor

fredbi commented Dec 7, 2020

@ShriprasadM could you please include some valid go source to be able to reproduce the case?

@ShriprasadM
Copy link

ShriprasadM commented Dec 8, 2020

Somehow, for this sample, I was able to reproduce it on macOS. Please correct me, if I am missing anything
Actually, I am facing an issue with the Linux library. And Looking for that fix

  1. Download sample.zip

  2. cd ~/Downloads/sample

  3. My Swagger command version (MacOs)
    version: v0.25.0
    commit: f032690

  4. swagger generate spec . (Run this command)
    unsupported type "invalid type"

@casualjim
Copy link
Member

how does that project compile? The project in sample isn't valid go the imports can't work

@ShriprasadM
Copy link

ShriprasadM commented Dec 8, 2020

I have not focused on project compilation it may not work for sample.zip. I was preferring to look for catching the swagger command error

@casualjim
Copy link
Member

does your actual application compile?

@ShriprasadM
Copy link

Yes. it compiles

@casualjim
Copy link
Member

if you run with the env var DEBUG=1, does it tell you which type is wrong? Can you share the type?

it would be very helpful to see both the content the file that has ss.SyncStatus and the import definition used in the file that has:

// SyncStatusResult represents the status of a request
//
// swagger:model SyncStatusResult
type SyncStatusResult struct {
	// Enum: queued,success,failure
	SyncStatus ss.SyncStatus `json:"sync_status"`
	Status string `json:"status"`
}

as well as how their paths are related on the filesystem? (one is a module or a sub/sibling/parent/unrelated)

@ShriprasadM
Copy link

This codebase is shared by @ahobbs-cb . Let me try to share my working version of code

@ShriprasadM
Copy link

ShriprasadM commented Dec 8, 2020

With debug=1 and and swagger generate spec . -> I can see following

2020/12/08 13:40:15 building parameters for: details
2020/12/08 13:40:15 build from type VehicleDetails: *types.Named
2020/12/08 13:40:15 field Vehicle: invalid type(*types.Basic) [""] ==> in: body
2020/12/08 13:40:15 build from field Vehicle: *types.Basic
unsupported type "invalid type"

I have updated #2379 (comment) sample.zip. if you run swagger command it will give you "invalid type" output. The project is not compiled because a package is in GOPATH / GOROOT

@ShriprasadM
Copy link

ShriprasadM commented Dec 8, 2020

  1. sample.zip
  2. Please use this zip file
  3. Extract it at /tmp
  4. Run following commands on Linux as well as on MacOS with same sample project above

export GOPATH=`go env | grep GOPATH | awk -F "=" '{print $2}'|xargs`:/tmp/sample
echo $GOPATH
cd /tmp/sample/
go run b.go
export DEBUG=1
swagger generate spec .

I can see, if the package is no under the src directory above issue is occurring. See the following screenshot. Execute same commands on Linux and macOS respectively

Still : In My actual project, it works on MacOS and fails on Linux

image

@fredbi
Copy link
Contributor

fredbi commented Dec 8, 2020

I am using linux (go1.15.3).

From your screenshot above, the generate spec fails on Mac AND linux with the same error.

My hunch on how this works is that to make generate spec work, the code needs to build correctly.

On my environment, your script fails at go run b.go when go modules are enabled (which is the default).

This works:

GO111MODULE=off go run b.go
{{1234}}

and so does the spec generation:

GO111MODULE=off swagger generate spec ./...
{
  "swagger": "2.0",
  "paths": {
    "/postit": {
      "post": {
        "tags": [
          "tag1"
        ],
        "operationId": "details",
        "parameters": [
          {
            "name": "Vehicle",
            "in": "body",
            "schema": {
              "$ref": "#/definitions/Vehicle"
            }
          }
        ]
      }
    }
  },
  "definitions": {
    "Vehicle": {
      "type": "object",
      "properties": {
        "vehicleno": {
          "type": "string",
          "x-go-name": "No"
        }
      },
      "x-go-package": "a"
    }
  }
}

This fails:

 GO111MODULE=on go run b.go
b.go:4:2: package a is not in GOROOT (/usr/local/go/src/a)

This works:

b.go:
import (
	"fmt"
	"sample/src/a"
)
go mod init sample
 GO111MODULE=on go run b.go
{{1234}}

@fredbi fredbi added install & setup and removed needs testing Needs more testing for issue confirmation/qualification labels Dec 8, 2020
@ShriprasadM
Copy link

I can see, if the package is no under the src directory above issue is occurring. See the following screenshot. Execute same commands on Linux and macOS respectively

This is what I have observed

@ShriprasadM
Copy link

export GOPATH=`go env | grep GOPATH | awk -F "=" '{print $2}'|xargs`:/tmp/sample

If you execute this step, go run will work

@ShriprasadM
Copy link

When can we expect the fix for Linux build?

@ShriprasadM
Copy link

@fredbi any updates on this

@fredbi
Copy link
Contributor

fredbi commented Dec 11, 2020

@ShriprasadM as I've shown above, running the generate command with the proper environment set up just worked.

I failed to see any evidence of how this works differently on macOS (the screenshots above essentially show the same failure).

There might be some uncovered edge case in the way we resolve GOPATH with multiple values. However, this can be worked around rather easily.

If you feel strongly about it, you can contribute a PR. What we do for packages identification just relies on the standard library here

"golang.org/x/tools/go/packages"

@fredbi fredbi added the scanner label Dec 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
generate spec Related to spec generation from code install & setup scanner
Projects
None yet
Development

No branches or pull requests

4 participants