Skip to content

Commit

Permalink
improve openapi (HapticX#367)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ethosa committed Oct 26, 2024
1 parent f33f606 commit 4bd026a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/happyx/ssr/request_models.nim
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ macro modelImplementation*(modelName: untyped, body: typed): untyped =
if typeImpl[2][i].len == 2:
modelFields[$name].add(newStmtList(typeImpl[2][i][0].toStrLit, typeImpl[2][i][1]))
else:
modelFields[$name].add(newStmtList(typeImpl[2][i][0].toStrLit, typeImpl[2][i][0].toStrLit))
modelFields[$name].add(newStmtList(typeImpl[2][i].toStrLit, typeImpl[2][i].toStrLit))
if argStr.toLower() != "formdataitem":
# JSON raw data
asgnStmt.add(newNimNode(nnkIfStmt).add(
Expand Down Expand Up @@ -346,6 +346,18 @@ macro modelImplementation*(modelName: untyped, body: typed): untyped =
postfix(argName, "*"), argType
))
modelFields[$modelName].add(newStmtList(argName.toStrLit, argType.toStrLit))
let argStr = $argType.toStrLit
if argStr.toLower notin builtinTypes:
let typeImpl = argType.getTypeImpl()[1].getImpl
if typeImpl[2].kind == nnkEnumTy:
let name = typeImpl[0].toStrLit
modelFields[$name] = newStmtList()
modelFieldsGenerics[$name] = newLit(true)
for i in 1..<typeImpl[2].len:
if typeImpl[2][i].len == 2:
modelFields[$name].add(newStmtList(typeImpl[2][i][0].toStrLit, typeImpl[2][i][1]))
else:
modelFields[$name].add(newStmtList(typeImpl[2][i].toStrLit, typeImpl[2][i].toStrLit))
if ($argType.toStrLit).toLower() != "formdataitem":
# JSON raw data
asgnStmt.add(newNimNode(nnkIfStmt).add(
Expand Down
12 changes: 12 additions & 0 deletions tests/testc15.nim
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ type
lNim = "nim",
lPython = "python",
lJavaScript = "javascript"
OptionsEncoding* = enum
encodingA, encodingB


model TestModel:
Expand All @@ -21,6 +23,12 @@ model UploadImage:
img: FormDataItem


model DataProcessRequest:
data: string = ""
storage: OptionsEncoding = encodingA
xtemplate: string = ""


mount Issue84:
get "/":
"Hello, world!"
Expand Down Expand Up @@ -66,3 +74,7 @@ serve "127.0.0.1", 5000:
get "/language/$lang?:enum(Language)":
## lang is lNim by default
return fmt"Hello from {lang}"

get "/language/[m:DataProcessRequest]":
## lang is lNim by default
return fmt"Hello from {m}"

0 comments on commit 4bd026a

Please sign in to comment.