swagger-api / swagger-codegen

swagger-codegen contains a template-driven engine to generate documentation, API clients and server stubs in different languages by parsing your OpenAPI / Swagger definition.
http://swagger.io
Apache License 2.0
16.9k stars 6.03k forks source link

[HASKELL] Build failure with "list" type after generation #8914

Open hasufell opened 5 years ago

hasufell commented 5 years ago
Description

Haskell GHC compiler fails to build the generated haskell code due to diverging data type names when the swagger type is list:

/home/maerwald/tmp/petstore/petstore/lib/SwaggerPetstore/API.hs:12:16: warning:
    -fcontext-stack=328 is deprecated: use -freduction-depth=328 instead
[1 of 2] Compiling SwaggerPetstore.Types ( lib/SwaggerPetstore/Types.hs, .stack-work/dist/x86_64-linux-tinfo6/Cabal-1.24.2.0/build/SwaggerPetstore/Types.o )
[2 of 2] Compiling SwaggerPetstore.API ( lib/SwaggerPetstore/API.hs, .stack-work/dist/x86_64-linux-tinfo6/Cabal-1.24.2.0/build/SwaggerPetstore/API.o )

/home/maerwald/tmp/petstore/petstore/lib/SwaggerPetstore/API.hs:67:43: error:
    Not in scope: type constructor or class ‘List’
    Perhaps you meant one of these:     
      ‘List_’ (imported from SwaggerPetstore.Types),
      ‘HList’ (imported from Servant.API)

/home/maerwald/tmp/petstore/petstore/lib/SwaggerPetstore/API.hs:130:19: error:
    Not in scope: type constructor or class ‘List’
    Perhaps you meant one of these:     
      ‘List_’ (imported from SwaggerPetstore.Types),
      ‘HList’ (imported from Servant.API)
Completed 74 action(s).                 

--  While building package swagger-petstore-0.1.0.0 using:
      /home/maerwald/.stack/setup-exe-cache/x86_64-linux-tinfo6/Cabal-simple_mPHDZzAJ_1.24.2.0_ghc-8.0.2 --builddir=.stack-work/dist/x86_64-linux-tinfo6/Cabal-1.24.2.0 build lib:swagger-petstore --ghc-options " -ddump-hi -ddump-to-file"
    Process exited with code: ExitFailure 1

The data type that is generated is:

-- | 
data List_ = List_
  { list_Id :: Integer -- ^ 
  , list_Name :: Text -- ^ 
  , list_Tag :: Text -- ^ 
  } deriving (Show, Eq, Generic)

But the API will use:

data SwaggerPetstoreBackend m = SwaggerPetstoreBackend
  { petsGet :: m [List]{- ^ Returns all pets from the system that the user has access to -}
  }

The whole generated project can be viewed at https://github.com/hasufell/petstore-swagger-bug

Swagger-codegen version

2.4.0-SNAPSHOT built from git d9c4e014e6791072b1c71faa4a77098f9bdcc3a4

Swagger declaration file content or url

This is derived from the minimal swagger petstore definition. The pet type has been renamed to list:

{
  "swagger": "2.0",
  "info": {
    "version": "1.0.0",
    "title": "Swagger Petstore",
    "description": "A sample API that uses a petstore as an example to demonstrate features in the swagger-2.0 specification",
    "termsOfService": "http://swagger.io/terms/",
    "contact": {
      "name": "Swagger API Team"
    },
    "license": {
      "name": "MIT"
    }
  },
  "host": "petstore.swagger.io",
  "basePath": "/api",
  "schemes": [
    "http"
  ],
  "consumes": [
    "application/json"
  ],
  "produces": [
    "application/json"
  ],
  "paths": {
    "/pets": {
      "get": {
        "description": "Returns all pets from the system that the user has access to",
        "produces": [
          "application/json"
        ],
        "responses": {
          "200": {
            "description": "A list of pets.",
            "schema": {
              "type": "array",
              "items": {
                "$ref": "#/definitions/list"
              }
            }
          }
        }
      }
    }
  },
  "definitions": {
    "list": {
      "type": "object",
      "required": [
        "id",
        "name"
      ],
      "properties": {
        "id": {
          "type": "integer",
          "format": "int64"
        },
        "name": {
          "type": "string"
        },
        "tag": {
          "type": "string"
        }
      }
    }
  }
}
Command line used for generation
java -jar "modules/swagger-codegen-cli/target/swagger-codegen-cli.jar" generate -i petstore-minimal.json     -l haskell -o petstore
Steps to reproduce

After generation, run:

adamConnerSax commented 5 years ago

There are an assortment of issues with the haskell-servant backend. I've been working on some related to the "Date" type and underscores and updates to servant. All of that is here: https://github.com/adamConnerSax/swagger-codegen/tree/UpdateHaskellServant Once I get all my issues fixed, I'll try and see what happens to yours and then update here.
I can see in the Java that swagger-codegen treats "List" as a primitive (!!) and also renames the swagger type "array" to "List" neither of which really make sense to me. So maybe I'll try to sort that out. But I don't really know java or the rest of swagger-codegen so I'm only able to do very simple things.

adamConnerSax commented 5 years ago

I think I fixed this one as well. Just involved removing "List" as a reserved word. There's what I think is a working version at the link above if you just need it to work. I'm not submitting the new version as a PR to swagger-codegen until I check some more things and get someone with more (read: any) servant experience to check that I haven't caused new issues.

hasufell commented 5 years ago

We use swagger-codegen for https://hackage.haskell.org/package/docusign-base in production However, there are a number of things that are not fully automated: E.g. the resulting docusign module is >7k LOC and will just blow up your ram, so we separated all types into single modules. You can check out https://github.com/capital-match/docusign-base#basic-steps if you want to see if the codegen does something funky. I would say that project (docusign) is big enough to stumble over a lot of things if something goes wrong... I might also give your branch a try!

The swagger definition is here

adamConnerSax commented 5 years ago

Thanks! Maybe I will give that a try. Also, there is apparently a new version of swagger-codegen happening https://github.com/openapitools/openapi-generator And they are going to incorporate the fixes there, with the help of the haskell-servant backend authors.

hasufell commented 5 years ago

Also, there is apparently a new version of swagger-codegen happening https://github.com/openapitools/openapi-generator

That version still generates a 7k LOC Types.hs file ;)

And it appears to have the same bug with List_ vs [List]