Skip to content
Open
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
15 changes: 9 additions & 6 deletions cli/example/nullable-enum.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ info:
version: 1.0.0
paths:
"/":
summary: Does stuff
responses:
200:
description: Ok
schema:
"#ref": "#/components/schemas/NullableEnum"
get:
summary: Does stuff
responses:
200:
description: Ok
content:
application/json:
schema:
"$ref": "#/components/schemas/NullableEnum"
components:
schemas:
NullableEnum:
Expand Down
15 changes: 11 additions & 4 deletions cli/example/overriding-global-security.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ components:
Data:
description: "Data"
type: string
responses:
Data:
description: "Data"
content:
application/json:
schema:
$ref: "#/components/schemas/Data"
securitySchemes:
Token:
type: apiKey
Expand All @@ -22,7 +29,7 @@ paths:
operationId: GetProtectedData
responses:
200:
$ref: "#/components/schemas/Data"
$ref: "#/components/responses/Data"

"/api/unprotected":
get:
Expand All @@ -32,7 +39,7 @@ paths:
security: []
responses:
200:
$ref: "#/components/schemas/Data"
$ref: "#/components/responses/Data"
post:
summary: Unprotected endpoint to store data
# Whoops! Security should be overridden here like for GET. Let's fix it
Expand All @@ -41,7 +48,7 @@ paths:
operationId: PostUnprotectedData
responses:
200:
$ref: "#/components/schemas/Data"
$ref: "#/components/responses/Data"
patch:
summary: Unprotected endpoint to store data (again)
# Whoops! This is supposed to be unprotected, like GET. Let's fix it
Expand All @@ -52,4 +59,4 @@ paths:
operationId: PatchUnprotectedData
responses:
200:
$ref: "#/components/schemas/Data"
$ref: "#/components/responses/Data"
8 changes: 6 additions & 2 deletions cli/example/recursive-allof-refs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,13 @@ components:
paths:
"/api/grand-child":
summary: Get a GrandChild object.
description:
description: Example description
get:
operationId: getGrandChild
responses:
200:
$ref: "#/components/schemas/GrandChild"
description: Success
content:
application/json:
schema:
$ref: "#/components/schemas/GrandChild"
5 changes: 5 additions & 0 deletions cli/src/Cli.elm
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type alias CliOptions =
, overrides : List OpenApi.Config.Path
, writeMergedTo : Maybe String
, noElmFormat : Bool
, keepGoing : Bool
}


Expand Down Expand Up @@ -96,6 +97,8 @@ program =
(Cli.Option.optionalKeywordArg "write-merged-to")
|> Cli.OptionsParser.with
(Cli.Option.flag "no-elm-format")
|> Cli.OptionsParser.with
(Cli.Option.flag "keep-going")
|> Cli.OptionsParser.withDoc
([ ""
, """version: 0.7.0"""
Expand Down Expand Up @@ -166,6 +169,7 @@ program =
, formatOption "overrides" [ "Load an additional file to override parts of the original Open API file." ]
, formatOption "write-merged-to" [ "Write the merged Open API spec to the given file." ]
, formatOption "no-elm-format" [ "Don't run elm-format on the outputs." ]
, formatOption "keep-going" [ "If a route can't be generated, skip it instead of erroring out." ]
]
|> String.join "\n\n"
)
Expand Down Expand Up @@ -370,6 +374,7 @@ parseCliOptions cliOptions =
|> OpenApi.Config.withGenerateTodos cliOptions.generateTodos
|> OpenApi.Config.withAutoConvertSwagger cliOptions.autoConvertSwagger
|> OpenApi.Config.withNoElmFormat cliOptions.noElmFormat
|> OpenApi.Config.withKeepGoing cliOptions.keepGoing
|> maybe OpenApi.Config.withSwaggerConversionUrl cliOptions.swaggerConversionUrl
|> maybe OpenApi.Config.withSwaggerConversionCommand
(cliOptions.swaggerConversionCommand
Expand Down
1 change: 1 addition & 0 deletions cli/src/TestGenScript.elm
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ run =
-- Slimmed config for profiling
OpenApi.Config.init "./generated"
|> OpenApi.Config.withNoElmFormat True
|> OpenApi.Config.withKeepGoing True
|> OpenApi.Config.withInput additionalProperties
|> OpenApi.Config.withInput recursiveAllofRefs
|> OpenApi.Config.withInput overridingGlobalSecurity
Expand Down
2 changes: 1 addition & 1 deletion review/suppressed/Docs.NoMissing.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"automatically created by": "elm-review suppress",
"learn more": "elm-review suppress --help",
"suppressions": [
{ "count": 30, "filePath": "src/OpenApi/Config.elm" },
{ "count": 29, "filePath": "src/OpenApi/Config.elm" },
{ "count": 5, "filePath": "src/OpenApi/Generate.elm" }
]
}
30 changes: 21 additions & 9 deletions src/CliMonad.elm
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import OpenApi exposing (OpenApi)
import OpenApi.Config
import Pretty
import String.Extra
import Triple.Extra


type alias Message =
Expand Down Expand Up @@ -85,6 +86,7 @@ type alias Input =
, namespace : List String
, formats : FastDict.Dict InternalFormatName InternalFormat
, warnOnMissingEnums : Bool
, keepGoing : Bool
}


Expand Down Expand Up @@ -126,6 +128,7 @@ run :
, namespace : List String
, formats : List OpenApi.Config.Format
, warnOnMissingEnums : Bool
, keepGoing : Bool
}
-> CliMonad (List Declaration)
->
Expand All @@ -148,6 +151,7 @@ run oneOfDeclarations input (CliMonad x) =
|> List.map toInternalFormat
|> FastDict.fromList
, warnOnMissingEnums = input.warnOnMissingEnums
, keepGoing = input.keepGoing
}

res : Result Message ( List Declaration, Output, Cache )
Expand Down Expand Up @@ -570,20 +574,28 @@ getApiSpec =
CliMonad (\input cache -> Ok ( input.openApi, 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)
errorToWarning (CliMonad f) =
CliMonad
(\input cache ->
case f input cache of
Ok ( res, output, cache2 ) ->
Ok ( Just res, output, cache2 )
if input.keepGoing then
case f input cache of
Ok ( res, output, cache2 ) ->
Ok ( Just res, output, cache2 )

Err { path, message } ->
( Nothing
, { emptyOutput | warnings = [ { path = path, message = message, details = Pretty.empty } ] }
, cache
)
|> Ok
Err { path, message } ->
( Nothing
, { emptyOutput | warnings = [ { path = path, message = message, details = Pretty.empty } ] }
, cache
)
|> Ok

else
Result.map
(Triple.Extra.mapFirst Just)
(f input cache)
)


Expand Down
4 changes: 2 additions & 2 deletions src/Common.elm
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,7 @@ parseRequestBodyRef ref =
Ok (RefTo RequestBody res)

else
Err ("Expected a reference to a schema, found a reference to " ++ ref)
Err ("Expected a reference to a request body, found a reference to " ++ ref)
)


Expand All @@ -589,7 +589,7 @@ parseResponseRef ref =
Ok (RefTo Response res)

else
Err ("Expected a reference to a schema, found a reference to " ++ ref)
Err ("Expected a reference to a response, found a reference to " ++ ref)
)


Expand Down
47 changes: 40 additions & 7 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
, withAutoConvertSwagger, AutoConvertSwagger(..), withEffectTypes, withFormat, withFormats, withGenerateTodos, withInput, withSwaggerConversionCommand, withSwaggerConversionUrl, withNoElmFormat, withKeepGoing
, withOutputModuleName, withOverrides, withServer, withWriteMergedTo, withWarnOnMissingEnums
, autoConvertSwagger, inputs, outputDirectory, swaggerConversionCommand, swaggerConversionUrl, noElmFormat
, autoConvertSwagger, inputs, outputDirectory, swaggerConversionCommand, swaggerConversionUrl, noElmFormat, keepGoing
, 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
@docs withAutoConvertSwagger, AutoConvertSwagger, withEffectTypes, withFormat, withFormats, withGenerateTodos, withInput, withSwaggerConversionCommand, withSwaggerConversionUrl, withNoElmFormat, withKeepGoing
@docs withOutputModuleName, withOverrides, withServer, withWriteMergedTo, withWarnOnMissingEnums


# Config properties

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


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


Expand Down Expand Up @@ -208,7 +209,8 @@ type Path
| Url Url.Url -- https://petstore3.swagger.io/api/v3/openapi.json


{-| -}
{-| Builds a `Path`. If the file is a valid URL, it will be used as such, otherwise it will be interpreted as a local file path.
-}
pathFromString : String -> Path
pathFromString path =
case Url.fromString path of
Expand All @@ -230,7 +232,15 @@ pathToString pathType =
Url.toString url


{-| -}
{-| Create an empty `Config` with no inputs and a given output directory.

The default options are to:

- run elm-format;
- ask before converting Swagger specs to OpenAPI specs;
- fail rather than generating `Debug.todo` or skipping unimplemented paths.

-}
init : String -> Config
init initialOutputDirectory =
{ inputs = []
Expand All @@ -242,6 +252,7 @@ init initialOutputDirectory =
, staticFormats = defaultFormats
, dynamicFormats = \_ -> []
, noElmFormat = False
, keepGoing = False
}
|> Config

Expand All @@ -266,7 +277,16 @@ inputFrom path =
-------------


{-| -}
{-| The built-in formats:

- `date-time`
- `date`
- `uri`
- `uuid`
- `byte`
- `password`

-}
defaultFormats : List Format
defaultFormats =
[ dateTimeFormat
Expand Down Expand Up @@ -550,6 +570,11 @@ withNoElmFormat newNoElmFormat (Config config) =
Config { config | noElmFormat = newNoElmFormat }


withKeepGoing : Bool -> Config -> Config
withKeepGoing newKeepGoing (Config config) =
Config { config | keepGoing = newKeepGoing }



-------------
-- Getters --
Expand Down Expand Up @@ -592,6 +617,12 @@ noElmFormat (Config config) =
config.noElmFormat


{-| -}
keepGoing : Config -> Bool
keepGoing (Config config) =
config.keepGoing


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


Expand Down Expand Up @@ -657,6 +689,7 @@ toGenerationConfig formatsInput (Config config) augmentedInputs =
, server = input.server
, warnOnMissingEnums = input.warnOnMissingEnums
, formats = config.staticFormats ++ config.dynamicFormats formatsInput
, keepGoing = config.keepGoing
}
, 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 @@ -119,7 +119,7 @@ files :
, warnings : List Message
, requiredPackages : FastSet.Set String
}
files { namespace, generateTodos, effectTypes, server, formats, warnOnMissingEnums } apiSpec =
files { namespace, generateTodos, effectTypes, server, formats, warnOnMissingEnums, keepGoing } apiSpec =
case extractEnums apiSpec of
Err e ->
Err e
Expand Down Expand Up @@ -148,6 +148,7 @@ files { namespace, generateTodos, effectTypes, server, formats, warnOnMissingEnu
, namespace = namespace
, formats = formats
, warnOnMissingEnums = warnOnMissingEnums
, keepGoing = keepGoing
}
|> Result.map
(\{ declarations, warnings, requiredPackages } ->
Expand Down
2 changes: 2 additions & 0 deletions tests/Test/OpenApi/Generate.elm
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ fuzzTitle =
, server = OpenApi.Config.Default
, formats = OpenApi.Config.defaultFormats
, warnOnMissingEnums = True
, keepGoing = False
}
oas
in
Expand Down Expand Up @@ -257,6 +258,7 @@ pr267 =
, server = OpenApi.Config.Default
, formats = OpenApi.Config.defaultFormats
, warnOnMissingEnums = True
, keepGoing = False
}
oas
in
Expand Down