Skip to content

Commit 0ae2aaa

Browse files
authored
Merge pull request #274 from wolfadex/dont-skip-paths
Don't skip paths by default
2 parents fc0e026 + 9b7f2d5 commit 0ae2aaa

11 files changed

Lines changed: 100 additions & 32 deletions

File tree

‎cli/example/nullable-enum.yaml‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@ info:
44
version: 1.0.0
55
paths:
66
"/":
7-
summary: Does stuff
8-
responses:
9-
200:
10-
description: Ok
11-
schema:
12-
"#ref": "#/components/schemas/NullableEnum"
7+
get:
8+
summary: Does stuff
9+
responses:
10+
200:
11+
description: Ok
12+
content:
13+
application/json:
14+
schema:
15+
"$ref": "#/components/schemas/NullableEnum"
1316
components:
1417
schemas:
1518
NullableEnum:

‎cli/example/overriding-global-security.yaml‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ components:
77
Data:
88
description: "Data"
99
type: string
10+
responses:
11+
Data:
12+
description: "Data"
13+
content:
14+
application/json:
15+
schema:
16+
$ref: "#/components/schemas/Data"
1017
securitySchemes:
1118
Token:
1219
type: apiKey
@@ -22,7 +29,7 @@ paths:
2229
operationId: GetProtectedData
2330
responses:
2431
200:
25-
$ref: "#/components/schemas/Data"
32+
$ref: "#/components/responses/Data"
2633

2734
"/api/unprotected":
2835
get:
@@ -32,7 +39,7 @@ paths:
3239
security: []
3340
responses:
3441
200:
35-
$ref: "#/components/schemas/Data"
42+
$ref: "#/components/responses/Data"
3643
post:
3744
summary: Unprotected endpoint to store data
3845
# Whoops! Security should be overridden here like for GET. Let's fix it
@@ -41,7 +48,7 @@ paths:
4148
operationId: PostUnprotectedData
4249
responses:
4350
200:
44-
$ref: "#/components/schemas/Data"
51+
$ref: "#/components/responses/Data"
4552
patch:
4653
summary: Unprotected endpoint to store data (again)
4754
# Whoops! This is supposed to be unprotected, like GET. Let's fix it
@@ -52,4 +59,4 @@ paths:
5259
operationId: PatchUnprotectedData
5360
responses:
5461
200:
55-
$ref: "#/components/schemas/Data"
62+
$ref: "#/components/responses/Data"

‎cli/example/recursive-allof-refs.yaml‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,13 @@ components:
3535
paths:
3636
"/api/grand-child":
3737
summary: Get a GrandChild object.
38-
description:
38+
description: Example description
3939
get:
4040
operationId: getGrandChild
4141
responses:
4242
200:
43-
$ref: "#/components/schemas/GrandChild"
43+
description: Success
44+
content:
45+
application/json:
46+
schema:
47+
$ref: "#/components/schemas/GrandChild"

‎cli/src/Cli.elm‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ type alias CliOptions =
5050
, overrides : List OpenApi.Config.Path
5151
, writeMergedTo : Maybe String
5252
, noElmFormat : Bool
53+
, keepGoing : Bool
5354
}
5455

5556

@@ -96,6 +97,8 @@ program =
9697
(Cli.Option.optionalKeywordArg "write-merged-to")
9798
|> Cli.OptionsParser.with
9899
(Cli.Option.flag "no-elm-format")
100+
|> Cli.OptionsParser.with
101+
(Cli.Option.flag "keep-going")
99102
|> Cli.OptionsParser.withDoc
100103
([ ""
101104
, """version: 0.7.0"""
@@ -166,6 +169,7 @@ program =
166169
, formatOption "overrides" [ "Load an additional file to override parts of the original Open API file." ]
167170
, formatOption "write-merged-to" [ "Write the merged Open API spec to the given file." ]
168171
, formatOption "no-elm-format" [ "Don't run elm-format on the outputs." ]
172+
, formatOption "keep-going" [ "If a route can't be generated, skip it instead of erroring out." ]
169173
]
170174
|> String.join "\n\n"
171175
)
@@ -370,6 +374,7 @@ parseCliOptions cliOptions =
370374
|> OpenApi.Config.withGenerateTodos cliOptions.generateTodos
371375
|> OpenApi.Config.withAutoConvertSwagger cliOptions.autoConvertSwagger
372376
|> OpenApi.Config.withNoElmFormat cliOptions.noElmFormat
377+
|> OpenApi.Config.withKeepGoing cliOptions.keepGoing
373378
|> maybe OpenApi.Config.withSwaggerConversionUrl cliOptions.swaggerConversionUrl
374379
|> maybe OpenApi.Config.withSwaggerConversionCommand
375380
(cliOptions.swaggerConversionCommand

‎cli/src/TestGenScript.elm‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ run =
119119
-- Slimmed config for profiling
120120
OpenApi.Config.init "./generated"
121121
|> OpenApi.Config.withNoElmFormat True
122+
|> OpenApi.Config.withKeepGoing True
122123
|> OpenApi.Config.withInput additionalProperties
123124
|> OpenApi.Config.withInput recursiveAllofRefs
124125
|> OpenApi.Config.withInput overridingGlobalSecurity

‎review/suppressed/Docs.NoMissing.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"automatically created by": "elm-review suppress",
44
"learn more": "elm-review suppress --help",
55
"suppressions": [
6-
{ "count": 30, "filePath": "src/OpenApi/Config.elm" },
6+
{ "count": 29, "filePath": "src/OpenApi/Config.elm" },
77
{ "count": 5, "filePath": "src/OpenApi/Generate.elm" }
88
]
99
}

‎src/CliMonad.elm‎

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import OpenApi exposing (OpenApi)
4444
import OpenApi.Config
4545
import Pretty
4646
import String.Extra
47+
import Triple.Extra
4748

4849

4950
type alias Message =
@@ -85,6 +86,7 @@ type alias Input =
8586
, namespace : List String
8687
, formats : FastDict.Dict InternalFormatName InternalFormat
8788
, warnOnMissingEnums : Bool
89+
, keepGoing : Bool
8890
}
8991

9092

@@ -126,6 +128,7 @@ run :
126128
, namespace : List String
127129
, formats : List OpenApi.Config.Format
128130
, warnOnMissingEnums : Bool
131+
, keepGoing : Bool
129132
}
130133
-> CliMonad (List Declaration)
131134
->
@@ -148,6 +151,7 @@ run oneOfDeclarations input (CliMonad x) =
148151
|> List.map toInternalFormat
149152
|> FastDict.fromList
150153
, warnOnMissingEnums = input.warnOnMissingEnums
154+
, keepGoing = input.keepGoing
151155
}
152156

153157
res : Result Message ( List Declaration, Output, Cache )
@@ -570,20 +574,28 @@ getApiSpec =
570574
CliMonad (\input cache -> Ok ( input.openApi, emptyOutput, cache ))
571575

572576

577+
{-| 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
578+
-}
573579
errorToWarning : CliMonad a -> CliMonad (Maybe a)
574580
errorToWarning (CliMonad f) =
575581
CliMonad
576582
(\input cache ->
577-
case f input cache of
578-
Ok ( res, output, cache2 ) ->
579-
Ok ( Just res, output, cache2 )
583+
if input.keepGoing then
584+
case f input cache of
585+
Ok ( res, output, cache2 ) ->
586+
Ok ( Just res, output, cache2 )
580587

581-
Err { path, message } ->
582-
( Nothing
583-
, { emptyOutput | warnings = [ { path = path, message = message, details = Pretty.empty } ] }
584-
, cache
585-
)
586-
|> Ok
588+
Err { path, message } ->
589+
( Nothing
590+
, { emptyOutput | warnings = [ { path = path, message = message, details = Pretty.empty } ] }
591+
, cache
592+
)
593+
|> Ok
594+
595+
else
596+
Result.map
597+
(Triple.Extra.mapFirst Just)
598+
(f input cache)
587599
)
588600

589601

‎src/Common.elm‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,7 @@ parseRequestBodyRef ref =
576576
Ok (RefTo RequestBody res)
577577

578578
else
579-
Err ("Expected a reference to a schema, found a reference to " ++ ref)
579+
Err ("Expected a reference to a request body, found a reference to " ++ ref)
580580
)
581581

582582

@@ -589,7 +589,7 @@ parseResponseRef ref =
589589
Ok (RefTo Response res)
590590

591591
else
592-
Err ("Expected a reference to a schema, found a reference to " ++ ref)
592+
Err ("Expected a reference to a response, found a reference to " ++ ref)
593593
)
594594

595595

‎src/OpenApi/Config.elm‎

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
module OpenApi.Config exposing
22
( Config, EffectType(..), effectTypeToPackage, Format, Input, Path(..), Server(..)
33
, init, inputFrom, pathFromString
4-
, withAutoConvertSwagger, AutoConvertSwagger(..), withEffectTypes, withFormat, withFormats, withGenerateTodos, withInput, withSwaggerConversionCommand, withSwaggerConversionUrl, withNoElmFormat
4+
, withAutoConvertSwagger, AutoConvertSwagger(..), withEffectTypes, withFormat, withFormats, withGenerateTodos, withInput, withSwaggerConversionCommand, withSwaggerConversionUrl, withNoElmFormat, withKeepGoing
55
, withOutputModuleName, withOverrides, withServer, withWriteMergedTo, withWarnOnMissingEnums
6-
, autoConvertSwagger, inputs, outputDirectory, swaggerConversionCommand, swaggerConversionUrl, noElmFormat
6+
, autoConvertSwagger, inputs, outputDirectory, swaggerConversionCommand, swaggerConversionUrl, noElmFormat, keepGoing
77
, oasPath, overrides, writeMergedTo
88
, toGenerationConfig, Generate, pathToString
99
, defaultFormats
@@ -20,13 +20,13 @@ module OpenApi.Config exposing
2020
# Creation
2121
2222
@docs init, inputFrom, pathFromString
23-
@docs withAutoConvertSwagger, AutoConvertSwagger, withEffectTypes, withFormat, withFormats, withGenerateTodos, withInput, withSwaggerConversionCommand, withSwaggerConversionUrl, withNoElmFormat
23+
@docs withAutoConvertSwagger, AutoConvertSwagger, withEffectTypes, withFormat, withFormats, withGenerateTodos, withInput, withSwaggerConversionCommand, withSwaggerConversionUrl, withNoElmFormat, withKeepGoing
2424
@docs withOutputModuleName, withOverrides, withServer, withWriteMergedTo, withWarnOnMissingEnums
2525
2626
2727
# Config properties
2828
29-
@docs autoConvertSwagger, inputs, outputDirectory, swaggerConversionCommand, swaggerConversionUrl, noElmFormat
29+
@docs autoConvertSwagger, inputs, outputDirectory, swaggerConversionCommand, swaggerConversionUrl, noElmFormat, keepGoing
3030
3131
3232
# Input properties
@@ -83,6 +83,7 @@ type Config
8383
, staticFormats : List Format
8484
, dynamicFormats : List { format : String, basicType : Common.BasicType } -> List Format
8585
, noElmFormat : Bool
86+
, keepGoing : Bool
8687
}
8788

8889

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

210211

211-
{-| -}
212+
{-| 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.
213+
-}
212214
pathFromString : String -> Path
213215
pathFromString path =
214216
case Url.fromString path of
@@ -230,7 +232,15 @@ pathToString pathType =
230232
Url.toString url
231233

232234

233-
{-| -}
235+
{-| Create an empty `Config` with no inputs and a given output directory.
236+
237+
The default options are to:
238+
239+
- run elm-format;
240+
- ask before converting Swagger specs to OpenAPI specs;
241+
- fail rather than generating `Debug.todo` or skipping unimplemented paths.
242+
243+
-}
234244
init : String -> Config
235245
init initialOutputDirectory =
236246
{ inputs = []
@@ -242,6 +252,7 @@ init initialOutputDirectory =
242252
, staticFormats = defaultFormats
243253
, dynamicFormats = \_ -> []
244254
, noElmFormat = False
255+
, keepGoing = False
245256
}
246257
|> Config
247258

@@ -266,7 +277,16 @@ inputFrom path =
266277
-------------
267278

268279

269-
{-| -}
280+
{-| The built-in formats:
281+
282+
- `date-time`
283+
- `date`
284+
- `uri`
285+
- `uuid`
286+
- `byte`
287+
- `password`
288+
289+
-}
270290
defaultFormats : List Format
271291
defaultFormats =
272292
[ dateTimeFormat
@@ -550,6 +570,11 @@ withNoElmFormat newNoElmFormat (Config config) =
550570
Config { config | noElmFormat = newNoElmFormat }
551571

552572

573+
withKeepGoing : Bool -> Config -> Config
574+
withKeepGoing newKeepGoing (Config config) =
575+
Config { config | keepGoing = newKeepGoing }
576+
577+
553578

554579
-------------
555580
-- Getters --
@@ -592,6 +617,12 @@ noElmFormat (Config config) =
592617
config.noElmFormat
593618

594619

620+
{-| -}
621+
keepGoing : Config -> Bool
622+
keepGoing (Config config) =
623+
config.keepGoing
624+
625+
595626
{-| -}
596627
oasPath : Input -> Path
597628
oasPath (Input input) =
@@ -624,6 +655,7 @@ type alias Generate =
624655
, server : Server
625656
, formats : List Format
626657
, warnOnMissingEnums : Bool
658+
, keepGoing : Bool
627659
}
628660

629661

@@ -657,6 +689,7 @@ toGenerationConfig formatsInput (Config config) augmentedInputs =
657689
, server = input.server
658690
, warnOnMissingEnums = input.warnOnMissingEnums
659691
, formats = config.staticFormats ++ config.dynamicFormats formatsInput
692+
, keepGoing = config.keepGoing
660693
}
661694
, spec
662695
)

‎src/OpenApi/Generate.elm‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ files :
119119
, warnings : List Message
120120
, requiredPackages : FastSet.Set String
121121
}
122-
files { namespace, generateTodos, effectTypes, server, formats, warnOnMissingEnums } apiSpec =
122+
files { namespace, generateTodos, effectTypes, server, formats, warnOnMissingEnums, keepGoing } apiSpec =
123123
case extractEnums apiSpec of
124124
Err e ->
125125
Err e
@@ -148,6 +148,7 @@ files { namespace, generateTodos, effectTypes, server, formats, warnOnMissingEnu
148148
, namespace = namespace
149149
, formats = formats
150150
, warnOnMissingEnums = warnOnMissingEnums
151+
, keepGoing = keepGoing
151152
}
152153
|> Result.map
153154
(\{ declarations, warnings, requiredPackages } ->

0 commit comments

Comments
 (0)