Fix a bunch of minor bugs in the Haskell generator

This commit is contained in:
Andrew Gibiansky 2016-04-05 17:47:23 -07:00
parent b8d723b7ae
commit 6d386aaa90
3 changed files with 17 additions and 6 deletions

View File

@ -156,6 +156,7 @@ public class HaskellServantCodegen extends DefaultCodegen implements CodegenConf
typeMapping.put("file", "FilePath"); typeMapping.put("file", "FilePath");
typeMapping.put("number", "Double"); typeMapping.put("number", "Double");
typeMapping.put("integer", "Int"); typeMapping.put("integer", "Int");
typeMapping.put("any", "Value");
importMapping.clear(); importMapping.clear();
importMapping.put("Map", "qualified Data.Map as Map"); importMapping.put("Map", "qualified Data.Map as Map");
@ -216,6 +217,9 @@ public class HaskellServantCodegen extends DefaultCodegen implements CodegenConf
additionalProperties.put("titleLower", apiName.substring(0, 1).toLowerCase() + apiName.substring(1)); additionalProperties.put("titleLower", apiName.substring(0, 1).toLowerCase() + apiName.substring(1));
additionalProperties.put("package", cabalName); additionalProperties.put("package", cabalName);
// Due to the way servant resolves types, we need a high context stack limit
additionalProperties.put("contextStackLimit", swagger.getPaths().size() * 2 + 300);
List<Map<String, Object>> replacements = new ArrayList<>(); List<Map<String, Object>> replacements = new ArrayList<>();
Object[] replacementChars = specialCharReplacements.keySet().toArray(); Object[] replacementChars = specialCharReplacements.keySet().toArray();
for(int i = 0; i < replacementChars.length; i++) { for(int i = 0; i < replacementChars.length; i++) {
@ -269,6 +273,8 @@ public class HaskellServantCodegen extends DefaultCodegen implements CodegenConf
return toModelName(type); return toModelName(type);
} else if(swaggerType == "object") { } else if(swaggerType == "object") {
type = "Value"; type = "Value";
} else if(typeMapping.containsValue(swaggerType)) {
type = swaggerType + "_";
} else { } else {
type = swaggerType; type = swaggerType;
} }
@ -423,7 +429,7 @@ public class HaskellServantCodegen extends DefaultCodegen implements CodegenConf
if (returnType.indexOf(" ") >= 0) { if (returnType.indexOf(" ") >= 0) {
returnType = "(" + returnType + ")"; returnType = "(" + returnType + ")";
} }
path.add(camelize(op.httpMethod.toLowerCase()) + " '[JSON] " + returnType); path.add("Verb '" + op.httpMethod.toUpperCase() + " 200 '[JSON] " + returnType);
type.add("m " + returnType); type.add("m " + returnType);
op.vendorExtensions.put("x-routeType", joinStrings(" :> ", path)); op.vendorExtensions.put("x-routeType", joinStrings(" :> ", path));
@ -473,6 +479,9 @@ public class HaskellServantCodegen extends DefaultCodegen implements CodegenConf
// Clean up the class name to remove invalid characters // Clean up the class name to remove invalid characters
model.classname = fixModelChars(model.classname); model.classname = fixModelChars(model.classname);
if(typeMapping.containsValue(model.classname)) {
model.classname += "_";
}
// From the model name, compute the prefix for the fields. // From the model name, compute the prefix for the fields.
String prefix = camelize(model.classname, true); String prefix = camelize(model.classname, true);
@ -499,6 +508,7 @@ public class HaskellServantCodegen extends DefaultCodegen implements CodegenConf
public CodegenParameter fromParameter(Parameter param, Set<String> imports) { public CodegenParameter fromParameter(Parameter param, Set<String> imports) {
CodegenParameter p = super.fromParameter(param, imports); CodegenParameter p = super.fromParameter(param, imports);
p.vendorExtensions.put("x-formParamName", camelize(p.baseName)); p.vendorExtensions.put("x-formParamName", camelize(p.baseName));
p.dataType = fixModelChars(p.dataType);
return p; return p;
} }
} }

View File

@ -1,6 +1,6 @@
{-# LANGUAGE DataKinds, TypeFamilies, TypeOperators, FlexibleInstances, OverloadedStrings, ViewPatterns #-} {-# LANGUAGE DataKinds, TypeFamilies, TypeOperators, FlexibleInstances, OverloadedStrings, ViewPatterns #-}
{-# LANGUAGE RecordWildCards, GeneralizedNewtypeDeriving, DeriveTraversable, FlexibleContexts, DeriveGeneric #-} {-# LANGUAGE RecordWildCards, GeneralizedNewtypeDeriving, DeriveTraversable, FlexibleContexts, DeriveGeneric #-}
{-# OPTIONS_GHC -fno-warn-unused-binds -fno-warn-unused-imports #-} {-# OPTIONS_GHC -fno-warn-unused-binds -fno-warn-unused-imports -fcontext-stack={{contextStackLimit}} #-}
module {{title}}.API ( module {{title}}.API (
-- * Client and Server -- * Client and Server
ServerConfig(..), ServerConfig(..),
@ -33,13 +33,13 @@ import GHC.Exts (IsString(..))
import qualified Data.Map as Map import qualified Data.Map as Map
import GHC.Generics (Generic) import GHC.Generics (Generic)
import Data.Monoid ((<>)) import Data.Monoid ((<>))
import Servant.API.Verbs (Verb, StdMethod(HEAD)) import Servant.API.Verbs (Verb, StdMethod(..))
import Control.Monad.Except (ExceptT) import Control.Monad.Except (ExceptT)
import Network.HTTP.Client (Manager, newManager, defaultManagerSettings) import Network.HTTP.Client (Manager, newManager, defaultManagerSettings)
import Network.HTTP.Types.Method (methodOptions)
instance ReflectMethod 'OPTIONS where
-- | HEAD with 200 status code. reflectMethod _ = methodOptions
type Head = Verb 'HEAD 200
{{#apiInfo}}{{#apis}}{{#operations}}{{#operation}}{{#hasFormParams}} {{#apiInfo}}{{#apis}}{{#operations}}{{#operation}}{{#hasFormParams}}

View File

@ -29,4 +29,5 @@ library
, transformers , transformers
, mtl , mtl
, http-client , http-client
, http-types
default-language: Haskell2010 default-language: Haskell2010