Skip to content

Commit d624b28

Browse files
Fjolnir-Dvorakjonschoning
authored andcommitted
[FEATURE][Haskell] Haskell-Servant serves static files (#4058)
* updated the golden files for haskell to be able to generate against those * Haskell-servant now serves static files which are in a directory called "static" * I missed to regenerate the docs directory
1 parent 4958ad7 commit d624b28

File tree

5 files changed

+32
-6
lines changed

5 files changed

+32
-6
lines changed

docs/generators/haskell.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ sidebar_label: haskell
1111
|prependFormOrBodyParameters|Add form or body parameters to the beginning of the parameter list.| |false|
1212
|modelPackage|package for generated models| |null|
1313
|apiPackage|package for generated api classes| |null|
14+
|serveStatic|serve will serve files from the directory 'static'.| |true|

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/HaskellServantCodegen.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ public class HaskellServantCodegen extends DefaultCodegen implements CodegenConf
4343
protected String apiVersion = "0.0.1";
4444
private static final Pattern LEADING_UNDERSCORE = Pattern.compile("^_+");
4545

46+
public static final String PROP_SERVE_STATIC = "serveStatic";
47+
public static final String PROP_SERVE_STATIC_DESC = "serve will serve files from the directory 'static'.";
48+
public static final Boolean PROP_SERVE_STATIC_DEFAULT = Boolean.TRUE;
49+
4650
/**
4751
* Configures the type of generator.
4852
*
@@ -183,6 +187,15 @@ public HaskellServantCodegen() {
183187

184188
cliOptions.add(new CliOption(CodegenConstants.MODEL_PACKAGE, CodegenConstants.MODEL_PACKAGE_DESC));
185189
cliOptions.add(new CliOption(CodegenConstants.API_PACKAGE, CodegenConstants.API_PACKAGE_DESC));
190+
cliOptions.add(new CliOption(PROP_SERVE_STATIC, PROP_SERVE_STATIC_DESC).defaultValue(PROP_SERVE_STATIC_DEFAULT.toString()));
191+
}
192+
193+
public void setBooleanProperty(String property, Boolean defaultValue) {
194+
if (additionalProperties.containsKey(property)) {
195+
additionalProperties.put(property, convertPropertyToBoolean(property));
196+
} else {
197+
additionalProperties.put(property, defaultValue);
198+
}
186199
}
187200

188201
@Override
@@ -192,6 +205,8 @@ public void processOpts() {
192205
if (StringUtils.isEmpty(System.getenv("HASKELL_POST_PROCESS_FILE"))) {
193206
LOGGER.info("Hint: Environment variable HASKELL_POST_PROCESS_FILE not defined so the Haskell code may not be properly formatted. To define it, try 'export HASKELL_POST_PROCESS_FILE=\"$HOME/.local/bin/hfmt -w\"' (Linux/Mac)");
194207
}
208+
209+
setBooleanProperty(PROP_SERVE_STATIC, PROP_SERVE_STATIC_DEFAULT);
195210
}
196211

197212
/**

modules/openapi-generator/src/main/resources/haskell-servant/API.mustache

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ import Servant.Client (ClientEnv, Scheme (Http), C
6161
mkClientEnv, parseBaseUrl)
6262
import Servant.Client.Core (baseUrlPort, baseUrlHost)
6363
import Servant.Client.Internal.HttpClient (ClientM (..))
64-
import Servant.Server (Handler (..))
64+
import Servant.Server (Handler (..)){{#serveStatic}}
65+
import Servant.Server.StaticFiles (serveDirectoryFileServer){{/serveStatic}}
6566
import Web.FormUrlEncoded
6667
import Web.HttpApiData
6768

@@ -131,7 +132,8 @@ formatSeparatedQueryList char = T.intercalate (T.singleton char) . map toQueryPa
131132
type {{title}}API
132133
= {{#apis}}{{#operations}}{{#operation}}{{& vendorExtensions.x-routeType}} -- '{{operationId}}' route{{#hasMore}}
133134
:<|> {{/hasMore}}{{/operation}}{{/operations}}{{#hasMore}}
134-
:<|> {{/hasMore}}{{/apis}}
135+
:<|> {{/hasMore}}{{/apis}}{{#serveStatic}}
136+
:<|> Raw {{/serveStatic}}
135137
{{/apiInfo}}
136138

137139

@@ -183,7 +185,8 @@ create{{title}}Client = {{title}}Backend{..}
183185
where
184186
({{#apis}}{{#operations}}{{#operation}}(coerce -> {{operationId}}){{#hasMore}} :<|>
185187
{{/hasMore}}{{/operation}}{{/operations}}{{#hasMore}} :<|>
186-
{{/hasMore}}{{/apis}}) = client (Proxy :: Proxy {{title}}API)
188+
{{/hasMore}}{{/apis}}{{#serveStatic}} :<|>
189+
_{{/serveStatic}}) = client (Proxy :: Proxy {{title}}API)
187190

188191
-- | Run requests in the {{title}}Client monad.
189192
run{{title}}Client :: Config -> {{title}}Client a -> ExceptT ClientError IO a
@@ -234,5 +237,6 @@ run{{title}}MiddlewareServer Config{..} middleware backend = do
234237
serverFromBackend {{title}}Backend{..} =
235238
({{#apis}}{{#operations}}{{#operation}}coerce {{operationId}}{{#hasMore}} :<|>
236239
{{/hasMore}}{{/operation}}{{/operations}}{{#hasMore}} :<|>
237-
{{/hasMore}}{{/apis}})
240+
{{/hasMore}}{{/apis}}{{#serveStatic}} :<|>
241+
serveDirectoryFileServer "static"{{/serveStatic}})
238242
{{/apiInfo}}

modules/openapi-generator/src/test/java/org/openapitools/codegen/options/HaskellServantOptionsProvider.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import com.google.common.collect.ImmutableMap;
2121
import org.openapitools.codegen.CodegenConstants;
22+
import org.openapitools.codegen.languages.HaskellServantCodegen;
2223

2324
import java.util.Map;
2425

@@ -44,6 +45,7 @@ public Map<String, String> createOptions() {
4445
.put(CodegenConstants.ENSURE_UNIQUE_PARAMS, ENSURE_UNIQUE_PARAMS_VALUE)
4546
.put(CodegenConstants.ALLOW_UNICODE_IDENTIFIERS, ALLOW_UNICODE_IDENTIFIERS_VALUE)
4647
.put(CodegenConstants.PREPEND_FORM_OR_BODY_PARAMETERS, PREPEND_FORM_OR_BODY_PARAMETERS_VALUE)
48+
.put(HaskellServantCodegen.PROP_SERVE_STATIC, HaskellServantCodegen.PROP_SERVE_STATIC_DEFAULT.toString())
4749
.build();
4850
}
4951

samples/server/petstore/haskell-servant/lib/OpenAPIPetstore/API.hs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ import Servant.Client (ClientEnv, Scheme (Http), C
6262
import Servant.Client.Core (baseUrlPort, baseUrlHost)
6363
import Servant.Client.Internal.HttpClient (ClientM (..))
6464
import Servant.Server (Handler (..))
65+
import Servant.Server.StaticFiles (serveDirectoryFileServer)
6566
import Web.FormUrlEncoded
6667
import Web.HttpApiData
6768

@@ -156,6 +157,7 @@ type OpenAPIPetstoreAPI
156157
:<|> "user" :> "login" :> QueryParam "username" Text :> QueryParam "password" Text :> Verb 'GET 200 '[JSON] Text -- 'loginUser' route
157158
:<|> "user" :> "logout" :> Verb 'GET 200 '[JSON] () -- 'logoutUser' route
158159
:<|> "user" :> Capture "username" Text :> ReqBody '[JSON] User :> Verb 'PUT 200 '[JSON] () -- 'updateUser' route
160+
:<|> Raw
159161

160162

161163
-- | Server or client configuration, specifying the host and port to query or serve on.
@@ -237,7 +239,8 @@ createOpenAPIPetstoreClient = OpenAPIPetstoreBackend{..}
237239
(coerce -> getUserByName) :<|>
238240
(coerce -> loginUser) :<|>
239241
(coerce -> logoutUser) :<|>
240-
(coerce -> updateUser)) = client (Proxy :: Proxy OpenAPIPetstoreAPI)
242+
(coerce -> updateUser) :<|>
243+
_) = client (Proxy :: Proxy OpenAPIPetstoreAPI)
241244

242245
-- | Run requests in the OpenAPIPetstoreClient monad.
243246
runOpenAPIPetstoreClient :: Config -> OpenAPIPetstoreClient a -> ExceptT ClientError IO a
@@ -303,4 +306,5 @@ runOpenAPIPetstoreMiddlewareServer Config{..} middleware backend = do
303306
coerce getUserByName :<|>
304307
coerce loginUser :<|>
305308
coerce logoutUser :<|>
306-
coerce updateUser)
309+
coerce updateUser :<|>
310+
serveDirectoryFileServer "static")

0 commit comments

Comments
 (0)