Skip to content

Commit

Permalink
added multiple clients from first tag and operation id generator (#2169)
Browse files Browse the repository at this point in the history
  • Loading branch information
Place1 authored and RicoSuter committed May 15, 2019
1 parent c1d0907 commit 7732208
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
@@ -0,0 +1,43 @@
//-----------------------------------------------------------------------
// <copyright file="MultipleClientsFromFirstTagAndOperationIdGenerator.cs" company="NSwag">
// Copyright (c) Rico Suter. All rights reserved.
// </copyright>
// <license>https://github.com/NSwag/NSwag/blob/master/LICENSE.md</license>
// <author>Rico Suter, mail@rsuter.com</author>
//-----------------------------------------------------------------------

using NJsonSchema;
using System.Linq;

namespace NSwag.CodeGeneration.OperationNameGenerators
{
/// <summary>Generates the client name based on the first tag and operation name based on the operation id (operation name = operationId, client name = first tag).</summary>
public class MultipleClientsFromFirstTagAndOperationIdGenerator : IOperationNameGenerator
{
/// <summary>Gets a value indicating whether the generator supports multiple client classes.</summary>
public bool SupportsMultipleClients { get; } = true;

/// <summary>Gets the client name for a given operation (may be empty).</summary>
/// <param name="document">The Swagger document.</param>
/// <param name="path">The HTTP path.</param>
/// <param name="httpMethod">The HTTP method.</param>
/// <param name="operation">The operation.</param>
/// <returns>The client name.</returns>
public virtual string GetClientName(SwaggerDocument document, string path, string httpMethod, SwaggerOperation operation)
{
return ConversionUtilities.ConvertToUpperCamelCase(operation.Tags.FirstOrDefault(), false);
}

/// <summary>Gets the operation name for a given operation.</summary>
/// <param name="document">The Swagger document.</param>
/// <param name="path">The HTTP path.</param>
/// <param name="httpMethod">The HTTP method.</param>
/// <param name="operation">The operation.</param>
/// <returns>The operation name.</returns>
public virtual string GetOperationName(SwaggerDocument document, string path, string httpMethod, SwaggerOperation operation)
{
var operationName = operation.OperationId;
return operationName;
}
}
}
Expand Up @@ -20,10 +20,13 @@ public enum OperationGenerationMode
/// <summary>From the first operation tag and path segments (operation name = last segment, client name = first operation tag).</summary> /// <summary>From the first operation tag and path segments (operation name = last segment, client name = first operation tag).</summary>
MultipleClientsFromFirstTagAndPathSegments, MultipleClientsFromFirstTagAndPathSegments,


/// <summary>From the first operation tag and operation ID (operation name = operation ID, client name = first operation tag).</summary>
MultipleClientsFromFirstTagAndOperationId,

/// <summary>From the Swagger operation ID.</summary> /// <summary>From the Swagger operation ID.</summary>
SingleClientFromOperationId, SingleClientFromOperationId,


/// <summary>From path segments suffixed by HTTP operation name</summary> /// <summary>From path segments suffixed by HTTP operation name</summary>
SingleClientFromPathSegments, SingleClientFromPathSegments,
} }
} }
4 changes: 4 additions & 0 deletions src/NSwag.Commands/OperationGenerationModeConverter.cs
Expand Up @@ -21,6 +21,8 @@ internal static OperationGenerationMode GetOperationGenerationMode(IOperationNam
return OperationGenerationMode.MultipleClientsFromPathSegments; return OperationGenerationMode.MultipleClientsFromPathSegments;
if (operationNameGenerator is MultipleClientsFromFirstTagAndPathSegmentsOperationNameGenerator) if (operationNameGenerator is MultipleClientsFromFirstTagAndPathSegmentsOperationNameGenerator)
return OperationGenerationMode.MultipleClientsFromFirstTagAndPathSegments; return OperationGenerationMode.MultipleClientsFromFirstTagAndPathSegments;
if (operationNameGenerator is MultipleClientsFromFirstTagAndOperationIdGenerator)
return OperationGenerationMode.MultipleClientsFromFirstTagAndOperationId;
if (operationNameGenerator is SingleClientFromOperationIdOperationNameGenerator) if (operationNameGenerator is SingleClientFromOperationIdOperationNameGenerator)
return OperationGenerationMode.SingleClientFromOperationId; return OperationGenerationMode.SingleClientFromOperationId;
if (operationNameGenerator is SingleClientFromPathSegmentsOperationNameGenerator) if (operationNameGenerator is SingleClientFromPathSegmentsOperationNameGenerator)
Expand All @@ -36,6 +38,8 @@ internal static IOperationNameGenerator GetOperationNameGenerator(OperationGener
return new MultipleClientsFromPathSegmentsOperationNameGenerator(); return new MultipleClientsFromPathSegmentsOperationNameGenerator();
else if (operationGenerationMode == OperationGenerationMode.MultipleClientsFromFirstTagAndPathSegments) else if (operationGenerationMode == OperationGenerationMode.MultipleClientsFromFirstTagAndPathSegments)
return new MultipleClientsFromFirstTagAndPathSegmentsOperationNameGenerator(); return new MultipleClientsFromFirstTagAndPathSegmentsOperationNameGenerator();
else if (operationGenerationMode == OperationGenerationMode.MultipleClientsFromFirstTagAndOperationId)
return new MultipleClientsFromFirstTagAndOperationIdGenerator();
else if (operationGenerationMode == OperationGenerationMode.SingleClientFromOperationId) else if (operationGenerationMode == OperationGenerationMode.SingleClientFromOperationId)
return new SingleClientFromOperationIdOperationNameGenerator(); return new SingleClientFromOperationIdOperationNameGenerator();
else if (operationGenerationMode == OperationGenerationMode.SingleClientFromPathSegments) else if (operationGenerationMode == OperationGenerationMode.SingleClientFromPathSegments)
Expand Down

0 comments on commit 7732208

Please sign in to comment.