Skip to content
Draft
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
68 changes: 68 additions & 0 deletions cli/example/multipart-form-data.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
openapi: 3.0.0
info:
version: 1.0.0
title: Multipart form data

paths:
"/api":
post:
responses:
200:
description: Response
requestBody:
$ref: "#/components/requestBodies/Multipart"
"/noMaybes":
post:
responses:
200:
description: Response
requestBody:
$ref: "#/components/requestBodies/MultipartNoMaybes"
components:
requestBodies:
Multipart:
# https://swagger.io/docs/specification/v3_0/describing-request-body/multipart-requests/
content:
multipart/form-data: # Media type
schema: # Request payload
type: object
required:
- id
properties: # Request parts
id: # Part 1 (string value)
type: string
format: uuid
address: # Part2 (object)
type: object
properties:
street:
type: string
city:
type: string
profileImage: # Part 3 (an image)
type: string
format: binary
MultipartNoMaybes:
# https://swagger.io/docs/specification/v3_0/describing-request-body/multipart-requests/
content:
multipart/form-data: # Media type
schema: # Request payload
type: object
required:
- id
- address
- profileImage
properties: # Request parts
id: # Part 1 (string value)
type: string
format: uuid
address: # Part2 (object)
type: object
properties:
street:
type: string
city:
type: string
profileImage: # Part 3 (an image)
type: string
format: binary
4 changes: 4 additions & 0 deletions cli/example/src/Example.elm
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import GithubV3RestApi.Api
import GithubV3RestApi.Types
import MarioPartyStats.Api
import MarioPartyStats.Types
import MultipartFormData.Api
import NullableEnum.Json
import OpenApi.Common
import PatreonApi.Api
Expand Down Expand Up @@ -45,6 +46,9 @@ init () =
let
_ =
SimpleRef.Json.decodeForbidden

_ =
MultipartFormData.Api.api
in
( {}
, Cmd.batch
Expand Down
5 changes: 5 additions & 0 deletions cli/src/TestGenScript.elm
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ run =
marioPartyStats =
OpenApi.Config.inputFrom (OpenApi.Config.File "./example/MarioPartyStats.json")

multipartFormData : OpenApi.Config.Input
multipartFormData =
OpenApi.Config.inputFrom (OpenApi.Config.File "./example/multipart-form-data.yaml")

nullableEnum : OpenApi.Config.Input
nullableEnum =
OpenApi.Config.inputFrom (OpenApi.Config.File "./example/nullable-enum.yaml")
Expand Down Expand Up @@ -136,6 +140,7 @@ run =
|> OpenApi.Config.withInput cookieAuth
|> OpenApi.Config.withInput ifconfigOvh
|> OpenApi.Config.withInput marioPartyStats
|> OpenApi.Config.withInput multipartFormData
|> OpenApi.Config.withInput nullableEnum
|> OpenApi.Config.withInput overridingGlobalSecurity
|> OpenApi.Config.withInput realworldConduit
Expand Down
39 changes: 38 additions & 1 deletion src/JsonSchema/Generate.elm
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module JsonSchema.Generate exposing (schemaToDeclarations)
module JsonSchema.Generate exposing (multipartFormDataRequestBodyToDeclarations, schemaToDeclarations)

import CliMonad exposing (CliMonad)
import Common
Expand All @@ -16,6 +16,43 @@ import NonEmpty
import SchemaUtils


multipartFormDataRequestBodyToDeclarations : Common.UnsafeName -> Json.Schema.Definitions.Schema -> CliMonad (List CliMonad.Declaration)
multipartFormDataRequestBodyToDeclarations name schema =
SchemaUtils.schemaToType [] schema
|> CliMonad.andThen
(\{ type_, documentation } ->
type_
|> SchemaUtils.typeToAnnotationWithNullable
|> CliMonad.map
(\annotation ->
let
typeName : Common.TypeName
typeName =
Common.toTypeName name
in
if (Elm.ToString.annotation annotation).signature == typeName then
[]

else
[ { moduleName = Common.Types Common.RequestBody
, name = typeName
, declaration =
Elm.alias typeName annotation
|> (case documentation of
Nothing ->
identity

Just doc ->
Elm.withDocumentation doc
)
|> Elm.expose
, group = "Aliases"
}
]
)
)


schemaToDeclarations : Common.Component -> Common.UnsafeName -> Json.Schema.Definitions.Schema -> CliMonad (List CliMonad.Declaration)
schemaToDeclarations component name schema =
SchemaUtils.schemaToType [] schema
Expand Down
Loading
Loading