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
61 changes: 61 additions & 0 deletions tests/SwaggerProvider.Tests/Schema.V2SchemaCompilationTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,67 @@ let ``v2 allOf inheritance derived type has its own property``() =
let dogType = types |> List.find(fun t -> t.Name = "Dog")
dogType.GetDeclaredProperty("Breed") |> isNull |> shouldEqual false

// ── V2 allOf single $ref collapse ─────────────────────────────────────────────

/// Swagger 2.0 schema where Alias wraps Pet via allOf with a single $ref and no
/// own properties. The compiler should collapse Alias into Pet.
let private v2AllOfSingleRefSchema =
"""{
"swagger": "2.0",
"info": { "title": "AliasTest", "version": "1.0.0" },
"basePath": "/",
"paths": {},
"definitions": {
"Pet": {
"type": "object",
"properties": {
"id": { "type": "integer" }
}
},
"Alias": {
"allOf": [ { "$ref": "#/definitions/Pet" } ]
}
}
}"""

[<Fact>]
let ``v2 allOf single ref collapses to the referenced type``() =
let types = compileV2Schema v2AllOfSingleRefSchema
types |> List.exists(fun t -> t.Name = "Pet") |> shouldEqual true

[<Fact>]
let ``v2 allOf single ref does not produce a separate wrapper type``() =
let types = compileV2Schema v2AllOfSingleRefSchema
types |> List.exists(fun t -> t.Name = "Alias") |> shouldEqual false

// ── V2 top-level string enum ──────────────────────────────────────────────────

/// Swagger 2.0 schema with a top-level string enum definition.
let private v2StringEnumSchema =
"""{
"swagger": "2.0",
"info": { "title": "EnumTest", "version": "1.0.0" },
"basePath": "/",
"paths": {},
"definitions": {
"Color": {
"type": "string",
"enum": ["red", "green", "blue"]
}
}
}"""

[<Fact>]
let ``v2 top-level string enum compiles to a named enum type``() =
let types = compileV2Schema v2StringEnumSchema
types |> List.exists(fun t -> t.Name = "Color") |> shouldEqual true

[<Fact>]
let ``v2 top-level string enum type is an enum``() =
let types = compileV2Schema v2StringEnumSchema
let colorType = types |> List.find(fun t -> t.Name = "Color")
colorType.IsEnum |> shouldEqual true

// ── ToString tests ───────────────────────────────────────────────────────────

[<Fact>]
Expand Down
44 changes: 44 additions & 0 deletions tests/SwaggerProvider.Tests/Schema.V3SchemaCompilationTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,50 @@ let ``anyOf with multiple refs emits the referenced component types``() =
types |> List.exists(fun t -> t.Name = "Fish") |> shouldEqual true
types |> List.exists(fun t -> t.Name = "Bird") |> shouldEqual true

// ── $ref to enum type β†’ property uses the named enum type ────────────────────

/// OpenAPI 3.0 schema where Task.status references Status (a string enum) via $ref.
let private refToEnumSchema =
"""{
"openapi": "3.0.0",
"info": { "title": "Test", "version": "1.0.0" },
"paths": {},
"components": {
"schemas": {
"Status": {
"type": "string",
"enum": ["open", "closed"]
},
"Task": {
"type": "object",
"required": ["status"],
"properties": {
"status": { "$ref": "#/components/schemas/Status" }
}
}
}
}
}"""

[<Fact>]
let ``required property referencing a string enum uses the named enum type``() =
let types = compileV3Schema refToEnumSchema false
let statusType = types |> List.find(fun t -> t.Name = "Status")
let taskType = types |> List.find(fun t -> t.Name = "Task")
let statusProp = taskType.GetDeclaredProperty("Status")
statusProp |> isNull |> shouldEqual false
statusProp.PropertyType |> shouldEqual statusType

[<Fact>]
let ``required property referencing a string enum is not wrapped in option``() =
// Required $ref to enum: the property should use the enum type directly (not Option<EnumType>).
let types = compileV3Schema refToEnumSchema false
let statusType = types |> List.find(fun t -> t.Name = "Status")
let taskType = types |> List.find(fun t -> t.Name = "Task")
let statusProp = taskType.GetDeclaredProperty("Status")
statusProp.PropertyType |> shouldEqual statusType
statusProp.PropertyType.IsGenericType |> shouldEqual false
Comment thread
sergey-tihon marked this conversation as resolved.

// ── allOf single $ref with extra wrapper properties β†’ new object type ─────────

/// OpenAPI 3.0 schema where Extended wraps Base via allOf with a single $ref,
Expand Down
Loading