Skip to content

Commit 3df8ef9

Browse files
authored
Add Fullname option (#610)
1 parent 5926723 commit 3df8ef9

File tree

106 files changed

+726
-519
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

106 files changed

+726
-519
lines changed

src/Microsoft.Azure.Functions.Worker.Extensions.OpenApi/Document.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public class Document : IDocument
3737
private NamingStrategy _strategy;
3838
private VisitorCollection _collection;
3939
private IHttpRequestDataObject _req;
40+
private bool _useFullName;
4041

4142
/// <summary>
4243
/// Initializes a new instance of the <see cref="Document"/> class.
@@ -119,6 +120,14 @@ public IDocument AddNamingStrategy(NamingStrategy strategy)
119120
return this;
120121
}
121122

123+
/// <inheritdoc/>
124+
public IDocument AddFullNameOption(bool useFullName = false)
125+
{
126+
this._useFullName = useFullName;
127+
128+
return this;
129+
}
130+
122131
/// <inheritdoc />
123132
public IDocument AddVisitors(VisitorCollection collection)
124133
{
@@ -179,9 +188,9 @@ public IDocument Build(Assembly assembly, OpenApiVersionType version = OpenApiVe
179188
}
180189

181190
operation.Security = this._helper.GetOpenApiSecurityRequirement(method, this._strategy);
182-
operation.Parameters = this._helper.GetOpenApiParameters(method, trigger, this._strategy, this._collection, version);
183-
operation.RequestBody = this._helper.GetOpenApiRequestBody(method, this._strategy, this._collection, version);
184-
operation.Responses = this._helper.GetOpenApiResponses(method, this._strategy, this._collection, version);
191+
operation.Parameters = this._helper.GetOpenApiParameters(method, trigger, this._strategy, this._collection, version, this._useFullName);
192+
operation.RequestBody = this._helper.GetOpenApiRequestBody(method, this._strategy, this._collection, version, this._useFullName);
193+
operation.Responses = this._helper.GetOpenApiResponses(method, this._strategy, this._collection, version, this._useFullName);
185194

186195
operations[verb] = operation;
187196
item.Operations = operations;
@@ -190,7 +199,7 @@ public IDocument Build(Assembly assembly, OpenApiVersionType version = OpenApiVe
190199
}
191200

192201
this.OpenApiDocument.Paths = paths;
193-
this.OpenApiDocument.Components.Schemas = this._helper.GetOpenApiSchemas(methods, this._strategy, this._collection);
202+
this.OpenApiDocument.Components.Schemas = this._helper.GetOpenApiSchemas(methods, this._strategy, this._collection, this._useFullName);
194203
this.OpenApiDocument.Components.SecuritySchemes = this._helper.GetOpenApiSecuritySchemes(methods, this._strategy);
195204
// this.OpenApiDocument.SecurityRequirements = this.OpenApiDocument
196205
// .Paths

src/Microsoft.Azure.Functions.Worker.Extensions.OpenApi/Extensions/DocumentHelperExtensions.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,9 @@ public static OpenApiOperation GetOpenApiOperation(this IDocumentHelper helper,
152152
/// <param name="namingStrategy"><see cref="NamingStrategy"/> instance to create the JSON schema from .NET Types.</param>
153153
/// <param name="collection"><see cref="VisitorCollection"/> instance to process parameters.</param>
154154
/// <param name="version"><see cref="OpenApiVersionType"/> value.</param>
155+
/// <param name="useFullName"> instance to get or set the value indicating whether to use the FullName or not.</param>
155156
/// <returns>List of <see cref="OpenApiParameter"/> instance.</returns>
156-
public static List<OpenApiParameter> GetOpenApiParameters(this IDocumentHelper helper, MethodInfo element, HttpTriggerAttribute trigger, NamingStrategy namingStrategy, VisitorCollection collection, OpenApiVersionType version)
157+
public static List<OpenApiParameter> GetOpenApiParameters(this IDocumentHelper helper, MethodInfo element, HttpTriggerAttribute trigger, NamingStrategy namingStrategy, VisitorCollection collection, OpenApiVersionType version, bool useFullName = false)
157158
{
158159
var parameters = element.GetCustomAttributes<OpenApiParameterAttribute>(inherit: false)
159160
.Where(p => p.Deprecated == false)
@@ -178,14 +179,14 @@ public static List<OpenApiParameter> GetOpenApiParameters(this IDocumentHelper h
178179

179180
var contents = attributes.Where(p => p.Deprecated == false)
180181
.Where(p => p.ContentType == "application/x-www-form-urlencoded" || p.ContentType == "multipart/form-data")
181-
.Select(p => p.ToOpenApiMediaType(namingStrategy, collection, version));
182+
.Select(p => p.ToOpenApiMediaType(namingStrategy, collection, version, useFullName));
182183
if (!contents.Any())
183184
{
184185
return parameters;
185186
}
186187

187188
var @ref = contents.First().Schema.Reference;
188-
var schemas = helper.GetOpenApiSchemas(new[] { element }.ToList(), namingStrategy, collection);
189+
var schemas = helper.GetOpenApiSchemas(new[] { element }.ToList(), namingStrategy, collection, useFullName);
189190
var schema = schemas.SingleOrDefault(p => p.Key == @ref.Id);
190191
if (schema.IsNullOrDefault())
191192
{

src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/Abstractions/IDocument.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ public interface IDocument
5757
/// <returns><see cref="IDocument"/> instance.</returns>
5858
IDocument AddVisitors(VisitorCollection collection);
5959

60+
/// <summary>
61+
/// Add the useFullName options
62+
/// </summary>
63+
/// <param name="useFullName">instance to get or set the value indicating whether to use the FullName or not.</param>
64+
/// <returns>instance</returns>
65+
IDocument AddFullNameOption(bool useFullName = false);
66+
6067
/// <summary>
6168
/// Builds OpenAPI document.
6269
/// </summary>

src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/Abstractions/IDocumentHelper.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,19 @@ public interface IDocumentHelper
3838
/// <param name="namingStrategy"><see cref="NamingStrategy"/> instance to create the JSON schema from .NET Types.</param>
3939
/// <param name="collection"><see cref="VisitorCollection"/> instance to process parameters.</param>
4040
/// <param name="version">OpenAPI spec version.</param>
41+
/// <param name="useFullName">instance to get or set the value indicating whether to use the FullName or not.</param>
4142
/// <returns><see cref="OpenApiRequestBody"/> instance.</returns>
42-
OpenApiRequestBody GetOpenApiRequestBody(MethodInfo element, NamingStrategy namingStrategy, VisitorCollection collection, OpenApiVersionType version = OpenApiVersionType.V2);
43+
OpenApiRequestBody GetOpenApiRequestBody(MethodInfo element, NamingStrategy namingStrategy, VisitorCollection collection, OpenApiVersionType version = OpenApiVersionType.V2 , bool useFullName = false);
4344

4445
/// <summary>
4546
/// Gets the <see cref="OpenApiResponses"/> instance.
4647
/// </summary>
4748
/// <param name="element"><see cref="MethodInfo"/> instance.</param>
4849
/// <param name="namingStrategy"><see cref="NamingStrategy"/> instance to create the JSON schema from .NET Types.</param>
50+
/// <param name="useFullName">instance to get or set the value indicating whether to use the FullName or not.</param>
4951
/// <returns><see cref="OpenApiResponses"/> instance.</returns>
5052
[Obsolete("This method is obsolete from 2.0.0. Use GetOpenApiResponses instead", error: true)]
51-
OpenApiResponses GetOpenApiResponseBody(MethodInfo element, NamingStrategy namingStrategy = null);
53+
OpenApiResponses GetOpenApiResponseBody(MethodInfo element, NamingStrategy namingStrategy = null, bool useFullName = default);
5254

5355
/// <summary>
5456
/// Gets the <see cref="OpenApiResponses"/> instance.
@@ -57,17 +59,19 @@ public interface IDocumentHelper
5759
/// <param name="namingStrategy"><see cref="NamingStrategy"/> instance to create the JSON schema from .NET Types.</param>
5860
/// <param name="collection"><see cref="VisitorCollection"/> instance to process parameters.</param>
5961
/// <param name="version">OpenAPI spec version.</param>
62+
/// <param name="useFullName">instance to get or set the value indicating whether to use the FullName or not.</param>
6063
/// <returns><see cref="OpenApiResponses"/> instance.</returns>
61-
OpenApiResponses GetOpenApiResponses(MethodInfo element, NamingStrategy namingStrategy, VisitorCollection collection, OpenApiVersionType version = OpenApiVersionType.V2);
64+
OpenApiResponses GetOpenApiResponses(MethodInfo element, NamingStrategy namingStrategy, VisitorCollection collection, OpenApiVersionType version = OpenApiVersionType.V2, bool useFullName = false);
6265

6366
/// <summary>
6467
/// Gets the collection of <see cref="OpenApiSchema"/> instances.
6568
/// </summary>
6669
/// <param name="elements">List of <see cref="MethodInfo"/> instance.</param>
6770
/// <param name="namingStrategy"><see cref="NamingStrategy"/> instance to create the JSON schema from .NET Types.</param>
6871
/// <param name="collection"><see cref="VisitorCollection"/> instance to add types to schema.</param>
72+
/// <param name="useFullName">instance to get or set the value indicating whether to use the FullName or not.</param>
6973
/// /// <returns>Collection of <see cref="OpenApiSchema"/> instance.</returns>
70-
Dictionary<string, OpenApiSchema> GetOpenApiSchemas(List<MethodInfo> elements, NamingStrategy namingStrategy, VisitorCollection collection);
74+
Dictionary<string, OpenApiSchema> GetOpenApiSchemas(List<MethodInfo> elements, NamingStrategy namingStrategy, VisitorCollection collection, bool useFullName = false);
7175

7276
/// <summary>
7377
/// Gets the collection of <see cref="OpenApiSecurityScheme"/> instances.

src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/Abstractions/IOpenApiConfigurationOptions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ public interface IOpenApiConfigurationOptions
3939
/// Gets or sets the value indicating whether to force the HTTPS protocol or not.
4040
/// </summary>
4141
bool ForceHttps { get; set; }
42+
43+
/// <summary>
44+
/// Gets or sets the value indicating whether to use the FullName or not.
45+
/// </summary>
46+
bool UseFullName { get; set; }
4247

4348
/// <summary>
4449
/// Gets or sets the list of <see cref="IDocumentFilter"/> instances.

src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/Configurations/DefaultOpenApiConfigurationOptions.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public class DefaultOpenApiConfigurationOptions : OpenApiConfigurationOptions
2525
private const string ExcludeRequestingHostKey = "OpenApi__ExcludeRequestingHost";
2626
private const string ForceHttpKey = "OpenApi__ForceHttp";
2727
private const string ForceHttpsKey = "OpenApi__ForceHttps";
28+
private const string FullnameKey = "OpenApi__UseFullNamespace";
2829

2930
/// <inheritdoc />
3031
public override OpenApiInfo Info { get; set; } = new OpenApiInfo()
@@ -49,6 +50,9 @@ public class DefaultOpenApiConfigurationOptions : OpenApiConfigurationOptions
4950
/// <inheritdoc />
5051
public override bool ForceHttps { get; set; } = IsHttpsForced();
5152

53+
/// <inheritdoc />
54+
public override bool UseFullName {get; set; } = UseFullNamespace();
55+
5256
/// <inheritdoc />
5357
public override List<IDocumentFilter> DocumentFilters { get; set; } = new List<IDocumentFilter>();
5458

@@ -187,6 +191,15 @@ private static bool IsFunctionsRuntimeEnvironmentDevelopment()
187191
{
188192
var development = Environment.GetEnvironmentVariable(FunctionsRuntimeEnvironmentKey) == "Development";
189193

194+
return development;
195+
}
196+
/// <summary>
197+
/// check whether to use FullName or not
198+
/// </summary>
199+
/// <returns>Return <c>true</c>if user use Fullname; otherwise return<c>false</c> </returns>
200+
public static bool UseFullNamespace(){
201+
var development = bool.TryParse(Environment.GetEnvironmentVariable(FullnameKey), out var result) ? result : false;
202+
190203
return development;
191204
}
192205
}

src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/Configurations/OpenApiConfigurationOptions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ public class OpenApiConfigurationOptions : IOpenApiConfigurationOptions
3434
/// <inheritdoc />
3535
public virtual List<IDocumentFilter> DocumentFilters { get; set; } = new List<IDocumentFilter>();
3636

37+
/// <inheritdoc />
38+
public virtual bool UseFullName {get; set;}
3739
/// <inheritdoc />
3840
public virtual IOpenApiHttpTriggerAuthorization Security { get; set; } = new OpenApiHttpTriggerAuthorization();
3941
}

src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/Configurations/OpenApiSettings.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ public class OpenApiSettings
5252
/// </summary>
5353
public virtual bool ForceHttp { get; set; }
5454

55+
/// <summary>
56+
/// Gets or sets the value indicating whether to use the FullName or not.
57+
/// </summary>
58+
public bool UseFullName { get; set; }
59+
5560
/// <summary>
5661
/// Gets or sets the value indicating whether to hide the Swagger UI page or not.
5762
/// </summary>

src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/DocumentHelper.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public List<OpenApiSecurityRequirement> GetOpenApiSecurityRequirement(MethodInfo
7979
}
8080

8181
/// <inheritdoc />
82-
public OpenApiRequestBody GetOpenApiRequestBody(MethodInfo element, NamingStrategy namingStrategy, VisitorCollection collection, OpenApiVersionType version = OpenApiVersionType.V2)
82+
public OpenApiRequestBody GetOpenApiRequestBody(MethodInfo element, NamingStrategy namingStrategy, VisitorCollection collection, OpenApiVersionType version = OpenApiVersionType.V2, bool useFullName = false)
8383
{
8484
var attributes = element.GetCustomAttributes<OpenApiRequestBodyAttribute>(inherit: false);
8585
if (!attributes.Any())
@@ -88,7 +88,7 @@ public OpenApiRequestBody GetOpenApiRequestBody(MethodInfo element, NamingStrate
8888
}
8989

9090
var contents = attributes.Where(p => p.Deprecated == false)
91-
.ToDictionary(p => p.ContentType, p => p.ToOpenApiMediaType(namingStrategy, collection, version));
91+
.ToDictionary(p => p.ContentType, p => p.ToOpenApiMediaType(namingStrategy, collection, version, useFullName));
9292

9393
if (contents.Any())
9494
{
@@ -105,17 +105,17 @@ public OpenApiRequestBody GetOpenApiRequestBody(MethodInfo element, NamingStrate
105105

106106
/// <inheritdoc />
107107
[Obsolete("This method is obsolete from 2.0.0. Use GetOpenApiResponses instead", error: true)]
108-
public OpenApiResponses GetOpenApiResponseBody(MethodInfo element, NamingStrategy namingStrategy = null)
108+
public OpenApiResponses GetOpenApiResponseBody(MethodInfo element, NamingStrategy namingStrategy = null, bool useFullname = false)
109109
{
110-
return this.GetOpenApiResponses(element, namingStrategy, null);
110+
return this.GetOpenApiResponses(element, namingStrategy, null, useFullname:useFullname);
111111
}
112112

113113
/// <inheritdoc />
114-
public OpenApiResponses GetOpenApiResponses(MethodInfo element, NamingStrategy namingStrategy, VisitorCollection collection, OpenApiVersionType version = OpenApiVersionType.V2)
114+
public OpenApiResponses GetOpenApiResponses(MethodInfo element, NamingStrategy namingStrategy, VisitorCollection collection, OpenApiVersionType version = OpenApiVersionType.V2, bool useFullname = false)
115115
{
116116
var responsesWithBody = element.GetCustomAttributes<OpenApiResponseWithBodyAttribute>(inherit: false)
117117
.Where(p => p.Deprecated == false)
118-
.Select(p => new { StatusCode = p.StatusCode, Response = p.ToOpenApiResponse(namingStrategy, version: version) });
118+
.Select(p => new { StatusCode = p.StatusCode, Response = p.ToOpenApiResponse(namingStrategy, version: version, useFullName: useFullname) });
119119

120120
var responsesWithoutBody = element.GetCustomAttributes<OpenApiResponseWithoutBodyAttribute>(inherit: false)
121121
.Select(p => new { StatusCode = p.StatusCode, Response = p.ToOpenApiResponse(namingStrategy) });
@@ -128,7 +128,7 @@ public OpenApiResponses GetOpenApiResponses(MethodInfo element, NamingStrategy n
128128
}
129129

130130
/// <inheritdoc />
131-
public Dictionary<string, OpenApiSchema> GetOpenApiSchemas(List<MethodInfo> elements, NamingStrategy namingStrategy, VisitorCollection collection)
131+
public Dictionary<string, OpenApiSchema> GetOpenApiSchemas(List<MethodInfo> elements, NamingStrategy namingStrategy, VisitorCollection collection, bool useFullName = false)
132132
{
133133
var requests = elements.SelectMany(p => p.GetCustomAttributes<OpenApiRequestBodyAttribute>(inherit: false))
134134
.Select(p => p.BodyType);
@@ -148,7 +148,7 @@ public Dictionary<string, OpenApiSchema> GetOpenApiSchemas(List<MethodInfo> elem
148148
var acceptorTypes = new Dictionary<string, Type>();
149149
foreach (var type in types)
150150
{
151-
var openApiReferenceId = type.GetOpenApiReferenceId(type.IsOpenApiDictionary(), type.IsOpenApiArray(), namingStrategy);
151+
var openApiReferenceId = type.GetOpenApiReferenceId(type.IsOpenApiDictionary(), type.IsOpenApiArray(), namingStrategy, useFullName);
152152
if (!acceptorTypes.ContainsKey(openApiReferenceId))
153153
{
154154
acceptorTypes.Add(openApiReferenceId, type);
@@ -159,7 +159,7 @@ public Dictionary<string, OpenApiSchema> GetOpenApiSchemas(List<MethodInfo> elem
159159
this._acceptor.RootSchemas = rootSchemas;
160160
this._acceptor.Schemas = schemas;
161161

162-
this._acceptor.Accept(collection, namingStrategy);
162+
this._acceptor.Accept(collection, namingStrategy, useFullName);
163163

164164
var union = schemas.Concat(rootSchemas.Where(p => !schemas.Keys.Contains(p.Key)))
165165
.Distinct()

src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/Extensions/DocumentHelperExtensions.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,9 @@ public static OpenApiOperation GetOpenApiOperation(this IDocumentHelper helper,
147147
/// <param name="namingStrategy"><see cref="NamingStrategy"/> instance to create the JSON schema from .NET Types.</param>
148148
/// <param name="collection"><see cref="VisitorCollection"/> instance to process parameters.</param>
149149
/// <param name="version"><see cref="OpenApiVersionType"/> value.</param>
150+
/// <param name="useFullName">instance to get or set the value indicating whether to use the FullName or not.</param>
150151
/// <returns>List of <see cref="OpenApiParameter"/> instance.</returns>
151-
public static List<OpenApiParameter> GetOpenApiParameters(this IDocumentHelper helper, MethodInfo element, HttpTriggerAttribute trigger, NamingStrategy namingStrategy, VisitorCollection collection, OpenApiVersionType version)
152+
public static List<OpenApiParameter> GetOpenApiParameters(this IDocumentHelper helper, MethodInfo element, HttpTriggerAttribute trigger, NamingStrategy namingStrategy, VisitorCollection collection, OpenApiVersionType version, bool useFullName = false)
152153
{
153154
var parameters = element.GetCustomAttributes<OpenApiParameterAttribute>(inherit: false)
154155
.Where(p => p.Deprecated == false)
@@ -173,14 +174,14 @@ public static List<OpenApiParameter> GetOpenApiParameters(this IDocumentHelper h
173174

174175
var contents = attributes.Where(p => p.Deprecated == false)
175176
.Where(p => p.ContentType == "application/x-www-form-urlencoded" || p.ContentType == "multipart/form-data")
176-
.Select(p => p.ToOpenApiMediaType(namingStrategy, collection, version));
177+
.Select(p => p.ToOpenApiMediaType(namingStrategy, collection, version, useFullName));
177178
if (!contents.Any())
178179
{
179180
return parameters;
180181
}
181182

182183
var @ref = contents.First().Schema.Reference;
183-
var schemas = helper.GetOpenApiSchemas(new[] { element }.ToList(), namingStrategy, collection);
184+
var schemas = helper.GetOpenApiSchemas(new[] { element }.ToList(), namingStrategy, collection, useFullName);
184185
var schema = schemas.SingleOrDefault(p => p.Key == @ref.Id);
185186
if (schema.IsNullOrDefault())
186187
{

0 commit comments

Comments
 (0)