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
29 changes: 29 additions & 0 deletions cli/example/simple-ref.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
openapi: 3.0.1
info:
title: "Simple Ref"
version: 1.0.0
components:
schemas:
Forbidden:
$ref: "#/components/schemas/Message"
Message:
type: object
properties:
msg:
type: string
required:
- msg

responses:
Forbidden:
description: Wrong credentials
content:
application/json:
schema:
$ref: "#/components/schemas/Forbidden"
paths:
"/api":
get:
responses:
200:
$ref: "#/components/responses/Forbidden"
31 changes: 24 additions & 7 deletions cli/example/src/Example.elm
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Example exposing (..)

import AdditionalProperties.Types
import AirlineCodeLookupApi.Api
import AirlineCodeLookupApi.Types
import AirlineCodeLookupApi.Types.Responses
import Browser
import DbFahrplanApi.Api
import DbFahrplanApi.Types
Expand All @@ -14,11 +14,13 @@ import MarioPartyStats.Types
import NullableEnum.Json
import OpenApi.Common
import PatreonApi.Api
import PatreonApi.Types
import PatreonApi.Types.Responses
import RealworldConduitApi.Api
import RealworldConduitApi.Types
import RealworldConduitApi.Types.Responses
import RecursiveAllofRefs.Types
import SimpleRef.Json
import SingleEnum.Types
import Trustmark.Api
import Trustmark.TradeCheck.Api
import Trustmark.TradeCheck.Servers
import Trustmark.TradeCheck.Types
Expand All @@ -40,6 +42,10 @@ type alias Model =

init : () -> ( Model, Cmd Msg )
init () =
let
_ =
SimpleRef.Json.decodeForbidden
in
( {}
, Cmd.batch
[ RealworldConduitApi.Api.getArticle
Expand Down Expand Up @@ -85,6 +91,13 @@ init () =
{ server = Trustmark.TradeCheck.Servers.sandbox
, authorization = { x_api_key = "?" }
, body = { publicId = 0, schemeId = Nothing }
, toMsg = TrustmarkTradeCheckResponse
}
, Trustmark.Api.taxonomiesDocumentTypes
{ authorization =
{ tm_api_key = "?"
, x_api_key = "?"
}
, toMsg = TrustmarkResponse
}
, PatreonApi.Api.getCampaign
Expand All @@ -109,14 +122,15 @@ subscriptions _ =


type Msg
= ConduitResponse (Result (OpenApi.Common.Error RealworldConduitApi.Types.GenericError String) RealworldConduitApi.Types.SingleArticleResponse)
| AmadeusResponse (Result (OpenApi.Common.Error AirlineCodeLookupApi.Types.Getairlines_Error String) AirlineCodeLookupApi.Types.Airlines)
= ConduitResponse (Result (OpenApi.Common.Error RealworldConduitApi.Types.Responses.GenericError String) RealworldConduitApi.Types.Responses.SingleArticleResponse)
| AmadeusResponse (Result (OpenApi.Common.Error AirlineCodeLookupApi.Types.Responses.Getairlines_Error String) AirlineCodeLookupApi.Types.Responses.Airlines)
-- | BimResponse (Result (OpenApi.Common.Error BimcloudApi20232AlphaRelease.BlobStoreService10BeginBatchUpload_Error Bytes.Bytes) Bytes.Bytes)
| GithubResponse (Result (OpenApi.Common.Error () String) GithubV3RestApi.Types.Root)
| DbFahrplanResponse (Result (OpenApi.Common.Error Never String) DbFahrplanApi.Types.LocationResponse)
| MarioPartyStatsResponse (Result (OpenApi.Common.Error Never String) (List MarioPartyStats.Types.Boards))
| TrustmarkResponse (Result (OpenApi.Common.Error Never String) Trustmark.TradeCheck.Types.TradeCheckResponse)
| PatreonResponse (Result (OpenApi.Common.Error PatreonApi.Types.GetCampaign_Error String) PatreonApi.Types.CampaignResponse)
| TrustmarkTradeCheckResponse (Result (OpenApi.Common.Error Never String) Trustmark.TradeCheck.Types.TradeCheckResponse)
| PatreonResponse (Result (OpenApi.Common.Error PatreonApi.Types.Responses.GetCampaign_Error String) PatreonApi.Types.Responses.CampaignResponse)
| TrustmarkResponse (Result (OpenApi.Common.Error Never String) (List String))


update : Msg -> Model -> ( Model, Cmd Msg )
Expand All @@ -143,6 +157,9 @@ update msg model =
PatreonResponse _ ->
( model, Cmd.none )

TrustmarkTradeCheckResponse _ ->
( model, Cmd.none )


view : Model -> Browser.Document Msg
view _ =
Expand Down
5 changes: 5 additions & 0 deletions cli/src/TestGenScript.elm
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ run =
telegramBot =
OpenApi.Config.inputFrom (OpenApi.Config.File "./example/telegram-bot.json")

simpleRef : OpenApi.Config.Input
simpleRef =
OpenApi.Config.inputFrom (OpenApi.Config.File "./example/simple-ref.yaml")

bug : Int -> OpenApi.Config.Input
bug n =
OpenApi.Config.inputFrom (OpenApi.Config.File ("./example/openapi-generator-bugs/" ++ String.fromInt n ++ ".yaml"))
Expand All @@ -130,6 +134,7 @@ run =
|> OpenApi.Config.withInput binaryResponse
|> OpenApi.Config.withInput nullableEnum
|> OpenApi.Config.withInput cookieAuth
|> OpenApi.Config.withInput simpleRef
|> OpenApi.Config.withInput (bug 7889)
|> OpenApi.Config.withInput (bug 10398)
|> OpenApi.Config.withInput (bug 16104)
Expand Down
8 changes: 6 additions & 2 deletions elm.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@
"wolfadex/elm-open-api": "2.0.0 <= v < 3.0.0"
},
"test-dependencies": {
"elm-explorations/test": "2.2.0 <= v < 3.0.0",
"miniBill/elm-unicode": "1.1.1 <= v < 2.0.0"
"arowM/elm-multiline-string": "1.0.0 <= v < 2.0.0",
"elm-explorations/test": "2.2.1 <= v < 3.0.0",
"miniBill/elm-diff": "1.1.1 <= v < 2.0.0",
"miniBill/elm-unicode": "1.1.1 <= v < 2.0.0",
"wolfadex/elm-ansi": "3.0.1 <= v < 4.0.0",
"MaybeJustJames/yaml": "2.1.7 <= v < 3.0.0"
}
}
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"elm-optimize-level-2": "0.3.5",
"elm-pages": "^3.0.28",
"elm-review": "^2.13.5",
"elm-test": "^0.19.1-revision16",
"elm-test": "^0.19.1-revision17",
"vite": "^6.0.3"
},
"volta": {
Expand Down
3 changes: 1 addition & 2 deletions review/suppressed/NoMissingTypeExpose.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"automatically created by": "elm-review suppress",
"learn more": "elm-review suppress --help",
"suppressions": [
{ "count": 4, "filePath": "src/OpenApi/Config.elm" },
{ "count": 1, "filePath": "src/OpenApi/Generate.elm" }
{ "count": 4, "filePath": "src/OpenApi/Config.elm" }
]
}
116 changes: 101 additions & 15 deletions src/CliMonad.elm
Original file line number Diff line number Diff line change
@@ -1,27 +1,33 @@
module CliMonad exposing
( CliMonad, Message, OneOfName, Path, Declaration
, run, stepOrFail
, succeed, succeedWith, fail
, succeed, succeedWith, fail, fromResult
, map, map2, map3
, andThen, andThen2, andThen3, andThen4, combine, combineDict, combineMap, foldl
, errorToWarning, getApiSpec, enumName, moduleToNamespace, getOrCache
, withPath, withWarning, withExtendedWarning, withRequiredPackage
, todo, todoWithDefault
, withFormat
, nameToAnnotation, refToAnnotation, refToEncoder, refToDecoder
)

{-|

@docs CliMonad, Message, OneOfName, Path, Declaration
@docs run, stepOrFail
@docs succeed, succeedWith, fail
@docs succeed, succeedWith, fail, fromResult
@docs map, map2, map3
@docs andThen, andThen2, andThen3, andThen4, combine, combineDict, combineMap, foldl
@docs errorToWarning, getApiSpec, enumName, moduleToNamespace, getOrCache
@docs withPath, withWarning, withExtendedWarning, withRequiredPackage
@docs todo, todoWithDefault
@docs withFormat


## Utils

@docs nameToAnnotation, refToAnnotation, refToEncoder, refToDecoder

-}

import Common
Expand Down Expand Up @@ -83,11 +89,11 @@ type alias Input =


type CliMonad a
= CliMonad
(Input
-> FastDict.Dict (List String) Common.Type
-> Result Message ( a, Output, FastDict.Dict (List String) Common.Type )
)
= CliMonad (Input -> Cache -> Result Message ( a, Output, Cache ))


type alias Cache =
FastDict.Dict String Common.Type


type alias Output =
Expand Down Expand Up @@ -144,9 +150,9 @@ run oneOfDeclarations input (CliMonad x) =
, warnOnMissingEnums = input.warnOnMissingEnums
}

res : Result Message ( List Declaration, Output, FastDict.Dict (List String) Common.Type )
res : Result Message ( List Declaration, Output, Cache )
res =
x internalInput FastDict.empty
x internalInput emptyCache
in
res
|> Result.andThen
Expand Down Expand Up @@ -182,8 +188,17 @@ run oneOfDeclarations input (CliMonad x) =
)


getOrCache : List String -> (() -> CliMonad Common.Type) -> CliMonad Common.Type
getOrCache key compute =
emptyCache : Cache
emptyCache =
FastDict.empty


getOrCache : Common.RefTo Common.Schema -> (() -> CliMonad Common.Type) -> CliMonad Common.Type
getOrCache ref compute =
let
(Common.UnsafeName key) =
Common.refToString ref
in
CliMonad
(\input cache ->
case FastDict.get key cache of
Expand Down Expand Up @@ -499,8 +514,8 @@ combineMap f ls =
List a
-> List b
-> Output
-> FastDict.Dict (List String) Common.Type
-> Result Message ( List b, Output, FastDict.Dict (List String) Common.Type )
-> Cache
-> Result Message ( List b, Output, Cache )
go queue acc output accCache =
case queue of
[] ->
Expand Down Expand Up @@ -531,8 +546,8 @@ combine ls =
List (CliMonad a)
-> List a
-> Output
-> FastDict.Dict (List String) Common.Type
-> Result Message ( List a, Output, FastDict.Dict (List String) Common.Type )
-> Cache
-> Result Message ( List a, Output, Cache )
go queue acc output accCache =
case queue of
[] ->
Expand Down Expand Up @@ -862,3 +877,74 @@ withRequiredPackage package (CliMonad f) =
)
(f input cache)
)


fromResult : Result String a -> CliMonad a
fromResult res =
case res of
Ok o ->
succeed o

Err e ->
fail e


refToDecoder : Common.RefTo component -> CliMonad Elm.Expression
refToDecoder ref =
let
( component, name ) =
Common.unwrapRef ref
in
map2
(\importFrom ann ->
Elm.value
{ importFrom = importFrom
, name = "decode" ++ Common.toTypeName name
, annotation = Just (Gen.Json.Decode.annotation_.decoder ann)
}
)
(moduleToNamespace (Common.Json component))
(refToAnnotation ref)


refToEncoder : Common.RefTo component -> CliMonad (Elm.Expression -> Elm.Expression)
refToEncoder ref =
let
( component, name ) =
Common.unwrapRef ref
in
map2
(\importFrom ann rec ->
Elm.apply
(Elm.value
{ importFrom = importFrom
, name = "encode" ++ Common.toTypeName name
, annotation = Just (Elm.Annotation.function [ ann ] Gen.Json.Encode.annotation_.value)
}
)
[ rec ]
)
(moduleToNamespace (Common.Json component))
(refToAnnotation ref)
|> withPath (Common.refToString ref)


refToAnnotation : Common.RefTo schema -> CliMonad Elm.Annotation.Annotation
refToAnnotation ref =
let
( component, name ) =
Common.unwrapRef ref
in
nameToAnnotation component name
|> withPath (Common.refToString ref)


nameToAnnotation : Common.Component -> Common.UnsafeName -> CliMonad Elm.Annotation.Annotation
nameToAnnotation component name =
moduleToNamespace (Common.Types component)
|> map
(\importFrom ->
Elm.Annotation.named
importFrom
(Common.toTypeName name)
)
Loading