added support of xml (#42) · Szer/GiraffeGenerator@f488a4a · GitHub
Skip to content

Commit f488a4a

Browse files
authored
added support of xml (#42)
added support of xml
1 parent bb1a366 commit f488a4a

8 files changed

Lines changed: 175 additions & 7 deletions

File tree

‎.github/workflows/dotnetcore.yml‎

Lines changed: 2 additions & 2 deletions

‎CHANGELOG.md‎

118 Bytes

Unreleased

Added

  • Added support of XML requests and responses

0.4.0 - 2020-11-13

Added

  • Body bindings
  • NodaTime support
  • Validation of body/query/path parameters

Added

  • Path and query bindings
  • Emit of default handlers

Added

  • Record generation from schemas
  • Multiple responses for one path now supported
  • Added support for XML docs generation

Added

  • Initial release

‎Directory.build.props‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<PropertyGroup>
44
<RepoRootDir>$([System.IO.Path]::GetFullPath("$(MSBuildThisFileDirectory)"))</RepoRootDir>
55
<Version>0.4.0</Version>
6+
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
67
</PropertyGroup>
78

89
<PropertyGroup>

‎src/GiraffeGenerator.Core/CodeGen.fs‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,9 @@ let giraffeAst (api: Api) =
637637
match contentType with
638638
| MediaType.Form -> "ctx.BindFormAsync"
639639
| MediaType.Json -> "ctx.BindJsonAsync"
640-
| v -> failwithf "Content type %A is not supported" v
640+
| MediaType.Xml -> "ctx.BindXmlAsync"
641+
| MediaType.NotSpecified -> failwithf "You must specify media type!"
642+
| MediaType.Other ct -> failwithf "Content type %A is not supported" ct
641643
|> longIdentExpr
642644
// add type parameter to the chosen method
643645
let typeApp =
@@ -909,10 +911,12 @@ let giraffeAst (api: Api) =
909911
let contentHandler =
910912
// currently everything goes to json
911913
match response.MediaType with
914+
| Xml -> "xml"
912915
| Json
913-
| NotSpecified -> app (identExpr "json" ) (identExpr inputBinding)
914-
| Form -> app (identExpr "form" ) (identExpr inputBinding)
915-
| Other x -> failwithf "Emitting of code for media type %s currently not supported" x
916+
| NotSpecified -> "json"
917+
| Form -> "form"
918+
| Other x -> failwithf "Code emitting for media type %s currently not supported" x
919+
|> fun mediaType -> app (identExpr mediaType) (identExpr inputBinding)
916920

917921
if response.Code = 200
918922
then

‎src/GiraffeGenerator.Core/OpenApi.fs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@ and TypeSchema =
339339
/// Supported response media types
340340
type MediaType =
341341
| Json
342+
| Xml
342343
| Form
343344
| NotSpecified
344345
| Other of string
@@ -400,7 +401,8 @@ let inline private parseCode x =
400401

401402
let inline private parseMediaType x =
402403
match trimLower x with
403-
| Some "application/json" -> Json
404+
| Some ("application/json" | "text/json") -> Json
405+
| Some ("application/xml" | "text/xml")-> Xml
404406
| Some "application/x-www-form-urlencoded" -> MediaType.Form
405407
| Some x -> Other x
406408
| None -> failwith "Media type can't be null!"

‎tests/GiraffeGenerator.IntegrationTests/GiraffeGenerator.IntegrationTests.fsproj‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
<Compile Include="SpecWithSchemas.fs">
1414
<OpenApiSpecFile>specs/specWithSchemas.yaml</OpenApiSpecFile>
1515
</Compile>
16+
<Compile Include="SpecWithTextXml.fs">
17+
<OpenApiSpecFile>specs/specWithTextXml.yaml</OpenApiSpecFile>
18+
</Compile>
1619
<Compile Include="SpecWithArguments.fs">
1720
<OpenApiSpecFile>specs/specWithArguments.yaml</OpenApiSpecFile>
1821
</Compile>
@@ -64,6 +67,7 @@
6467
<None Include="NuGet.Config" />
6568
<Compile Include="SpecSimpleTests.fs" />
6669
<Compile Include="SpecWithSchemasTests.fs" />
70+
<Compile Include="SpecWithTextXmlTests.fs" />
6771
<Compile Include="SpecWithArgumentsTests.fs" />
6872
<Compile Include="SpecWithParametersAndRequestBodyTests.fs" />
6973
<Compile Include="SpecGeneralForNodaTimeTests.fs" />
@@ -72,6 +76,7 @@
7276
<Compile Include="SpecWithValidationExtensibilityTests.fs" />
7377
<Content Include="RemoveGenerated.ps1" />
7478
<Content Include="specs\specWithSchemas.yaml" />
79+
<Content Include="specs\specWithTextXml.yaml" />
7580
<Content Include="specs\specWithParametersAndRequestBodySchemas.yaml" />
7681
<Content Include="specs\specGeneralForNodaTime.yaml" />
7782
<Content Include="specs\specForNodaTimeDateTimeFormatHandling.yaml" />
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
namespace GiraffeGenerator.IntegrationTests
2+
3+
open System.Net
4+
open System.Net.Http
5+
open System.Threading.Tasks
6+
open FSharp.Control.Tasks.V2.ContextInsensitive
7+
open System
8+
open Giraffe
9+
open Microsoft.AspNetCore
10+
open Microsoft.AspNetCore.Builder
11+
open Microsoft.AspNetCore.Hosting
12+
open Microsoft.AspNetCore.TestHost
13+
open Microsoft.Extensions.DependencyInjection
14+
open Microsoft.Extensions.Logging
15+
open Xunit
16+
17+
type SpecWithTextXmlTests() =
18+
19+
let service =
20+
{ new SpecwithxmlschemasAPI.Service() with
21+
member _.PostXmlInput ((i,_)) = Task.FromResult i
22+
member _.ApplicationJsonInput _ = Task.FromResult "1"
23+
member _.ApplicationXmlInput _ = Task.FromResult "2"
24+
member _.TextJsonInput _ = Task.FromResult "3"
25+
member _.TextXmlInput _ = Task.FromResult "4" }
26+
27+
let configureApp (app : IApplicationBuilder) =
28+
app.UseGiraffe SpecwithxmlschemasAPI.webApp
29+
30+
let configureServices (services : IServiceCollection) =
31+
services
32+
.AddGiraffe()
33+
.AddSingleton(service)
34+
|> ignore
35+
36+
let configureLogging (loggerBuilder : ILoggingBuilder) =
37+
loggerBuilder.AddFilter(fun lvl -> lvl.Equals LogLevel.Error)
38+
.AddConsole()
39+
.AddDebug() |> ignore
40+
41+
let webHostBuilder =
42+
WebHost.CreateDefaultBuilder()
43+
.Configure(Action<IApplicationBuilder> configureApp)
44+
.ConfigureServices(configureServices)
45+
.ConfigureLogging(configureLogging)
46+
47+
let server = new TestServer(webHostBuilder)
48+
let client = server.CreateClient()
49+
50+
[<Fact>]
51+
let ``GET /applicationJson -> OK "1"``() = task {
52+
let! response = client.GetAsync("/applicationJson")
53+
let! text = response.Content.ReadAsStringAsync()
54+
Assert.Equal("\"1\"",text)
55+
Assert.Equal(HttpStatusCode.OK ,response.StatusCode)
56+
}
57+
58+
[<Fact>]
59+
let ``GET /applicationXml -> OK "2"``() = task {
60+
let! response = client.GetAsync("/applicationXml")
61+
let! text = response.Content.ReadAsStringAsync()
62+
Assert.Equal("""<?xml version="1.0" encoding="utf-8"?>
63+
<string>2</string>""",text)
64+
Assert.Equal(HttpStatusCode.OK ,response.StatusCode)
65+
}
66+
67+
[<Fact>]
68+
let ``GET /textJson -> OK "3"``() = task {
69+
let! response = client.GetAsync("/textJson")
70+
let! text = response.Content.ReadAsStringAsync()
71+
Assert.Equal("\"3\"",text)
72+
Assert.Equal(HttpStatusCode.OK ,response.StatusCode)
73+
}
74+
75+
[<Fact>]
76+
let ``GET /textXml -> OK "4"``() = task {
77+
let! response = client.GetAsync("/textXml")
78+
let! text = response.Content.ReadAsStringAsync()
79+
Assert.Equal("""<?xml version="1.0" encoding="utf-8"?>
80+
<string>4</string>""",text)
81+
Assert.Equal(HttpStatusCode.OK ,response.StatusCode)
82+
}
83+
84+
[<Fact>]
85+
let ``POST /postXml -> OK "42"``() = task {
86+
use xmlContent = new StringContent("""<?xml version="1.0" encoding="utf-8"?>
87+
<int>42</int>""", Text.Encoding.UTF8, "application/xml")
88+
let! response = client.PostAsync("/postXml", xmlContent)
89+
let! text = response.Content.ReadAsStringAsync()
90+
Assert.Equal("""<?xml version="1.0" encoding="utf-8"?>
91+
<int>42</int>""",text)
92+
Assert.Equal(HttpStatusCode.OK ,response.StatusCode)
93+
}
94+
95+
interface IDisposable with
96+
member _.Dispose() = server.Dispose()
Lines changed: 60 additions & 0 deletions

0 commit comments

Comments
 (0)