Skip to content

Commit cad52f1

Browse files
[AGENT] Address alias registration review
1 parent d0aac22 commit cad52f1

4 files changed

Lines changed: 163 additions & 38 deletions

File tree

‎src/SwaggerProvider.DesignTime/DefinitionCompiler.fs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ type DefinitionCompiler(schema: OpenApiDocument, provideNullable, useDateOnly: b
254254
| true, def ->
255255
let ns, tyName = tyPath |> DefinitionPath.Parse |> nsRoot.Resolve
256256
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.
257258
registerInDef tyPath ty
258259
ty :> Type
259260
| false, _ when tyPath.StartsWith DefinitionPath.DefinitionPrefix ->
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
{
2+
"openapi": "3.0.0",
3+
"info": {
4+
"title": "Multiple named object aliases",
5+
"version": "1.0.0"
6+
},
7+
"paths": {
8+
"/parent-a": {
9+
"get": {
10+
"operationId": "getParentA",
11+
"responses": {
12+
"200": {
13+
"description": "First alias response",
14+
"content": {
15+
"application/json": {
16+
"schema": {
17+
"$ref": "#/components/schemas/ParentA"
18+
}
19+
}
20+
}
21+
}
22+
}
23+
}
24+
},
25+
"/parent-b": {
26+
"get": {
27+
"operationId": "getParentB",
28+
"responses": {
29+
"200": {
30+
"description": "Second alias response",
31+
"content": {
32+
"application/json": {
33+
"schema": {
34+
"$ref": "#/components/schemas/ParentB"
35+
}
36+
}
37+
}
38+
}
39+
}
40+
}
41+
},
42+
"/alias-chain": {
43+
"get": {
44+
"operationId": "getAliasChain",
45+
"responses": {
46+
"200": {
47+
"description": "Alias chain response",
48+
"content": {
49+
"application/json": {
50+
"schema": {
51+
"$ref": "#/components/schemas/AliasA"
52+
}
53+
}
54+
}
55+
}
56+
}
57+
}
58+
}
59+
},
60+
"components": {
61+
"schemas": {
62+
"ParentA": {
63+
"oneOf": [
64+
{
65+
"$ref": "#/components/schemas/Child"
66+
}
67+
]
68+
},
69+
"ParentB": {
70+
"anyOf": [
71+
{
72+
"$ref": "#/components/schemas/Child"
73+
}
74+
]
75+
},
76+
"Child": {
77+
"type": "object",
78+
"required": [
79+
"value"
80+
],
81+
"properties": {
82+
"value": {
83+
"type": "string"
84+
}
85+
}
86+
},
87+
"AliasA": {
88+
"oneOf": [
89+
{
90+
"$ref": "#/components/schemas/AliasB"
91+
}
92+
]
93+
},
94+
"AliasB": {
95+
"allOf": [
96+
{
97+
"$ref": "#/components/schemas/AliasC"
98+
}
99+
]
100+
},
101+
"AliasC": {
102+
"type": "object",
103+
"required": [
104+
"chainValue"
105+
],
106+
"properties": {
107+
"chainValue": {
108+
"type": "string"
109+
}
110+
}
111+
}
112+
}
113+
}
114+
}

‎tests/SwaggerProvider.ProviderTests/Swagger.NamedObjectAliases.Tests.fs‎

Lines changed: 47 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -17,77 +17,87 @@ let AllOfSchema = __SOURCE_DIRECTORY__ + "/Schemas/named-object-alias-allof.json
1717
let NamespacedSchema =
1818
__SOURCE_DIRECTORY__ + "/Schemas/named-object-alias-namespaced.json"
1919

20+
[<Literal>]
21+
let MultipleAliasesSchema =
22+
__SOURCE_DIRECTORY__ + "/Schemas/named-object-alias-multiple.json"
23+
2024
type OneOfApi = OpenApiClientProvider<OneOfSchema, SsrfProtection=false>
2125
type AnyOfApi = OpenApiClientProvider<AnyOfSchema, SsrfProtection=false>
2226
type AllOfApi = OpenApiClientProvider<AllOfSchema, SsrfProtection=false>
2327
type NamespacedApi = OpenApiClientProvider<NamespacedSchema, SsrfProtection=false>
28+
type MultipleAliasesApi = OpenApiClientProvider<MultipleAliasesSchema, SsrfProtection=false>
2429

25-
let private oneOfClient = OneOfApi.Client()
26-
let private anyOfClient = AnyOfApi.Client()
27-
let private allOfClient = AllOfApi.Client()
28-
let private namespacedClient = NamespacedApi.Client()
29-
30-
// These helpers are compile-time assertions that operation responses expose the child property.
31-
let private getOneOfChildValue() =
32-
task {
33-
let! response = oneOfClient.GetParent()
34-
return response.ChildValue
35-
}
36-
37-
let private getAnyOfChildValue() =
38-
task {
39-
let! response = anyOfClient.GetParent()
40-
return response.ChildValue
41-
}
42-
43-
let private getAllOfChildValue() =
44-
task {
45-
let! response = allOfClient.GetParent()
46-
return response.ChildValue
47-
}
48-
49-
let private getNamespacedValue() =
50-
task {
51-
let! response = namespacedClient.GetParent()
52-
return response.Value
53-
}
54-
55-
let private getResponseType(client: obj) =
30+
let private getResponseType (clientType: System.Type) operationName =
5631
let methodInfo =
57-
client.GetType().GetMethods()
58-
|> Array.filter(fun candidate -> candidate.Name = "GetParent")
32+
clientType.GetMethods()
33+
|> Array.filter(fun candidate -> candidate.Name = operationName)
5934
|> Array.exactlyOne
6035

6136
methodInfo.ReturnType.GetGenericArguments() |> Array.exactlyOne
6237

38+
let private getGeneratedTypeNames(generatedType: System.Type) =
39+
generatedType.DeclaringType.GetNestedTypes()
40+
|> Array.map(fun nestedType -> nestedType.Name)
41+
6342
[<Fact>]
6443
let ``oneOf named component alias response resolves to child object type``() =
65-
getResponseType oneOfClient
44+
getResponseType typeof<OneOfApi.Client> "GetParent"
6645
|> shouldEqual typeof<OneOfApi.Parent_Child>
6746

6847
let response = OneOfApi.Parent_Child("oneOf")
6948
response.ChildValue |> shouldEqual "oneOf"
7049

50+
let generatedTypeNames = getGeneratedTypeNames typeof<OneOfApi.Parent_Child>
51+
generatedTypeNames |> shouldNotContain "Parent"
52+
53+
generatedTypeNames
54+
|> Array.filter(fun typeName -> typeName = "Parent_Child")
55+
|> Array.length
56+
|> shouldEqual 1
57+
7158
[<Fact>]
7259
let ``anyOf named component alias response resolves to child object type``() =
73-
getResponseType anyOfClient
60+
getResponseType typeof<AnyOfApi.Client> "GetParent"
7461
|> shouldEqual typeof<AnyOfApi.Parent_Child>
7562

7663
let response = AnyOfApi.Parent_Child("anyOf")
7764
response.ChildValue |> shouldEqual "anyOf"
7865

7966
[<Fact>]
8067
let ``allOf named component alias response resolves to child object type``() =
81-
getResponseType allOfClient
68+
getResponseType typeof<AllOfApi.Client> "GetParent"
8269
|> shouldEqual typeof<AllOfApi.Parent_Child>
8370

8471
let response = AllOfApi.Parent_Child("allOf")
8572
response.ChildValue |> shouldEqual "allOf"
8673

8774
[<Fact>]
8875
let ``namespaced aliases with equal leaf names register the child object only once``() =
89-
getResponseType namespacedClient
76+
getResponseType typeof<NamespacedApi.Client> "GetParent"
9077
|> shouldEqual typeof<NamespacedApi.B.Parent>
9178

9279
let response = NamespacedApi.B.Parent("namespaced")
9380
response.Value |> shouldEqual "namespaced"
81+
82+
[<Fact>]
83+
let ``multiple aliases and alias chains resolve to their object types``() =
84+
getResponseType typeof<MultipleAliasesApi.Client> "GetParentA"
85+
|> shouldEqual typeof<MultipleAliasesApi.Child>
86+
87+
getResponseType typeof<MultipleAliasesApi.Client> "GetParentB"
88+
|> shouldEqual typeof<MultipleAliasesApi.Child>
89+
90+
getResponseType typeof<MultipleAliasesApi.Client> "GetAliasChain"
91+
|> shouldEqual typeof<MultipleAliasesApi.AliasC>
92+
93+
let child = MultipleAliasesApi.Child("shared")
94+
child.Value |> shouldEqual "shared"
95+
96+
let chained = MultipleAliasesApi.AliasC("chained")
97+
chained.ChainValue |> shouldEqual "chained"
98+
99+
let generatedTypeNames = getGeneratedTypeNames typeof<MultipleAliasesApi.Child>
100+
generatedTypeNames |> shouldNotContain "ParentA"
101+
generatedTypeNames |> shouldNotContain "ParentB"
102+
generatedTypeNames |> shouldNotContain "AliasA"
103+
generatedTypeNames |> shouldNotContain "AliasB"

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
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.NullableDate.Tests.fs" />
2019
<Compile Include="Swagger.NamedObjectAliases.Tests.fs" />
20+
<Compile Include="Swagger.NullableDate.Tests.fs" />
2121
<Compile Include="Swagger.SchemaReaderErrors.Tests.fs" />
2222
<Compile Include="Swashbuckle.ReturnControllers.Tests.fs" />
2323
<Compile Include="Swashbuckle.CancellationToken.Tests.fs" />

0 commit comments

Comments
 (0)