Skip to content

Commit

Permalink
Fix a bunch of minor bugs in the Haskell generator
Browse files Browse the repository at this point in the history
  • Loading branch information
gibiansky committed Apr 6, 2016
1 parent b8d723b commit 6d386aa
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ public HaskellServantCodegen() {
typeMapping.put("file", "FilePath");
typeMapping.put("number", "Double");
typeMapping.put("integer", "Int");
typeMapping.put("any", "Value");

importMapping.clear();
importMapping.put("Map", "qualified Data.Map as Map");
Expand Down Expand Up @@ -216,6 +217,9 @@ public void preprocessSwagger(Swagger swagger) {
additionalProperties.put("titleLower", apiName.substring(0, 1).toLowerCase() + apiName.substring(1));
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<>();
Object[] replacementChars = specialCharReplacements.keySet().toArray();
for(int i = 0; i < replacementChars.length; i++) {
Expand Down Expand Up @@ -269,6 +273,8 @@ public String getSwaggerType(Property p) {
return toModelName(type);
} else if(swaggerType == "object") {
type = "Value";
} else if(typeMapping.containsValue(swaggerType)) {
type = swaggerType + "_";
} else {
type = swaggerType;
}
Expand Down Expand Up @@ -423,7 +429,7 @@ public CodegenOperation fromOperation(String resourcePath, String httpMethod, Op
if (returnType.indexOf(" ") >= 0) {
returnType = "(" + returnType + ")";
}
path.add(camelize(op.httpMethod.toLowerCase()) + " '[JSON] " + returnType);
path.add("Verb '" + op.httpMethod.toUpperCase() + " 200 '[JSON] " + returnType);
type.add("m " + returnType);

op.vendorExtensions.put("x-routeType", joinStrings(" :> ", path));
Expand Down Expand Up @@ -473,6 +479,9 @@ public CodegenModel fromModel(String name, Model mod, Map<String, Model> allDefi

// Clean up the class name to remove invalid characters
model.classname = fixModelChars(model.classname);
if(typeMapping.containsValue(model.classname)) {
model.classname += "_";
}

// From the model name, compute the prefix for the fields.
String prefix = camelize(model.classname, true);
Expand All @@ -499,6 +508,7 @@ public CodegenModel fromModel(String name, Model mod, Map<String, Model> allDefi
public CodegenParameter fromParameter(Parameter param, Set<String> imports) {
CodegenParameter p = super.fromParameter(param, imports);
p.vendorExtensions.put("x-formParamName", camelize(p.baseName));
p.dataType = fixModelChars(p.dataType);
return p;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{-# LANGUAGE DataKinds, TypeFamilies, TypeOperators, FlexibleInstances, OverloadedStrings, ViewPatterns #-}
{-# 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 (
-- * Client and Server
ServerConfig(..),
Expand Down Expand Up @@ -33,13 +33,13 @@ import GHC.Exts (IsString(..))
import qualified Data.Map as Map
import GHC.Generics (Generic)
import Data.Monoid ((<>))
import Servant.API.Verbs (Verb, StdMethod(HEAD))
import Servant.API.Verbs (Verb, StdMethod(..))
import Control.Monad.Except (ExceptT)
import Network.HTTP.Client (Manager, newManager, defaultManagerSettings)
import Network.HTTP.Types.Method (methodOptions)


-- | HEAD with 200 status code.
type Head = Verb 'HEAD 200
instance ReflectMethod 'OPTIONS where
reflectMethod _ = methodOptions


{{#apiInfo}}{{#apis}}{{#operations}}{{#operation}}{{#hasFormParams}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ library
, transformers
, mtl
, http-client
, http-types
default-language: Haskell2010

0 comments on commit 6d386aa

Please sign in to comment.