Skip to content

Commit

Permalink
Enum values are checked when parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
Dzoukr committed Sep 19, 2018
1 parent 50507f7 commit 0c72fb4
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 14 deletions.
4 changes: 2 additions & 2 deletions .paket/Paket.Restore.targets
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
<PaketRestoreRequired Condition=" '$(PaketRestoreLockFileHash)' == '$(PaketRestoreCachedHash)' ">false</PaketRestoreRequired>
<PaketRestoreRequired Condition=" '$(PaketRestoreLockFileHash)' == '' ">true</PaketRestoreRequired>
</PropertyGroup>

<PropertyGroup Condition="'$(PaketPropsVersion)' != '5.174.2' ">
<PaketRestoreRequired>true</PaketRestoreRequired>
</PropertyGroup>
Expand All @@ -96,7 +96,7 @@
<PaketOriginalReferencesFilePath Condition=" !Exists('$(PaketOriginalReferencesFilePath)')">$(MSBuildProjectDirectory)\$(MSBuildProjectName).paket.references</PaketOriginalReferencesFilePath>
<!-- paket.references -->
<PaketOriginalReferencesFilePath Condition=" !Exists('$(PaketOriginalReferencesFilePath)')">$(MSBuildProjectDirectory)\paket.references</PaketOriginalReferencesFilePath>

<DoAllResolvedFilesExist>false</DoAllResolvedFilesExist>
<DoAllResolvedFilesExist Condition="Exists(%(PaketResolvedFilePaths.Identity))">true</DoAllResolvedFilesExist>
<PaketRestoreRequired>true</PaketRestoreRequired>
Expand Down
3 changes: 3 additions & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
### 1.3 - September 19 2018
* Enum values are checked when parsing

### 1.2 - September 14 2018
* Added filewatcher

Expand Down
16 changes: 5 additions & 11 deletions paket.lock
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ NUGET
Microsoft.NET.Test.Sdk (15.8)
Microsoft.CodeCoverage (>= 15.8) - restriction: || (== netcoreapp2.1) (&& (== netstandard2.0) (>= net45)) (&& (== netstandard2.0) (>= netcoreapp1.0))
Microsoft.TestPlatform.TestHost (>= 15.8) - restriction: || (== netcoreapp2.1) (&& (== netstandard2.0) (>= netcoreapp1.0))
Microsoft.NETCore.Platforms (2.1)
Microsoft.NETCore.Platforms (2.1.1)
Microsoft.NETCore.Targets (2.1)
Microsoft.TestPlatform.ObjectModel (15.8) - restriction: || (== netcoreapp2.1) (&& (== netstandard2.0) (>= netcoreapp1.0))
NETStandard.Library (>= 1.6)
Expand All @@ -80,7 +80,6 @@ NUGET
Microsoft.NETCore.Targets (>= 1.1)
System.Runtime (>= 4.3)
Microsoft.Win32.Registry (4.5) - restriction: || (== netcoreapp2.1) (&& (== netstandard2.0) (>= netcoreapp1.0))
System.Memory (>= 4.5)
System.Security.AccessControl (>= 4.5)
System.Security.Principal.Windows (>= 4.5)
NETStandard.Library (2.0.3)
Expand All @@ -98,7 +97,7 @@ NUGET
System.Threading.Thread (>= 4.3) - restriction: || (== netcoreapp2.1) (&& (== netstandard2.0) (>= netcoreapp1.0))
System.Xml.XmlDocument (>= 4.3) - restriction: || (== netcoreapp2.1) (&& (== netstandard2.0) (>= netcoreapp1.0))
System.Xml.XPath.XmlDocument (>= 4.3) - restriction: || (== netcoreapp2.1) (&& (== netstandard2.0) (>= netcoreapp1.0))
OpenAPIParser (1.1)
OpenAPIParser (1.2)
FSharp.Core (>= 4.2.3)
Newtonsoft.Json (>= 10.0.1)
YamlDotNet (>= 4.3.1)
Expand Down Expand Up @@ -340,8 +339,6 @@ NUGET
System.Reflection.Extensions (>= 4.3)
System.Resources.ResourceManager (>= 4.3)
System.Runtime (>= 4.3)
System.Memory (4.5.1) - restriction: || (== netcoreapp2.1) (&& (== netstandard2.0) (>= netcoreapp1.0)) (&& (== netstandard2.0) (>= netcoreapp2.0))
System.Runtime.CompilerServices.Unsafe (>= 4.5)
System.Net.Http (4.3.3)
Microsoft.NETCore.Platforms (>= 1.1)
runtime.native.System (>= 4.3)
Expand Down Expand Up @@ -456,7 +453,7 @@ NUGET
Microsoft.NETCore.Platforms (>= 1.1)
Microsoft.NETCore.Targets (>= 1.1)
System.Runtime (>= 4.3)
System.Reflection.TypeExtensions (4.5)
System.Reflection.TypeExtensions (4.5.1)
System.Resources.ResourceManager (4.3)
Microsoft.NETCore.Platforms (>= 1.1)
Microsoft.NETCore.Targets (>= 1.1)
Expand All @@ -466,7 +463,6 @@ NUGET
System.Runtime (4.3)
Microsoft.NETCore.Platforms (>= 1.1)
Microsoft.NETCore.Targets (>= 1.1)
System.Runtime.CompilerServices.Unsafe (4.5.1) - restriction: || (== netcoreapp2.1) (&& (== netstandard2.0) (>= netcoreapp1.0)) (&& (== netstandard2.0) (>= netcoreapp2.0))
System.Runtime.Extensions (4.3)
Microsoft.NETCore.Platforms (>= 1.1)
Microsoft.NETCore.Targets (>= 1.1)
Expand Down Expand Up @@ -525,7 +521,6 @@ NUGET
System.Security.Cryptography.Primitives (>= 4.3)
System.Text.Encoding (>= 4.3)
System.Security.Cryptography.Cng (4.5)
Microsoft.NETCore.Platforms (>= 2.0) - restriction: || (== netcoreapp2.1) (&& (== netstandard2.0) (>= netcoreapp2.0))
System.Security.Cryptography.Csp (4.3)
Microsoft.NETCore.Platforms (>= 1.1)
System.IO (>= 4.3)
Expand Down Expand Up @@ -554,7 +549,6 @@ NUGET
System.Security.Cryptography.Primitives (>= 4.3)
System.Text.Encoding (>= 4.3)
System.Security.Cryptography.OpenSsl (4.5)
Microsoft.NETCore.Platforms (>= 2.0) - restriction: || (== netcoreapp2.1) (&& (== netstandard2.0) (>= netcoreapp2.0))
System.Security.Cryptography.Primitives (4.3)
System.Diagnostics.Debug (>= 4.3)
System.Globalization (>= 4.3)
Expand Down Expand Up @@ -715,5 +709,5 @@ NUGET
YamlDotNet (5.0.1)
GITHUB
remote: fsprojects/FSharp.TypeProviders.SDK
src/ProvidedTypes.fs (d50b7556cc57c403cd53bd1116027450d60c72db)
src/ProvidedTypes.fsi (d50b7556cc57c403cd53bd1116027450d60c72db)
src/ProvidedTypes.fs (45cae888d8cf104b6e02b93cd4ed4546e6762fed)
src/ProvidedTypes.fsi (45cae888d8cf104b6e02b93cd4ed4546e6762fed)
3 changes: 2 additions & 1 deletion src/OpenAPITypeProvider/Inference.fs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ let private getStringType = function
| StringFormat.Byte -> typeof<byte>
| StringFormat.Date | StringFormat.DateTime -> typeof<DateTime>
| StringFormat.UUID -> typeof<Guid>
| StringFormat.Enum _ -> typeof<string>

let rec getComplexType (getSchemaFun: Schema -> Type) schema =
match schema with
Expand All @@ -26,4 +27,4 @@ let rec getComplexType (getSchemaFun: Schema -> Type) schema =
| Array schema ->
let typ = schema |> getComplexType getSchemaFun
typedefof<List<_>>.MakeGenericType([|typ|])
| Object _ -> schema |> getSchemaFun
| Object _ -> schema |> getSchemaFun
6 changes: 6 additions & 0 deletions src/OpenAPITypeProvider/Json/Parser.fs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ let private checkRequiredProperties (req:string list) (jObject:JObject) =

let defaultDateFormat = "yyyy-MM-ddTHH:mm:ss.FFFFFFFK"

let private isOneOfAllowed (allowedValues:string list) (value:string) =
match allowedValues |> List.tryFind ((=) value) with
| Some v -> value
| None -> FormatException (sprintf "Invalid value %s - Enum must contain one of allowed values: %A" value allowedValues) |> raise

let rec parseForSchema createObj defaultTyp (schema:Schema) (json:JToken) =
match schema with
| Boolean -> json.Value<bool>() |> box
Expand All @@ -28,6 +33,7 @@ let rec parseForSchema createObj defaultTyp (schema:Schema) (json:JToken) =
| String StringFormat.DateTime
| String StringFormat.Date -> json.Value<DateTime>() |> box
| String StringFormat.UUID -> json.Value<string>() |> Guid |> box
| String (StringFormat.Enum values) -> json.Value<string>() |> isOneOfAllowed values |> box
| Array itemsSchema ->
let jArray = json :?> JArray
let items = [ for x in jArray do yield parseForSchema createObj defaultTyp itemsSchema x ]
Expand Down
17 changes: 17 additions & 0 deletions tests/OpenAPITypeProvider.Tests/BasicTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,16 @@ let ``Parses and converts basic schema with UUID property (from JSON)``() =
Assert.AreEqual("Name", parsed.Name)
Assert.AreEqual(guid, parsed.Id)

[<Test>]
let ``Parses and converts basic schema with enum``() =
let json = """{"id":"123","myEnum":"b"}"""
let instance = PetStore.Schemas.WithEnum("123", "b")
let parsed = PetStore.Schemas.WithEnum.Parse json
Assert.AreEqual(json, instance.ToJson(Formatting.None))
Assert.AreEqual(json, parsed.ToJson(Formatting.None))
Assert.AreEqual("b", instance.MyEnum)
Assert.AreEqual("b", parsed.MyEnum)

[<Test>]
let ``Fails with parsing mismatched types``() =
Assert.Throws<System.FormatException>(fun _ ->
Expand All @@ -290,6 +300,13 @@ let ``Fails with parsing null in required properties``() =
PetStore.Schemas.Error.Parse json |> ignore
) |> ignore

[<Test>]
let ``Fails with parsing in unallowed enum value``() =
Assert.Throws<System.FormatException>(fun _ ->
let json = """{"id":123,"myEnum":"d"}"""
PetStore.Schemas.WithEnum.Parse json |> ignore
) |> ignore

//[<Test>]
//let ``Path methods works``() =
// let petStoreAPI = new PetStore()
Expand Down
9 changes: 9 additions & 0 deletions tests/OpenAPITypeProvider.Tests/Samples/PetStore.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,15 @@ components:
WithUUID:
$ref: 'ExternalSchemas.yaml#WithUUID'

WithEnum:
properties:
id:
type: string
myEnum:
type: string
enum: [a, b, c]
required: [id, myEnum]

Error:
required:
- code
Expand Down

0 comments on commit 0c72fb4

Please sign in to comment.