Skip to content

Commit 65d3e54

Browse files
authored
Merge pull request #289 from wolfadex/multipart-form-data
multipart/form-data
2 parents e9710e1 + 818f042 commit 65d3e54

6 files changed

Lines changed: 308 additions & 9 deletions

File tree

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
openapi: 3.0.0
2+
info:
3+
version: 1.0.0
4+
title: Multipart form data
5+
6+
paths:
7+
"/api":
8+
post:
9+
responses:
10+
200:
11+
description: Response
12+
requestBody:
13+
$ref: "#/components/requestBodies/Multipart"
14+
"/noMaybes":
15+
post:
16+
responses:
17+
200:
18+
description: Response
19+
requestBody:
20+
$ref: "#/components/requestBodies/MultipartNoMaybes"
21+
components:
22+
requestBodies:
23+
Multipart:
24+
# https://swagger.io/docs/specification/v3_0/describing-request-body/multipart-requests/
25+
content:
26+
multipart/form-data: # Media type
27+
schema: # Request payload
28+
type: object
29+
required:
30+
- id
31+
properties: # Request parts
32+
id: # Part 1 (string value)
33+
type: string
34+
format: uuid
35+
address: # Part2 (object)
36+
type: object
37+
properties:
38+
street:
39+
type: string
40+
city:
41+
type: string
42+
profileImage: # Part 3 (an image)
43+
type: string
44+
format: binary
45+
MultipartNoMaybes:
46+
# https://swagger.io/docs/specification/v3_0/describing-request-body/multipart-requests/
47+
content:
48+
multipart/form-data: # Media type
49+
schema: # Request payload
50+
type: object
51+
required:
52+
- id
53+
- address
54+
- profileImage
55+
properties: # Request parts
56+
id: # Part 1 (string value)
57+
type: string
58+
format: uuid
59+
address: # Part2 (object)
60+
type: object
61+
properties:
62+
street:
63+
type: string
64+
city:
65+
type: string
66+
profileImage: # Part 3 (an image)
67+
type: string
68+
format: binary

cli/example/src/Example.elm

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import GithubV3RestApi.Api
1111
import GithubV3RestApi.Types
1212
import MarioPartyStats.Api
1313
import MarioPartyStats.Types
14+
import MultipartFormData.Api
1415
import NullableEnum.Json
1516
import OpenApi.Common
1617
import PatreonApi.Api
@@ -45,6 +46,9 @@ init () =
4546
let
4647
_ =
4748
SimpleRef.Json.decodeForbidden
49+
50+
_ =
51+
MultipartFormData.Api.api
4852
in
4953
( {}
5054
, Cmd.batch

cli/src/TestGenScript.elm

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ run =
5151
marioPartyStats =
5252
OpenApi.Config.inputFrom (OpenApi.Config.File "./example/MarioPartyStats.json")
5353

54+
multipartFormData : OpenApi.Config.Input
55+
multipartFormData =
56+
OpenApi.Config.inputFrom (OpenApi.Config.File "./example/multipart-form-data.yaml")
57+
5458
nullableEnum : OpenApi.Config.Input
5559
nullableEnum =
5660
OpenApi.Config.inputFrom (OpenApi.Config.File "./example/nullable-enum.yaml")
@@ -136,6 +140,7 @@ run =
136140
|> OpenApi.Config.withInput cookieAuth
137141
|> OpenApi.Config.withInput ifconfigOvh
138142
|> OpenApi.Config.withInput marioPartyStats
143+
|> OpenApi.Config.withInput multipartFormData
139144
|> OpenApi.Config.withInput nullableEnum
140145
|> OpenApi.Config.withInput overridingGlobalSecurity
141146
|> OpenApi.Config.withInput realworldConduit

src/JsonSchema/Generate.elm

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module JsonSchema.Generate exposing (schemaToDeclarations)
1+
module JsonSchema.Generate exposing (multipartFormDataRequestBodyToDeclarations, schemaToDeclarations)
22

33
import CliMonad exposing (CliMonad)
44
import Common
@@ -16,6 +16,43 @@ import NonEmpty
1616
import SchemaUtils
1717

1818

19+
multipartFormDataRequestBodyToDeclarations : Common.UnsafeName -> Json.Schema.Definitions.Schema -> CliMonad (List CliMonad.Declaration)
20+
multipartFormDataRequestBodyToDeclarations name schema =
21+
SchemaUtils.schemaToType [] schema
22+
|> CliMonad.andThen
23+
(\{ type_, documentation } ->
24+
type_
25+
|> SchemaUtils.typeToAnnotationWithNullable
26+
|> CliMonad.map
27+
(\annotation ->
28+
let
29+
typeName : Common.TypeName
30+
typeName =
31+
Common.toTypeName name
32+
in
33+
if (Elm.ToString.annotation annotation).signature == typeName then
34+
[]
35+
36+
else
37+
[ { moduleName = Common.Types Common.RequestBody
38+
, name = typeName
39+
, declaration =
40+
Elm.alias typeName annotation
41+
|> (case documentation of
42+
Nothing ->
43+
identity
44+
45+
Just doc ->
46+
Elm.withDocumentation doc
47+
)
48+
|> Elm.expose
49+
, group = "Aliases"
50+
}
51+
]
52+
)
53+
)
54+
55+
1956
schemaToDeclarations : Common.Component -> Common.UnsafeName -> Json.Schema.Definitions.Schema -> CliMonad (List CliMonad.Declaration)
2057
schemaToDeclarations component name schema =
2158
SchemaUtils.schemaToType [] schema

0 commit comments

Comments
 (0)