Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support weird cookie trick #30

Merged
merged 1 commit into from
Jun 21, 2022
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
2 changes: 1 addition & 1 deletion src/Tie/Codegen/Operation.hs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ codegenOperations resolver operations = do
)
)

inlineablePragma =
inlineablePragma =
"{-#" <+> "INLINABLE" <+> "application" <+> "#-}"

pure (dataApiDecl <> PP.line <> PP.line <> apiDecl <> PP.line <> inlineablePragma)
Expand Down
19 changes: 12 additions & 7 deletions src/Tie/Name.hs
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,14 @@ toFieldName :: Name -> PP.Doc ann
toFieldName =
PP.pretty . Text.pack . escapeKeyword . lowerFirstLetter . toCamelCase . Text.unpack . unName

-- | Returns the name as written, should be used within quotes only.
toParamName :: Name -> PP.Doc ann
toParamName =
PP.pretty . unName
PP.pretty . filterNUL . unName
where
-- Filter away '\0' to support the weird cookie trick
-- (see test/golden/weird-cookie-trick.yaml)
filterNUL = Text.filter (/= '\0')

toParamBinder :: Name -> PP.Doc ann
toParamBinder =
Expand Down Expand Up @@ -262,12 +267,12 @@ escapeKeyword input = haskelify $ case input of
_ -> input

haskelify :: String -> String
haskelify xs =
[ case x of
'-' -> '_'
_ -> x
| x <- xs
]
haskelify = concatMap escape
where
escape c = case c of
'-' -> "_"
'\0' -> "NUL"
_ -> [c]

toCamelCase :: String -> String
toCamelCase input =
Expand Down
32 changes: 32 additions & 0 deletions test/golden/weird-cookie-trick.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
openapi: "3.0.0"
info:
version: 1.0.0
title: Scarf
license:
name: AllRightsReserved
servers:
- url: https://scarf.sh/api/v1
paths:
/test:
get:
summary: test
operationId: test
responses:
'200':
description: Some response
content:
application/json:
schema:
description: Undocumented
headers:
Set-Cookie:
description: Session cookie
schema:
type: string
example: SESSIONID=abcde12345; Path=/
"\0Set-Cookie":
description: CSRF token
schema:
type: string
example: CSRFTOKEN=fghijk678910; Path=/; HttpOnly
components: {}
Loading