Skip to content

Commit f54eb43

Browse files
Refactor OpenAPI extensions as "links" nested property instead of array to allow future extensions
1 parent 7f79ad1 commit f54eb43

File tree

4 files changed

+53
-44
lines changed

4 files changed

+53
-44
lines changed

src/AspNetCore/WebApi/src/Asp.Versioning.OpenApi/Transformers/ApiExplorerTransformer.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,8 +301,11 @@ private void AddLinkExtensions( OpenApiDocument document, ApiVersionDescription
301301

302302
if ( array.Count > 0 )
303303
{
304+
var obj = new JsonObject();
304305
var extensions = document.Extensions ??= new Dictionary<string, IOpenApiExtension>();
305-
extensions[ExtensionName] = new JsonNodeExtension( array );
306+
307+
obj["links"] = array;
308+
extensions[ExtensionName] = new JsonNodeExtension( obj );
306309
}
307310
}
308311

@@ -347,7 +350,7 @@ protected virtual JsonObject ToJson( LinkHeaderValue link )
347350

348351
if ( link.Languages.Count > 0 )
349352
{
350-
obj["lang"] = new JsonArray( link.Languages.Select( l => JsonNode.Parse( l.ToString() ) ).ToArray() );
353+
obj["lang"] = new JsonArray( [.. link.Languages.Select( l => JsonNode.Parse( l.ToString() ) )] );
351354
}
352355

353356
foreach ( var (key, value) in link.Extensions )

src/AspNetCore/WebApi/test/Asp.Versioning.OpenApi.Tests/Content/v1-minimal.json

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -70,18 +70,20 @@
7070
"name": "Test"
7171
}
7272
],
73-
"x-api-versioning": [
74-
{
75-
"title": "Version Deprecation Policy",
76-
"type": "text/html",
77-
"rel": "deprecation",
78-
"url": "http://my.api.com/policies/versions/deprecated.html"
79-
},
80-
{
81-
"title": "Version Sunset Policy",
82-
"type": "text/html",
83-
"rel": "sunset",
84-
"url": "http://my.api.com/policies/versions/sunset.html"
85-
}
86-
]
73+
"x-api-versioning": {
74+
"links": [
75+
{
76+
"title": "Version Deprecation Policy",
77+
"type": "text/html",
78+
"rel": "deprecation",
79+
"url": "http://my.api.com/policies/versions/deprecated.html"
80+
},
81+
{
82+
"title": "Version Sunset Policy",
83+
"type": "text/html",
84+
"rel": "sunset",
85+
"url": "http://my.api.com/policies/versions/sunset.html"
86+
}
87+
]
88+
}
8789
}

src/AspNetCore/WebApi/test/Asp.Versioning.OpenApi.Tests/Content/v1-mixed.json

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -142,18 +142,20 @@
142142
"name": "Test"
143143
}
144144
],
145-
"x-api-versioning": [
146-
{
147-
"title": "Version Deprecation Policy",
148-
"type": "text/html",
149-
"rel": "deprecation",
150-
"url": "http://my.api.com/policies/versions/deprecated.html"
151-
},
152-
{
153-
"title": "Version Sunset Policy",
154-
"type": "text/html",
155-
"rel": "sunset",
156-
"url": "http://my.api.com/policies/versions/sunset.html"
157-
}
158-
]
145+
"x-api-versioning": {
146+
"links": [
147+
{
148+
"title": "Version Deprecation Policy",
149+
"type": "text/html",
150+
"rel": "deprecation",
151+
"url": "http://my.api.com/policies/versions/deprecated.html"
152+
},
153+
{
154+
"title": "Version Sunset Policy",
155+
"type": "text/html",
156+
"rel": "sunset",
157+
"url": "http://my.api.com/policies/versions/sunset.html"
158+
}
159+
]
160+
}
159161
}

src/AspNetCore/WebApi/test/Asp.Versioning.OpenApi.Tests/Content/v1.json

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -89,18 +89,20 @@
8989
"name": "Test"
9090
}
9191
],
92-
"x-api-versioning": [
93-
{
94-
"title": "Version Deprecation Policy",
95-
"type": "text/html",
96-
"rel": "deprecation",
97-
"url": "http://my.api.com/policies/versions/deprecated.html"
98-
},
99-
{
100-
"title": "Version Sunset Policy",
101-
"type": "text/html",
102-
"rel": "sunset",
103-
"url": "http://my.api.com/policies/versions/sunset.html"
104-
}
105-
]
92+
"x-api-versioning": {
93+
"links": [
94+
{
95+
"title": "Version Deprecation Policy",
96+
"type": "text/html",
97+
"rel": "deprecation",
98+
"url": "http://my.api.com/policies/versions/deprecated.html"
99+
},
100+
{
101+
"title": "Version Sunset Policy",
102+
"type": "text/html",
103+
"rel": "sunset",
104+
"url": "http://my.api.com/policies/versions/sunset.html"
105+
}
106+
]
107+
}
106108
}

0 commit comments

Comments
 (0)