diff --git a/src/SwaggerProvider.DesignTime/v3/DefinitionCompiler.fs b/src/SwaggerProvider.DesignTime/v3/DefinitionCompiler.fs index a0e980d..09ffae1 100644 --- a/src/SwaggerProvider.DesignTime/v3/DefinitionCompiler.fs +++ b/src/SwaggerProvider.DesignTime/v3/DefinitionCompiler.fs @@ -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 @@ -245,18 +245,37 @@ type DefinitionCompiler(schema: OpenApiDocument, provideNullable) as this = let ty = ProvidedTypeDefinition(tyName, Some typeof, 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 + | 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 @@ -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 diff --git a/tests/SwaggerProvider.ProviderTests/Schemas/v3/issue255.yaml b/tests/SwaggerProvider.ProviderTests/Schemas/v3/issue255.yaml new file mode 100644 index 0000000..eb054ed --- /dev/null +++ b/tests/SwaggerProvider.ProviderTests/Schemas/v3/issue255.yaml @@ -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" diff --git a/tests/SwaggerProvider.Tests/Schema.Parser.Tests.fs b/tests/SwaggerProvider.Tests/Schema.Parser.Tests.fs index 5867d93..caff84b 100644 --- a/tests/SwaggerProvider.Tests/Schema.Parser.Tests.fs +++ b/tests/SwaggerProvider.Tests/Schema.Parser.Tests.fs @@ -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 { @@ -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 = @@ -89,6 +89,17 @@ let ``Parse PetStore``() = + "/../SwaggerProvider.ProviderTests/Schemas/v2/petstore.json" ) +[] +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 + (* [] let parseJsonSchemaTests =