webapi icon indicating copy to clipboard operation
webapi copied to clipboard

Webapi-Swagger : Restructure generation of Types from Swagger doc.

Open kahlil29 opened this issue 5 years ago • 1 comments

This change involves breaking down the generated types into modules. The planned structure would look like :

New Types Generation Directory Structure

Some things we need to take care of while implementing this change :

  • All imports will be qualified (even in Contract.hs)
  • The modules in GlobalDefinitions would need to be imported into all (all routes, all methods) other Types.hs files.
  • In ResponseDefinitions.hs , we would also need to generate the Header types (if any are specified in the Swagger Doc) for the global response types (if any)
  • We need to take care that while generating the Contract, we are referring to the correct types at correct places (correct qualification) especially for Params.

kahlil29 avatar Apr 01 '19 14:04 kahlil29

Current approach (for collecting types and for generation) is :

Store (in state) a Hashmap with the following structure :

HashMap LevelInfo [ContractInnerType]

data LevelInfo = Global GlobalLocalType | Route (RouteName, MethodName) | Local GlobalLocalType (RouteName, MethodName)

data GlobalLocalType = DefinitionTy | ResponseTy | ParamTy

data ContractInnerType = ParamType CreateDataType | ResponseType CreateDataType | DefinitionType CreateDataType 

data CreateDataType = SumType DualSumType | ProductType NewData

data DualSumType = BasicEnum String [String] [String] | ComplexSumType String [(String, String)]

data NewData = NewData
  {
    mName :: String
  , mRecordTypes :: InnerRecords
  } 


data ApiTypeDetails = ApiTypeDetails
  {
    apiOut :: (LevelInfo, String)
  , apiErr :: (LevelInfo, Maybe String)
  , formParam :: (LevelInfo, Maybe String)
  , queryParam :: (LevelInfo, Maybe String)
  , fileParam :: (LevelInfo, Maybe String)
  , headerIn :: (LevelInfo, Maybe String)
  , requestBody :: (LevelInfo, Maybe String)
  , contentTypes :: (LevelInfo, Maybe String)
  , headerOut :: (LevelInfo, Maybe String)
  , hasXML :: Bool
  }

After we complete collection/calculation of all types from the Swagger Doc, we will generate the Contract and then do the following steps :

  1. Create folder with RouteName name
  2. Create folder with MethodName name
  3. Create a Types.hs module inside the above created folder.
  4. Generate/Write all the types necessary for this module (route-method) to the file. (we would also need to add the appropriate Module declaration, imports and any language extensions that are necessary)

kahlil29 avatar Apr 01 '19 14:04 kahlil29