2017-11-08 10 views
0

私はSwashbuckleを使用して.NETアセンブリのスワッガーを生成しています。私は、非順次的な値と負の値を持つenumを持っています。タイトルと値のスワッガー

[JsonConverter(typeof(StringEnumConverter))] 
public enum ShiftDayOffRule 
{ 
    /// <summary> 
    /// Shift has no day off rule. 
    /// </summary> 
    None = 0,  // DayOffNotRequired 

    /// <summary> 
    /// If shift with this rule is scheduled then next day must be a day off. 
    /// </summary> 
    OffAfter = 1, // DayOffAfter <=> next is off 

    /// <summary> 
    /// If shift with this rule is scheduled then previous day must be a day off. 
    /// </summary> 
    OffBefore = -1, // DayOffBefore <=> previous is off 

    /// <summary> 
    /// If shift with this rule is scheduled then next day must be a work day. 
    /// </summary> 
    InAfter = 3  // DayInAfter <=> next cannot be off 
}; 

次の値は、私はタイトルと値を含めるために、出力JSONを得るのですか代わりに0、1、-1、3

どのように、0に並べ替え1、2、3なっているようにlike:

"dayoffRule": { 
    "description": "Day off rule for the shift. ScheduleData.Enums.ShiftDayOffRule", 
    "enum": [ 
     {"title": "None", "value": 0}, 
     {"title": "OffAfter", "value": 1}, 
     {"title": "InAfter", "value": 3}, 
     {"title": "OffBefore", "value": -1} 
    ], 
    "type": "string" 
} 
+0

にどのように見えるかです。 Re:enum値のラベルを指定する - OpenAPI仕様はこれをサポートしていません。関連機能要求:[#348](https://github.com/OAI/OpenAPI-Specification/issues/348)、[#681](https://github.com/OAI/OpenAPI-Specification/issues/681) )。 – Helen

答えて

1

設定でDescribeAllEnumsAsStringsを有効にします。
このように見えるようにあなたの列挙型を変更します:あなたはタイトルと値の両方を必要としない

"parameters": [ 
     { 
     "name": "dayOffRule", 
     "in": "query", 
     "required": true, 
     "type": "string", 
     "enum": [ 
      "None", 
      "OffAfter", 
      "OffBefore", 
      "InAfter" 
     ] 
     } 
    ], 

...

ここでは、ライブのサンプルです:
http://swashbuckletest.azurewebsites.net/swagger/ui/index?filter=Def#/Default/Default_Post



マッピング(タイトル:値)を絶対に含める必要がある場合は、SchemaFilterを使用してサンプルに注入することができます。コードは

です
private class EnumExampleSchemaFilter : ISchemaFilter 
    { 
     public void Apply(Schema schema, SchemaRegistry schemaRegistry, Type type) 
     { 
      if (type == typeof(ShiftDayOffRule)) 
      { 
       var example = new Dictionary<string, int>(); 
       foreach (var item in Enum.GetValues(typeof(ShiftDayOffRule))) 
       { 
        example.Add(item.ToString(), (int)item); 
       } 
       schema.example = example; 
      } 
     } 
    } 

そして、ここではそれが闊歩ドキュメントの並べ替えがSwashbuckleのバグかもしれない

"Value": { 
     "enum": [ 
     "None", 
     "OffAfter", 
     "OffBefore", 
     "InAfter" 
     ], 
     "type": "string", 
     "readOnly": true, 
     "example": { 
     "None": 0, 
     "OffAfter": 1, 
     "InAfter": 3, 
     "OffBefore": -1 
     } 
    } 
+0

'EnumExampleSchemaFilter'クラスはどこに置くべきですか? 'ISchemaFilter'のリファレンスを追加する必要がありますか? – Alex

+0

EnumExampleSchemaFilterはあなたの設定に行きます:https://github.com/heldersepu/SwashbuckleTest/blob/master/Swagger_Test/App_Start/SwaggerConfig.cs#L140 – HelderSepu

+0

私が最終的にしようとしているのは-1 enumの有効な値この解決策では、デフォルトで値が0,1,2,3に戻されます。 'c.DescribeAllEnumsAsStrings();を削除すると、generate enumに_1が2回あるため、ビルドエラーが発生します。 1回1回、-1回1回。 – Alex

関連する問題