Skip to content

Commit 64214ad

Browse files
committed
Merge PR #479 into #478 and consolidate alias fix
2 parents 0216d42 + 055cee0 commit 64214ad

5 files changed

Lines changed: 100 additions & 126 deletions

File tree

‎src/SwaggerProvider.DesignTime/DefinitionCompiler.fs‎

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -235,24 +235,16 @@ type DefinitionCompiler(schema: OpenApiDocument, provideNullable, useDateOnly: b
235235

236236
providedField, providedProperty
237237

238-
let registerInNsAndInDef tyPath (ns: NamespaceAbstraction) (name, ty: Type) =
239-
// Detect alias: a ProvidedTypeDefinition that was already registered for a different
240-
// component path (e.g. a named component that is a single-ref oneOf/anyOf/allOf wrapper).
241-
// For aliases we only need the pathToType cache entry; adding the same
242-
// ProvidedTypeDefinition to the namespace a second time would cause a
243-
// "duplicate entry" error during assembly emit (issue #477).
244-
let isAlias =
245-
match ty with
246-
| :? ProvidedTypeDefinition -> pathToType.Values |> Seq.exists(fun v -> obj.ReferenceEquals(v, ty))
247-
| _ -> false
248-
238+
let registerInDef tyPath (ty: Type) =
249239
if not <| pathToType.ContainsKey tyPath then
250240
pathToType.Add(tyPath, ty)
251241

252-
if not isAlias then
253-
match ty with
254-
| :? ProvidedTypeDefinition as prTy -> ns.RegisterType(name, prTy)
255-
| _ -> ()
242+
let registerInNsAndInDef tyPath (ns: NamespaceAbstraction) (name, ty: Type) =
243+
registerInDef tyPath ty
244+
245+
match ty with
246+
| :? ProvidedTypeDefinition as prTy -> ns.RegisterType(name, prTy)
247+
| _ -> ()
256248

257249
let rec compileByPath(tyPath: string) : Type =
258250
match pathToType.TryGetValue tyPath with
@@ -262,6 +254,8 @@ type DefinitionCompiler(schema: OpenApiDocument, provideNullable, useDateOnly: b
262254
| true, def ->
263255
let ns, tyName = tyPath |> DefinitionPath.Parse |> nsRoot.Resolve
264256
let ty = compileBySchema ns tyName def true (registerInNsAndInDef tyPath ns) true
257+
// An alias can resolve to an existing provided type, so only cache its component path here.
258+
registerInDef tyPath ty
265259
ty :> Type
266260
| false, _ when tyPath.StartsWith DefinitionPath.DefinitionPrefix ->
267261
failwithf $"Cannot find definition '%s{tyPath}' in schema definitions %A{pathToType.Keys |> Seq.toArray}"
@@ -615,6 +609,7 @@ type DefinitionCompiler(schema: OpenApiDocument, provideNullable, useDateOnly: b
615609
enumTy.AddMember field
616610
intValue <- intValue + 1L
617611

612+
registerNew(tyName, enumTy :> Type)
618613
enumTy :> Type
619614
| _ ->
620615
ns.MarkTypeAsNameAlias tyName
@@ -651,9 +646,6 @@ type DefinitionCompiler(schema: OpenApiDocument, provideNullable, useDateOnly: b
651646
elTy.MakeArrayType 1
652647
| ty, format -> failwithf $"Type %s{tyName}(%A{ty},%s{format}) should be caught by other match statement (%A{resolvedType})"
653648

654-
if fromByPathCompiler then
655-
registerNew(tyName, tyType)
656-
657649
if isRequired then
658650
tyType
659651
else if tyType.IsValueType then
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{
2+
"openapi": "3.0.0",
3+
"info": {
4+
"title": "Named oneOf object alias",
5+
"version": "1.0.0"
6+
},
7+
"paths": {
8+
"/parent": {
9+
"get": {
10+
"operationId": "getParent",
11+
"responses": {
12+
"200": {
13+
"description": "Parent response",
14+
"content": {
15+
"application/json": {
16+
"schema": {
17+
"$ref": "#/components/schemas/Parent"
18+
}
19+
}
20+
}
21+
}
22+
}
23+
}
24+
}
25+
},
26+
"components": {
27+
"schemas": {
28+
"Parent": {
29+
"oneOf": [
30+
{
31+
"$ref": "#/components/schemas/Parent_Child"
32+
}
33+
]
34+
},
35+
"Parent_Child": {
36+
"type": "object",
37+
"required": [
38+
"childValue"
39+
],
40+
"properties": {
41+
"childValue": {
42+
"type": "string"
43+
}
44+
}
45+
}
46+
}
47+
}
48+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
module Swagger.NamedObjectAliases.Tests
2+
3+
open SwaggerProvider
4+
open Xunit
5+
open FsUnitTyped
6+
7+
[<Literal>]
8+
let Schema = __SOURCE_DIRECTORY__ + "/Schemas/v3/named-object-alias-oneof.json"
9+
10+
type Api = OpenApiClientProvider<Schema, SsrfProtection=false>
11+
12+
[<Fact>]
13+
let ``named component alias response resolves to the referenced object type``() =
14+
let methodInfo =
15+
typeof<Api.Client>.GetMethods()
16+
|> Array.filter(fun candidate -> candidate.Name = "GetParent")
17+
|> Array.exactlyOne
18+
19+
let responseType = methodInfo.ReturnType.GetGenericArguments() |> Array.exactlyOne
20+
21+
responseType |> shouldEqual typeof<Api.Parent_Child>
22+
23+
let response = Api.Parent_Child("oneOf")
24+
response.ChildValue |> shouldEqual "oneOf"

‎tests/SwaggerProvider.ProviderTests/SwaggerProvider.ProviderTests.fsproj‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<Compile Include="Swagger.I0181.Tests.fs" />
1717
<Compile Include="Swagger.I0219.Tests.fs" />
1818
<Compile Include="Swagger.I0279.Tests.fs" />
19+
<Compile Include="Swagger.NamedObjectAliases.Tests.fs" />
1920
<Compile Include="Swagger.NullableDate.Tests.fs" />
2021
<Compile Include="Swagger.SchemaReaderErrors.Tests.fs" />
2122
<Compile Include="Swashbuckle.ReturnControllers.Tests.fs" />

‎tests/SwaggerProvider.Tests/Schema.V3SchemaCompilationTests.fs‎

Lines changed: 17 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,13 @@ let private anyOfSingleRefSchema =
7777
}"""
7878

7979
[<Fact>]
80-
let ``allOf single $ref resolves to the referenced type without creating a new object type``() =
80+
let ``allOf single $ref resolves to exactly one referenced type``() =
8181
let types = compileV3Schema allOfSingleRefSchema false
82-
// PetRef collapses into Pet via ReleaseNameReservation; the referenced type is present.
83-
types |> List.exists(fun t -> t.Name = "Pet") |> shouldEqual true
82+
// PetRef collapses into Pet via ReleaseNameReservation; the referenced type is registered once.
83+
types
84+
|> List.filter(fun t -> t.Name = "Pet")
85+
|> List.length
86+
|> shouldEqual 1
8487

8588
[<Fact>]
8689
let ``allOf single $ref does not produce a separate wrapper type``() =
@@ -89,126 +92,32 @@ let ``allOf single $ref does not produce a separate wrapper type``() =
8992
types |> List.exists(fun t -> t.Name = "PetRef") |> shouldEqual false
9093

9194
[<Fact>]
92-
let ``oneOf single $ref resolves to the referenced type``() =
93-
let types = compileV3Schema oneOfSingleRefSchema false
94-
types |> List.exists(fun t -> t.Name = "Dog") |> shouldEqual true
95-
96-
[<Fact>]
97-
let ``oneOf single $ref does not produce a separate wrapper type``() =
95+
let ``oneOf single $ref resolves to exactly one referenced type``() =
9896
let types = compileV3Schema oneOfSingleRefSchema false
99-
types |> List.exists(fun t -> t.Name = "DogRef") |> shouldEqual false
100-
101-
[<Fact>]
102-
let ``anyOf single $ref resolves to the referenced type``() =
103-
let types = compileV3Schema anyOfSingleRefSchema false
104-
types |> List.exists(fun t -> t.Name = "Cat") |> shouldEqual true
105-
106-
[<Fact>]
107-
let ``anyOf single $ref does not produce a separate wrapper type``() =
108-
let types = compileV3Schema anyOfSingleRefSchema false
109-
types |> List.exists(fun t -> t.Name = "CatRef") |> shouldEqual false
110-
111-
// ── Named component alias to another named object component (issue #477) ─────
112-
//
113-
// When a named component schema is a single-ref oneOf/anyOf/allOf wrapper that
114-
// resolves to another named object component, the resolved ProvidedTypeDefinition
115-
// must appear in the type list exactly once. Before the fix, registerInNsAndInDef
116-
// would register the same PTD a second time (once for the target, once for the
117-
// alias), causing "duplicate entry '<name>' in type index table" during assembly emit.
118-
119-
let private namedAliasOneOfSchema =
120-
"""{
121-
"openapi": "3.0.0",
122-
"info": { "title": "Test", "version": "1.0.0" },
123-
"paths": {},
124-
"components": {
125-
"schemas": {
126-
"Parent": {
127-
"oneOf": [ { "$ref": "#/components/schemas/Parent_Child" } ]
128-
},
129-
"Parent_Child": {
130-
"type": "object",
131-
"required": ["childValue"],
132-
"properties": {
133-
"childValue": { "type": "string" }
134-
}
135-
}
136-
}
137-
}
138-
}"""
139-
140-
let private namedAliasAllOfSchema =
141-
"""{
142-
"openapi": "3.0.0",
143-
"info": { "title": "Test", "version": "1.0.0" },
144-
"paths": {},
145-
"components": {
146-
"schemas": {
147-
"MyAlias": {
148-
"allOf": [ { "$ref": "#/components/schemas/MyObject" } ]
149-
},
150-
"MyObject": {
151-
"type": "object",
152-
"properties": { "value": { "type": "integer" } }
153-
}
154-
}
155-
}
156-
}"""
157-
158-
let private namedAliasAnyOfSchema =
159-
"""{
160-
"openapi": "3.0.0",
161-
"info": { "title": "Test", "version": "1.0.0" },
162-
"paths": {},
163-
"components": {
164-
"schemas": {
165-
"AliasAnyOf": {
166-
"anyOf": [ { "$ref": "#/components/schemas/TargetObject" } ]
167-
},
168-
"TargetObject": {
169-
"type": "object",
170-
"properties": { "name": { "type": "string" } }
171-
}
172-
}
173-
}
174-
}"""
175-
176-
[<Fact>]
177-
let ``oneOf named alias to object component does not produce duplicate provided type``() =
178-
// Regression test for issue #477: Parent aliases Parent_Child via oneOf.
179-
// The resolved ProvidedTypeDefinition must appear exactly once.
180-
let types = compileV3Schema namedAliasOneOfSchema false
18197

18298
types
183-
|> List.filter(fun t -> t.Name = "Parent_Child")
99+
|> List.filter(fun t -> t.Name = "Dog")
184100
|> List.length
185101
|> shouldEqual 1
186102

187103
[<Fact>]
188-
let ``oneOf named alias to object component exposes the target type``() =
189-
let types = compileV3Schema namedAliasOneOfSchema false
190-
191-
types
192-
|> List.exists(fun t -> t.Name = "Parent_Child")
193-
|> shouldEqual true
104+
let ``oneOf single $ref does not produce a separate wrapper type``() =
105+
let types = compileV3Schema oneOfSingleRefSchema false
106+
types |> List.exists(fun t -> t.Name = "DogRef") |> shouldEqual false
194107

195108
[<Fact>]
196-
let ``allOf named alias to object component does not produce duplicate provided type``() =
197-
let types = compileV3Schema namedAliasAllOfSchema false
109+
let ``anyOf single $ref resolves to exactly one referenced type``() =
110+
let types = compileV3Schema anyOfSingleRefSchema false
198111

199112
types
200-
|> List.filter(fun t -> t.Name = "MyObject")
113+
|> List.filter(fun t -> t.Name = "Cat")
201114
|> List.length
202115
|> shouldEqual 1
203116

204117
[<Fact>]
205-
let ``anyOf named alias to object component does not produce duplicate provided type``() =
206-
let types = compileV3Schema namedAliasAnyOfSchema false
207-
208-
types
209-
|> List.filter(fun t -> t.Name = "TargetObject")
210-
|> List.length
211-
|> shouldEqual 1
118+
let ``anyOf single $ref does not produce a separate wrapper type``() =
119+
let types = compileV3Schema anyOfSingleRefSchema false
120+
types |> List.exists(fun t -> t.Name = "CatRef") |> shouldEqual false
212121

213122
// ── Required vs optional properties ──────────────────────────────────────────
214123

0 commit comments

Comments
 (0)