Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions cli/src/Cli.elm
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type alias CliOptions =
, writeMergedTo : Maybe String
, noElmFormat : Bool
, keepGoing : Bool
, noEnumSort : Bool
}


Expand Down Expand Up @@ -186,6 +187,10 @@ program =
(Cli.Option.flag "keep-going"
|> Cli.Option.withDescription "If a route can't be generated, skip it instead of erroring out."
)
|> Cli.OptionsParser.with
(Cli.Option.flag "no-enum-sort"
|> Cli.Option.withDescription "Don't sort enum variants alphabetically, preserve the order from the spec."
)
)


Expand Down Expand Up @@ -388,6 +393,7 @@ parseCliOptions cliOptions =
|> OpenApi.Config.withAutoConvertSwagger cliOptions.autoConvertSwagger
|> OpenApi.Config.withNoElmFormat cliOptions.noElmFormat
|> OpenApi.Config.withKeepGoing cliOptions.keepGoing
|> OpenApi.Config.withNoEnumSort cliOptions.noEnumSort
|> maybe OpenApi.Config.withSwaggerConversionUrl cliOptions.swaggerConversionUrl
|> maybe OpenApi.Config.withSwaggerConversionCommand
(cliOptions.swaggerConversionCommand
Expand Down
2 changes: 2 additions & 0 deletions docs/USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
- `[--effect-types <effect types>]`: A list of which kind of APIs to generate. Each item should be of the form `package.type`. If `package` is omitted it defaults to `elm/http`. If `type` is omitted it defaults to `cmd,task`. The options for package are: `elm/http`, `dillonkearns/elm-pages`, `lamdera/program-test`. The options for type are: `cmd`, `cmdrisky`, `cmdrecord`, `task`, `taskrisky`, `taskrecord`.
- `[--server <server>]`: The base URL for the OpenAPI server. If not specified this will be extracted from the OAS or default to root of the web application. You can pass in an object to define multiple servers, like `{"dev": "http://localhost", "prod": "https://example.com"}`. This will add a `server` parameter to functions and define a `Servers` module with your servers. You can pass in an empty object if you have fully dynamic servers.
- `[--no-elm-format]`: Don't run elm-format on the outputs.
- `[--no-enum-sort]`: Don't sort enum variants alphabetically, preserve the order from the spec.
- `[--keep-going]`: If a route can't be generated, skip it instead of erroring out.

## Example outputs:

Assume we have an OAS file named `my-cool-company-oas.json` and it has a field `"title": "My Coool Company"` and we run the CLI like so
Expand Down
12 changes: 11 additions & 1 deletion src/CliMonad.elm
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module CliMonad exposing
, withPath, withWarning, withExtendedWarning, withRequiredPackage
, todo, todoWithDefault
, withFormat
, noEnumSort
, nameToAnnotation, refToAnnotation, refToEncoder, refToDecoder
)

Expand All @@ -22,6 +23,7 @@ module CliMonad exposing
@docs withPath, withWarning, withExtendedWarning, withRequiredPackage
@docs todo, todoWithDefault
@docs withFormat
@docs noEnumSort


## Utils
Expand Down Expand Up @@ -88,6 +90,7 @@ type alias Input =
, formats : FastDict.Dict InternalFormatName InternalFormat
, warnOnMissingEnums : Bool
, keepGoing : Bool
, noEnumSort : Bool
}


Expand Down Expand Up @@ -130,6 +133,7 @@ run :
, formats : List OpenApi.Config.Format
, warnOnMissingEnums : Bool
, keepGoing : Bool
, noEnumSort : Bool
}
-> CliMonad (List Declaration)
->
Expand All @@ -153,6 +157,7 @@ run oneOfDeclarations input (CliMonad x) =
|> FastDict.fromList
, warnOnMissingEnums = input.warnOnMissingEnums
, keepGoing = input.keepGoing
, noEnumSort = input.noEnumSort
}

res : Result Message ( List Declaration, Output, Cache )
Expand Down Expand Up @@ -575,6 +580,11 @@ getApiSpec =
CliMonad (\input cache -> Ok ( input.openApi, emptyOutput, cache ))


noEnumSort : CliMonad Bool
noEnumSort =
CliMonad (\input cache -> Ok ( input.noEnumSort, emptyOutput, cache ))


{-| If the user has chosen to keep going in the face of errors, this will convert an error into a warning. Otherwise this returns the input
-}
errorToWarning : CliMonad a -> CliMonad (Maybe a)
Expand Down Expand Up @@ -664,7 +674,7 @@ enumName : List Common.UnsafeName -> CliMonad (Maybe Common.UnsafeName)
enumName variants =
CliMonad
(\input cache ->
case FastDict.get (List.map Common.unwrapUnsafe variants) input.enums of
case FastDict.get (List.sort (List.map Common.unwrapUnsafe variants)) input.enums of
Just { name } ->
Ok ( Just name, emptyOutput, cache )

Expand Down
1 change: 0 additions & 1 deletion src/Common.elm
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,6 @@ unwrapUnsafe (UnsafeName name) =
enum : ( String, List String ) -> Type
enum variants =
variants
|> NonEmpty.sort
|> NonEmpty.map UnsafeName
|> Enum

Expand Down
27 changes: 23 additions & 4 deletions src/OpenApi/Config.elm
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
module OpenApi.Config exposing
( Config, EffectType(..), effectTypeToPackage, Format, Input, Path(..), Server(..)
, init, inputFrom, pathFromString
, withAutoConvertSwagger, AutoConvertSwagger(..), withEffectTypes, withFormat, withFormats, withGenerateTodos, withInput, withSwaggerConversionCommand, withSwaggerConversionUrl, withNoElmFormat, withKeepGoing
, withAutoConvertSwagger, AutoConvertSwagger(..), withEffectTypes, withFormat, withFormats, withGenerateTodos, withInput, withSwaggerConversionCommand, withSwaggerConversionUrl, withNoElmFormat, withKeepGoing, withNoEnumSort
, withOutputModuleName, withOverrides, withServer, withWriteMergedTo, withWarnOnMissingEnums
, autoConvertSwagger, inputs, outputDirectory, swaggerConversionCommand, swaggerConversionUrl, noElmFormat, keepGoing
, autoConvertSwagger, inputs, outputDirectory, swaggerConversionCommand, swaggerConversionUrl, noElmFormat, keepGoing, noEnumSort
, oasPath, overrides, writeMergedTo
, toGenerationConfig, Generate, pathToString
, defaultFormats
Expand All @@ -20,13 +20,13 @@ module OpenApi.Config exposing
# Creation

@docs init, inputFrom, pathFromString
@docs withAutoConvertSwagger, AutoConvertSwagger, withEffectTypes, withFormat, withFormats, withGenerateTodos, withInput, withSwaggerConversionCommand, withSwaggerConversionUrl, withNoElmFormat, withKeepGoing
@docs withAutoConvertSwagger, AutoConvertSwagger, withEffectTypes, withFormat, withFormats, withGenerateTodos, withInput, withSwaggerConversionCommand, withSwaggerConversionUrl, withNoElmFormat, withKeepGoing, withNoEnumSort
@docs withOutputModuleName, withOverrides, withServer, withWriteMergedTo, withWarnOnMissingEnums


# Config properties

@docs autoConvertSwagger, inputs, outputDirectory, swaggerConversionCommand, swaggerConversionUrl, noElmFormat, keepGoing
@docs autoConvertSwagger, inputs, outputDirectory, swaggerConversionCommand, swaggerConversionUrl, noElmFormat, keepGoing, noEnumSort


# Input properties
Expand Down Expand Up @@ -84,6 +84,7 @@ type Config
, dynamicFormats : List { format : String, basicType : Common.BasicType } -> List Format
, noElmFormat : Bool
, keepGoing : Bool
, noEnumSort : Bool
}


Expand Down Expand Up @@ -253,6 +254,7 @@ init initialOutputDirectory =
, dynamicFormats = \_ -> []
, noElmFormat = False
, keepGoing = False
, noEnumSort = False
}
|> Config

Expand Down Expand Up @@ -575,6 +577,14 @@ withKeepGoing newKeepGoing (Config config) =
Config { config | keepGoing = newKeepGoing }


{-| When `True`, enum variants are kept in the order they appear in the OpenAPI spec
instead of being sorted alphabetically. Defaults to `False`.
-}
withNoEnumSort : Bool -> Config -> Config
withNoEnumSort newNoEnumSort (Config config) =
Config { config | noEnumSort = newNoEnumSort }



-------------
-- Getters --
Expand Down Expand Up @@ -623,6 +633,13 @@ keepGoing (Config config) =
config.keepGoing


{-| Whether enum variants should be kept in spec-defined order instead of sorted alphabetically.
-}
noEnumSort : Config -> Bool
noEnumSort (Config config) =
config.noEnumSort


{-| -}
oasPath : Input -> Path
oasPath (Input input) =
Expand Down Expand Up @@ -656,6 +673,7 @@ type alias Generate =
, formats : List Format
, warnOnMissingEnums : Bool
, keepGoing : Bool
, noEnumSort : Bool
}


Expand Down Expand Up @@ -690,6 +708,7 @@ toGenerationConfig formatsInput (Config config) augmentedInputs =
, warnOnMissingEnums = input.warnOnMissingEnums
, formats = config.staticFormats ++ config.dynamicFormats formatsInput
, keepGoing = config.keepGoing
, noEnumSort = config.noEnumSort
}
, spec
)
Expand Down
3 changes: 2 additions & 1 deletion src/OpenApi/Generate.elm
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ files :
, warnings : List Message
, requiredPackages : FastSet.Set String
}
files { namespace, generateTodos, effectTypes, server, formats, warnOnMissingEnums, keepGoing } apiSpec =
files { namespace, generateTodos, effectTypes, server, formats, warnOnMissingEnums, keepGoing, noEnumSort } apiSpec =
case extractEnums apiSpec of
Err e ->
Err e
Expand Down Expand Up @@ -156,6 +156,7 @@ files { namespace, generateTodos, effectTypes, server, formats, warnOnMissingEnu
, formats = formats
, warnOnMissingEnums = warnOnMissingEnums
, keepGoing = keepGoing
, noEnumSort = noEnumSort
}
|> Result.map
(\{ declarations, warnings, requiredPackages } ->
Expand Down
32 changes: 20 additions & 12 deletions src/SchemaUtils.elm
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,23 @@ schemaToType seen schema =
}
)

enumType : NonEmpty String -> CliMonad { type_ : Common.Type, documentation : Maybe String }
enumType decodedEnums =
CliMonad.noEnumSort
|> CliMonad.map
(\noSort ->
{ type_ =
Common.enum
(if noSort then
decodedEnums

else
NonEmpty.sort decodedEnums
)
, documentation = subSchema.description
}
)

singleTypeToType : Json.Schema.Definitions.SingleType -> CliMonad { type_ : Common.Type, documentation : Maybe String }
singleTypeToType singleType =
let
Expand Down Expand Up @@ -380,10 +397,7 @@ schemaToType seen schema =
singleTypeToType Json.Schema.Definitions.StringType

Ok (Just { decodedEnums, hasNull }) ->
CliMonad.succeed
{ type_ = Common.enum decodedEnums
, documentation = subSchema.description
}
enumType decodedEnums
|> (if hasNull then
nullable

Expand Down Expand Up @@ -466,10 +480,7 @@ schemaToType seen schema =
CliMonad.succeed { type_ = Common.Value, documentation = subSchema.description }

Ok (Just { decodedEnums, hasNull }) ->
CliMonad.succeed
{ type_ = Common.enum decodedEnums
, documentation = subSchema.description
}
enumType decodedEnums
|> (if hasNull then
nullable

Expand All @@ -486,10 +497,7 @@ schemaToType seen schema =
nullable (singleTypeToType Json.Schema.Definitions.StringType)

Ok (Just { decodedEnums }) ->
CliMonad.succeed
{ type_ = Common.enum decodedEnums
, documentation = subSchema.description
}
enumType decodedEnums
|> nullable

Err e ->
Expand Down
Loading
Loading