2

WebAPI 2 및 OWIN을 사용하여 하나의 웹 서비스를 개발 중입니다. 내 목표는 Swashbuckle과 함께 문서를 추가하는 것입니다. 내 AttributeDto 클래스 코드 아래Swagger UI에서 내 요청/응답에 대한 예제 데이터를 어떻게 제공합니까?

/// <summary> 
/// La mia descrizione... 
/// </summary> 
/// <param name="id">Identificativo</param> 
/// <param name="list">Una lista</param> 
[HttpPut] 
[Route("~/api/v1/documents/{id}/attribute")] 
public IHttpActionResult Put(int id, List<AttributeDto> list) 
{ 
    _myService.Create(list, id); 
    return Ok(); 
} 

:

Swagger UI autogenerated data example.. : 나는 자신감 UI를 열면

using Newtonsoft.Json; 

namespace WebApi.Dtos 
{ 
    public class AttributeDto 
    { 
     [JsonIgnore] 
     public int Id { get; set; } 

     [JsonIgnore] 
     public int OtherId { get; set; } 

     public string Label { get; set; } 

     public string Value { get; set; } 
    } 
} 

내가 자동 생성 된 데이터를 하나 개의 예시적인 부분을 볼 수 있습니다 내 방법에 따라

[ 
    { 
    "label": "Etichetta di esempio", 
    "value": "Valore di esempio" 
    } 
] 

답변

1

내가 대신 swashbuckle의 Swagger-Net를 사용을 권장합니다 : 그림이 아래와 같이됩니다에서 JSON 있도록어떻게 자동 생성 된 데이터를 사용자 정의 할 수 있습니다.

public class AttributeDto 
{ 
    [JsonIgnore] 
    public int Id { get; set; } 

    [JsonIgnore] 
    public int OtherId { get; set; } 

    /// <example>Etichetta di esempio</example> 
    public string Label { get; set; } 

    /// <example>Valore di esempio</example> 
    public string Value { get; set; } 
} 

이이 swashbuckle 제안했지만 아직 병합되지 않았습니다있다 :
https://github.com/domaindrivendev/Swashbuckle/pull/1091
자신감-순이다 당신이해야 할 모든이 같은 XML 예제 주석으로 정의 속성을 장식하다 내 Swashbuckle 포크지만, 많은 좋은 기능을 합병하고 많은 버그를 수정했습니다.

0

의 옵션은 ISchemaFilter을 (즉, SwaggerConfig.cs에 간다), 여기에 예입니다 사용하는 것입니다 : 우리가 정보를 유지해야하기 때문에 내가 개인적으로 솔루션을 사랑하지 않는

private class ApplySchemaVendorExtensions : ISchemaFilter 
{ 
    public void Apply(Schema schema, SchemaRegistry schemaRegistry, Type type) 
    { 
     if (schema.properties != null) 
     { 
      foreach (var p in schema.properties) 
      { 
       switch (p.Key) 
       { 
        case "label": 
         p.Value.example = "Etichetta di esempio"; 
         break; 
        case "value": 
         p.Value.example = "Valore di esempio"; 
         break; 
       } 
       break; 
      } 
     } 
    } 
} 

두 개의 다른 파일에있는 클래스 ...