Skip to content

Modify UseFullname to be available in Outproc #620

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 21 commits into
base: v2
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class Document : IDocument
private NamingStrategy _strategy;
private VisitorCollection _collection;
private IHttpRequestDataObject _req;
private bool _useFullName;

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

/// <inheritdoc/>
public IDocument AddFullNameOption(bool useFullName = false)
{
this._useFullName = useFullName;

return this;
}

/// <inheritdoc />
public IDocument AddVisitors(VisitorCollection collection)
{
Expand Down Expand Up @@ -179,9 +188,9 @@ public IDocument Build(Assembly assembly, OpenApiVersionType version = OpenApiVe
}

operation.Security = this._helper.GetOpenApiSecurityRequirement(method, this._strategy);
operation.Parameters = this._helper.GetOpenApiParameters(method, trigger, this._strategy, this._collection, version);
operation.RequestBody = this._helper.GetOpenApiRequestBody(method, this._strategy, this._collection, version);
operation.Responses = this._helper.GetOpenApiResponses(method, this._strategy, this._collection, version);
operation.Parameters = this._helper.GetOpenApiParameters(method, trigger, this._strategy, this._collection, version, this._useFullName);
operation.RequestBody = this._helper.GetOpenApiRequestBody(method, this._strategy, this._collection, version, this._useFullName);
operation.Responses = this._helper.GetOpenApiResponses(method, this._strategy, this._collection, version, this._useFullName);

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

this.OpenApiDocument.Paths = paths;
this.OpenApiDocument.Components.Schemas = this._helper.GetOpenApiSchemas(methods, this._strategy, this._collection);
this.OpenApiDocument.Components.Schemas = this._helper.GetOpenApiSchemas(methods, this._strategy, this._collection, this._useFullName);
this.OpenApiDocument.Components.SecuritySchemes = this._helper.GetOpenApiSecuritySchemes(methods, this._strategy);
// this.OpenApiDocument.SecurityRequirements = this.OpenApiDocument
// .Paths
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,9 @@ public static OpenApiOperation GetOpenApiOperation(this IDocumentHelper helper,
/// <param name="namingStrategy"><see cref="NamingStrategy"/> instance to create the JSON schema from .NET Types.</param>
/// <param name="collection"><see cref="VisitorCollection"/> instance to process parameters.</param>
/// <param name="version"><see cref="OpenApiVersionType"/> value.</param>
/// <param name="useFullName"> instance to get or set the value indicating whether to use the FullName or not.</param>
/// <returns>List of <see cref="OpenApiParameter"/> instance.</returns>
public static List<OpenApiParameter> GetOpenApiParameters(this IDocumentHelper helper, MethodInfo element, HttpTriggerAttribute trigger, NamingStrategy namingStrategy, VisitorCollection collection, OpenApiVersionType version)
public static List<OpenApiParameter> GetOpenApiParameters(this IDocumentHelper helper, MethodInfo element, HttpTriggerAttribute trigger, NamingStrategy namingStrategy, VisitorCollection collection, OpenApiVersionType version, bool useFullName = false)
{
var parameters = element.GetCustomAttributes<OpenApiParameterAttribute>(inherit: false)
.Where(p => p.Deprecated == false)
Expand All @@ -178,14 +179,14 @@ public static List<OpenApiParameter> GetOpenApiParameters(this IDocumentHelper h

var contents = attributes.Where(p => p.Deprecated == false)
.Where(p => p.ContentType == "application/x-www-form-urlencoded" || p.ContentType == "multipart/form-data")
.Select(p => p.ToOpenApiMediaType(namingStrategy, collection, version));
.Select(p => p.ToOpenApiMediaType(namingStrategy, collection, version, useFullName));
if (!contents.Any())
{
return parameters;
}

var @ref = contents.First().Schema.Reference;
var schemas = helper.GetOpenApiSchemas(new[] { element }.ToList(), namingStrategy, collection);
var schemas = helper.GetOpenApiSchemas(new[] { element }.ToList(), namingStrategy, collection, useFullName);
var schema = schemas.SingleOrDefault(p => p.Key == @ref.Id);
if (schema.IsNullOrDefault())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public async Task<HttpResponseData> RenderSwaggerDocument(HttpRequestData req, s
.AddMetadata(this._context.OpenApiConfigurationOptions.Info)
.AddServer(request, this._context.HttpSettings.RoutePrefix, this._context.OpenApiConfigurationOptions)
.AddNamingStrategy(this._context.NamingStrategy)
.AddFullNameOption(this._context.OpenApiConfigurationOptions.UseFullName)
.AddVisitors(this._context.GetVisitorCollection())
.Build(this._context.ApplicationAssembly, this._context.OpenApiConfigurationOptions.OpenApiVersion)
.ApplyDocumentFilters(this._context.GetDocumentFilterCollection())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ public interface IDocument
/// <returns><see cref="IDocument"/> instance.</returns>
IDocument AddVisitors(VisitorCollection collection);

/// <summary>
/// Add the useFullName options
/// </summary>
/// <param name="useFullName">instance to get or set the value indicating whether to use the FullName or not.</param>
/// <returns>instance</returns>
IDocument AddFullNameOption(bool useFullName = false);

/// <summary>
/// Builds OpenAPI document.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,19 @@ public interface IDocumentHelper
/// <param name="namingStrategy"><see cref="NamingStrategy"/> instance to create the JSON schema from .NET Types.</param>
/// <param name="collection"><see cref="VisitorCollection"/> instance to process parameters.</param>
/// <param name="version">OpenAPI spec version.</param>
/// <param name="useFullName">instance to get or set the value indicating whether to use the FullName or not.</param>
/// <returns><see cref="OpenApiRequestBody"/> instance.</returns>
OpenApiRequestBody GetOpenApiRequestBody(MethodInfo element, NamingStrategy namingStrategy, VisitorCollection collection, OpenApiVersionType version = OpenApiVersionType.V2);
OpenApiRequestBody GetOpenApiRequestBody(MethodInfo element, NamingStrategy namingStrategy, VisitorCollection collection, OpenApiVersionType version = OpenApiVersionType.V2 , bool useFullName = false);

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

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

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

/// <summary>
/// Gets the collection of <see cref="OpenApiSecurityScheme"/> instances.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ public interface IOpenApiConfigurationOptions
/// Gets or sets the value indicating whether to force the HTTPS protocol or not.
/// </summary>
bool ForceHttps { get; set; }

/// <summary>
/// Gets or sets the value indicating whether to use the FullName or not.
/// </summary>
bool UseFullName { get; set; }

/// <summary>
/// Gets or sets the list of <see cref="IDocumentFilter"/> instances.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class DefaultOpenApiConfigurationOptions : OpenApiConfigurationOptions
private const string ExcludeRequestingHostKey = "OpenApi__ExcludeRequestingHost";
private const string ForceHttpKey = "OpenApi__ForceHttp";
private const string ForceHttpsKey = "OpenApi__ForceHttps";
private const string FullnameKey = "OpenApi__UseFullNamespace";

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

/// <inheritdoc />
public override bool UseFullName {get; set; } = UseFullNamespace();

/// <inheritdoc />
public override List<IDocumentFilter> DocumentFilters { get; set; } = new List<IDocumentFilter>();

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

return development;
}
/// <summary>
/// check whether to use FullName or not
/// </summary>
/// <returns>Return <c>true</c>if user use Fullname; otherwise return<c>false</c> </returns>
public static bool UseFullNamespace(){
var development = bool.TryParse(Environment.GetEnvironmentVariable(FullnameKey), out var result) ? result : false;

return development;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public class OpenApiConfigurationOptions : IOpenApiConfigurationOptions
/// <inheritdoc />
public virtual List<IDocumentFilter> DocumentFilters { get; set; } = new List<IDocumentFilter>();

/// <inheritdoc />
public virtual bool UseFullName {get; set;}
/// <inheritdoc />
public virtual IOpenApiHttpTriggerAuthorization Security { get; set; } = new OpenApiHttpTriggerAuthorization();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ public class OpenApiSettings
/// </summary>
public virtual bool ForceHttp { get; set; }

/// <summary>
/// Gets or sets the value indicating whether to use the FullName or not.
/// </summary>
public bool UseFullName { get; set; }

/// <summary>
/// Gets or sets the value indicating whether to hide the Swagger UI page or not.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public List<OpenApiSecurityRequirement> GetOpenApiSecurityRequirement(MethodInfo
}

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

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

if (contents.Any())
{
Expand All @@ -105,17 +105,17 @@ public OpenApiRequestBody GetOpenApiRequestBody(MethodInfo element, NamingStrate

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

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

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

/// <inheritdoc />
public Dictionary<string, OpenApiSchema> GetOpenApiSchemas(List<MethodInfo> elements, NamingStrategy namingStrategy, VisitorCollection collection)
public Dictionary<string, OpenApiSchema> GetOpenApiSchemas(List<MethodInfo> elements, NamingStrategy namingStrategy, VisitorCollection collection, bool useFullName = false)
{
var requests = elements.SelectMany(p => p.GetCustomAttributes<OpenApiRequestBodyAttribute>(inherit: false))
.Select(p => p.BodyType);
Expand All @@ -148,7 +148,7 @@ public Dictionary<string, OpenApiSchema> GetOpenApiSchemas(List<MethodInfo> elem
var acceptorTypes = new Dictionary<string, Type>();
foreach (var type in types)
{
var openApiReferenceId = type.GetOpenApiReferenceId(type.IsOpenApiDictionary(), type.IsOpenApiArray(), namingStrategy);
var openApiReferenceId = type.GetOpenApiReferenceId(type.IsOpenApiDictionary(), type.IsOpenApiArray(), namingStrategy, useFullName);
if (!acceptorTypes.ContainsKey(openApiReferenceId))
{
acceptorTypes.Add(openApiReferenceId, type);
Expand All @@ -159,7 +159,7 @@ public Dictionary<string, OpenApiSchema> GetOpenApiSchemas(List<MethodInfo> elem
this._acceptor.RootSchemas = rootSchemas;
this._acceptor.Schemas = schemas;

this._acceptor.Accept(collection, namingStrategy);
this._acceptor.Accept(collection, namingStrategy, useFullName);

var union = schemas.Concat(rootSchemas.Where(p => !schemas.Keys.Contains(p.Key)))
.Distinct()
Expand Down
Loading