mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-12-06 03:16:10 +00:00
* handle Alias models with newtypes * add inlineConsumesContentTypes cli option * generate swagger.yaml instead of swagger.json * check for/validate unhandled authMethods * refactoring
117 lines
4.4 KiB
Plaintext
117 lines
4.4 KiB
Plaintext
{{>partial_header}}
|
|
{-|
|
|
Module : {{title}}.Model
|
|
-}
|
|
|
|
{-# LANGUAGE DeriveDataTypeable #-}
|
|
{-# LANGUAGE DeriveFoldable #-}
|
|
{-# LANGUAGE DeriveGeneric #-}
|
|
{-# LANGUAGE DeriveTraversable #-}
|
|
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
|
|
{-# LANGUAGE NamedFieldPuns #-}
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
{-# LANGUAGE RecordWildCards #-}
|
|
{-# LANGUAGE TupleSections #-}
|
|
{-# LANGUAGE TypeFamilies #-}
|
|
{-# OPTIONS_GHC -fno-warn-unused-matches -fno-warn-unused-binds -fno-warn-unused-imports #-}
|
|
|
|
module {{title}}.Model where
|
|
|
|
import {{title}}.Core
|
|
|
|
import Data.Aeson ((.:),(.:!),(.:?),(.=))
|
|
|
|
import qualified Data.Aeson as A
|
|
import qualified Data.ByteString as B
|
|
import qualified Data.ByteString.Lazy as BL
|
|
import qualified Data.Data as P (Data, Typeable)
|
|
import qualified Data.Foldable as P
|
|
import qualified Data.HashMap.Lazy as HM
|
|
import qualified Data.Map as Map
|
|
import qualified Data.Maybe as P
|
|
import qualified Data.Set as Set
|
|
import qualified Data.Text as T
|
|
import qualified Data.Text.Encoding as T
|
|
import qualified Data.Time as TI
|
|
import qualified Web.FormUrlEncoded as WH
|
|
import qualified Web.HttpApiData as WH
|
|
|
|
import Control.Applicative ((<|>))
|
|
import Control.Applicative (Alternative)
|
|
import Data.Text (Text)
|
|
import Prelude (($), (.),(<$>),(<*>),(>>=),Maybe(..),Bool(..),Char,Double,FilePath,Float,Int,Integer,String,fmap,undefined,mempty,maybe,pure,Monad,Applicative,Functor)
|
|
|
|
import qualified Prelude as P
|
|
|
|
|
|
{{#imports}}import {{import}}
|
|
{{/imports}}
|
|
|
|
-- * Models
|
|
|
|
{{#models}}
|
|
{{#model}}
|
|
|
|
-- ** {{classname}}
|
|
-- | {{classname}}{{#title}}
|
|
-- {{{.}}}
|
|
-- {{/title}}{{#description}}
|
|
-- {{{.}}}{{/description}}{{#isAlias}}
|
|
newtype {{classname}} = {{classname}}
|
|
{ un{{classname}} :: {{{dataType}}}
|
|
} deriving (P.Eq, P.Show, P.Typeable, A.ToJSON, A.FromJSON, WH.ToHttpApiData, WH.FromHttpApiData{{#modelDeriving}}, {{modelDeriving}}{{/modelDeriving}}){{/isAlias}}{{^isAlias}}
|
|
data {{classname}} = {{classname}}
|
|
{ {{#vars}}{{name}} :: {{#x-strictFields}}!({{/x-strictFields}}{{^required}}Maybe {{/required}}{{datatype}}{{#x-strictFields}}){{/x-strictFields}} -- ^ {{#required}}/Required/ {{/required}}{{#readOnly}}/ReadOnly/ {{/readOnly}}"{{baseName}}"{{#description}} - {{description}}{{/description}}{{#hasMore}}
|
|
, {{/hasMore}}{{/vars}}
|
|
} deriving (P.Show, P.Eq, P.Typeable{{#modelDeriving}}, {{modelDeriving}}{{/modelDeriving}}){{/isAlias}}
|
|
|
|
{{^isAlias}}-- | FromJSON {{classname}}
|
|
instance A.FromJSON {{classname}} where
|
|
parseJSON = A.withObject "{{classname}}" $ \o ->
|
|
{{^hasVars}}pure {{/hasVars}}{{classname}}
|
|
{{#hasVars}}<$>{{/hasVars}}{{#vars}} (o {{#required}}.: {{/required}}{{^required}}{{^allowFromJsonNulls}}.:!{{/allowFromJsonNulls}}{{#allowFromJsonNulls}}.:?{{/allowFromJsonNulls}}{{/required}} "{{baseName}}"){{#hasMore}}
|
|
<*>{{/hasMore}}{{/vars}}
|
|
|
|
-- | ToJSON {{classname}}
|
|
instance A.ToJSON {{classname}} where
|
|
toJSON {{classname}} {{#hasVars}}{..}{{/hasVars}} =
|
|
{{^allowToJsonNulls}}_omitNulls{{/allowToJsonNulls}}{{#allowToJsonNulls}}A.object{{/allowToJsonNulls}}
|
|
[ {{#vars}}"{{baseName}}" .= {{name}}{{#hasMore}}
|
|
, {{/hasMore}}{{/vars}}
|
|
]
|
|
|
|
{{#vendorExtensions.x-hasMimeFormUrlEncoded}}
|
|
-- | FromForm {{classname}}
|
|
instance WH.FromForm {{classname}} where
|
|
fromForm f =
|
|
{{^hasVars}}pure {{/hasVars}}{{classname}}
|
|
{{#hasVars}}<$>{{/hasVars}}{{#vars}} ({{#required}}WH.parseUnique {{/required}}{{^required}}WH.parseMaybe {{/required}}"{{baseName}}" f){{#hasMore}}
|
|
<*>{{/hasMore}}{{/vars}}
|
|
|
|
-- | ToForm {{classname}}
|
|
instance WH.ToForm {{classname}} where
|
|
toForm {{classname}} {{#hasVars}}{..}{{/hasVars}} =
|
|
WH.Form $ HM.fromList $ P.catMaybes $
|
|
[ {{#vars}}_toFormItem "{{baseName}}" ({{#required}}Just {{/required}}{{name}}){{#hasMore}}
|
|
, {{/hasMore}}{{/vars}}
|
|
]
|
|
{{/vendorExtensions.x-hasMimeFormUrlEncoded}}
|
|
|
|
{{#generateModelConstructors}}
|
|
-- | Construct a value of type '{{classname}}' (by applying it's required fields, if any)
|
|
mk{{classname}}
|
|
:: {{#requiredVars}}{{{datatype}}} -- ^ '{{name}}'{{#description}}:{{/description}} {{{description}}}
|
|
-> {{/requiredVars}}{{classname}}
|
|
mk{{classname}} {{#requiredVars}}{{name}} {{/requiredVars}}=
|
|
{{classname}}
|
|
{ {{#vars}}{{#required}}{{name}}{{/required}}{{^required}}{{name}} = {{#isListContainer}}Nothing{{/isListContainer}}{{#isMapContainer}}Nothing{{/isMapContainer}}{{^isContainer}}Nothing{{/isContainer}}{{/required}}{{#hasMore}}
|
|
, {{/hasMore}}{{/vars}}
|
|
}
|
|
{{/generateModelConstructors}}
|
|
{{/isAlias}}
|
|
|
|
{{/model}}
|
|
{{/models}}
|
|
|
|
|