Skip to content

Commit

Permalink
Added allOf properties to the schema definition (#256)
Browse files Browse the repository at this point in the history
* Added allOf properties to the schema definition (#255)

* Better PM

* Fixed formatting
  • Loading branch information
QrystaL authored Jan 16, 2025
1 parent 8e2e350 commit 8eb1183
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 9 deletions.
31 changes: 25 additions & 6 deletions src/SwaggerProvider.DesignTime/v3/DefinitionCompiler.fs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ type DefinitionCompiler(schema: OpenApiDocument, provideNullable) as this =

and compileBySchema (ns: NamespaceAbstraction) tyName (schemaObj: OpenApiSchema) isRequired registerNew fromByPathCompiler =
let compileNewObject() =
if schemaObj.Properties.Count = 0 then
if schemaObj.Properties.Count = 0 && schemaObj.AllOf.Count = 0 then
if not <| isNull tyName then
ns.MarkTypeAsNameAlias tyName

Expand All @@ -245,18 +245,37 @@ type DefinitionCompiler(schema: OpenApiDocument, provideNullable) as this =
let ty = ProvidedTypeDefinition(tyName, Some typeof<obj>, isErased = false)
registerNew(tyName, ty :> Type)

// Combine composite schemas
let schemaObjProperties =
match schemaObj.AllOf.Count > 0 with
| true ->
schemaObj.AllOf
|> Seq.append [ schemaObj ]
|> Seq.collect(fun x -> x.Properties)
| false -> schemaObj.Properties

let schemaObjRequired =
match schemaObj.AllOf.Count > 0 with
| true ->
schemaObj.AllOf
|> Seq.append [ schemaObj ]
|> Seq.collect(fun x -> x.Required)
|> System.Collections.Generic.HashSet
:> System.Collections.Generic.ISet<string>
| false -> schemaObj.Required

// Generate fields and properties
let members =
let generateProperty = generateProperty(UniqueNameGenerator())

List.ofSeq schemaObj.Properties
List.ofSeq schemaObjProperties
|> List.map(fun p ->
let propName, propSchema = p.Key, p.Value

if String.IsNullOrEmpty(propName) then
failwithf $"Property cannot be created with empty name. TypeName:%A{tyName}; SchemaObj:%A{schemaObj}"

let isRequired = schemaObj.Required.Contains(propName)
let isRequired = schemaObjRequired.Contains(propName)

let pTy =
compileBySchema ns (ns.ReserveUniqueName tyName (nicePascalName propName)) propSchema isRequired ns.RegisterType false
Expand All @@ -279,15 +298,15 @@ type DefinitionCompiler(schema: OpenApiDocument, provideNullable) as this =
// Add full-init constructor
let ctorParams, fields =
let required, optional =
List.zip (List.ofSeq schemaObj.Properties) members
|> List.partition(fun (x, _) -> schemaObj.Required.Contains(x.Key))
List.zip (List.ofSeq schemaObjProperties) members
|> List.partition(fun (x, _) -> schemaObjRequired.Contains(x.Key))

(required @ optional)
|> List.map(fun (x, (f, p)) ->
let paramName = niceCamelName p.Name

let prParam =
if schemaObj.Required.Contains(x.Key) then
if schemaObjRequired.Contains(x.Key) then
ProvidedParameter(paramName, f.FieldType)
else
let paramDefaultValue = this.GetDefaultValue f.FieldType
Expand Down
14 changes: 14 additions & 0 deletions tests/SwaggerProvider.ProviderTests/Schemas/v3/issue255.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
openapi: 3.0.0
paths:
/run:
summary: Do nothing.
components:
schemas:
Person:
title: Person
allOf:
- type: object
properties:
first_name:
type: string
description: "First Name"
17 changes: 14 additions & 3 deletions tests/SwaggerProvider.Tests/Schema.Parser.Tests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ module V3 =
let defCompiler = DefinitionCompiler(schema, false)
let opCompiler = OperationCompiler(schema, defCompiler, true, false, true)
opCompiler.CompileProvidedClients(defCompiler.Namespace)
ignore <| defCompiler.Namespace.GetProvidedTypes()
defCompiler.Namespace.GetProvidedTypes()
with e when e.Message.IndexOf("not supported yet") >= 0 ->
()
List.Empty

let parserTestBody(path: string) =
task {
Expand All @@ -49,7 +49,7 @@ let parserTestBody(path: string) =
if path.IndexOf("v2") >= 0 then
V2.testSchema schemaStr
else
V3.testSchema schemaStr
V3.testSchema schemaStr |> ignore
}

let rootFolder =
Expand Down Expand Up @@ -89,6 +89,17 @@ let ``Parse PetStore``() =
+ "/../SwaggerProvider.ProviderTests/Schemas/v2/petstore.json"
)

[<Fact>]
let ``Add definition for schema with only allOf properties``() =
let definitions =
__SOURCE_DIRECTORY__
+ "/../SwaggerProvider.ProviderTests/Schemas/v3/issue255.yaml"
|> File.ReadAllText
|> V3.testSchema

definitions |> shouldHaveLength 1
definitions[0].GetDeclaredProperty("FirstName") |> shouldNotEqual null

(*
[<Tests>]
let parseJsonSchemaTests =
Expand Down

0 comments on commit 8eb1183

Please sign in to comment.