Skip to content
Merged
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
6 changes: 6 additions & 0 deletions src/SwaggerProvider.DesignTime/DefinitionCompiler.fs
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,9 @@ type DefinitionCompiler(schema: OpenApiDocument, provideNullable, useDateOnly: b
let ty = ProvidedTypeDefinition(tyName, Some typeof<obj>, isErased = false)
registerNew(tyName, ty :> Type)

if not(String.IsNullOrWhiteSpace schemaObj.Description) then
ty.AddXmlDoc schemaObj.Description

// Combine composite schemas
let schemaObjProperties =
let getProps(s: IOpenApiSchema) =
Expand Down Expand Up @@ -549,6 +552,9 @@ type DefinitionCompiler(schema: OpenApiDocument, provideNullable, useDateOnly: b

enumTy.SetEnumUnderlyingType underlyingIntType

if not(String.IsNullOrWhiteSpace schemaObj.Description) then
enumTy.AddXmlDoc schemaObj.Description

// String enums need [JsonConverter(typeof<JsonStringEnumConverter>)] on the type
// so System.Text.Json serialises them as strings rather than integers.
// Per-member [JsonStringEnumMemberName] attributes (added below) let it honour
Expand Down
124 changes: 124 additions & 0 deletions tests/SwaggerProvider.Tests/Schema.XmlDocTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -208,3 +208,127 @@ let ``non-enum query parameter does not add Allowed values to XmlDoc``() =
doc.IsSome |> shouldEqual true
doc.Value |> shouldContainText "Health check"
doc.Value |> shouldNotContainText "Allowed values:"

// ── Type-level XmlDoc (schema descriptions on generated types) ───────────────

let private compileSchemaTypes(schemaStr: string) =
let schema = parseSchema schemaStr
let defCompiler = DefinitionCompiler(schema, false, false)
let opCompiler = OperationCompiler(schema, defCompiler, true, false, true)
opCompiler.CompileProvidedClients(defCompiler.Namespace)
defCompiler.Namespace.GetProvidedTypes()

[<Fact>]
let ``object type with description gets XmlDoc``() =
let schemaStr =
"""openapi: "3.0.0"
info:
title: XmlDocTypeTest
version: "1.0.0"
paths: {}
components:
schemas:
Pet:
type: object
description: "A pet in the system"
properties:
name:
type: string
"""

let types = compileSchemaTypes schemaStr
let petTy = types |> List.find(fun t -> t.Name = "Pet")
getXmlDocAttr petTy |> shouldEqual(Some "A pet in the system")

[<Fact>]
let ``object type without description has no XmlDoc``() =
let schemaStr =
"""openapi: "3.0.0"
info:
title: XmlDocTypeTest
version: "1.0.0"
paths: {}
components:
schemas:
Widget:
type: object
properties:
id:
type: integer
"""

let types = compileSchemaTypes schemaStr
let widgetTy = types |> List.find(fun t -> t.Name = "Widget")
getXmlDocAttr widgetTy |> shouldEqual None

[<Fact>]
let ``named string enum type with description gets XmlDoc``() =
let schemaStr =
"""openapi: "3.0.0"
info:
title: XmlDocEnumTest
version: "1.0.0"
paths: {}
components:
schemas:
Status:
type: string
description: "The lifecycle status of an item"
enum:
- active
- inactive
- pending
"""

let types = compileSchemaTypes schemaStr
let statusTy = types |> List.find(fun t -> t.Name = "Status")

getXmlDocAttr statusTy
|> shouldEqual(Some "The lifecycle status of an item")

[<Fact>]
let ``named integer enum type with description gets XmlDoc``() =
let schemaStr =
"""openapi: "3.0.0"
info:
title: XmlDocEnumTest
version: "1.0.0"
paths: {}
components:
schemas:
Priority:
type: integer
description: "Priority level (1=low, 2=medium, 3=high)"
enum:
- 1
- 2
- 3
"""

let types = compileSchemaTypes schemaStr
let priorityTy = types |> List.find(fun t -> t.Name = "Priority")

getXmlDocAttr priorityTy
|> shouldEqual(Some "Priority level (1=low, 2=medium, 3=high)")

[<Fact>]
let ``named enum type without description has no XmlDoc``() =
let schemaStr =
"""openapi: "3.0.0"
info:
title: XmlDocEnumTest
version: "1.0.0"
paths: {}
components:
schemas:
Color:
type: string
enum:
- red
- green
- blue
"""

let types = compileSchemaTypes schemaStr
let colorTy = types |> List.find(fun t -> t.Name = "Color")
getXmlDocAttr colorTy |> shouldEqual None
Loading