2017-09-08 2 views
1

전화를 숨길 때 Swashbuckle에 문제가 있습니다. 호출에 연결된 모델의 정의가 JSON의 정의에 남아 있습니다.Swashbuckle 참조하지 않은 모델 숨기기

문서 필터를 사용하여 인터페이스에서 호출을 제거 할 수 있습니다.

호출은 생성 된 JSON에 남아 있지만 Swagger UI에서는 보이지 않습니다. 또한 Theses 호출에 연결된 Model 및 Enum의 정의를 볼 수 있습니다.

이 문서는 내부 전화이며 JSON의 외부 눈에서 숨길 필요가 있습니다.

모든 통화와 참조를 숨기려면 어떻게해야합니까?

[ApiExplorerSettings (IgnoreApi = true)]를 사용하면 문제가 해결되지만 기존 속성으로 필터링해야합니다.

답변

2

그래서 열심히 내 질문에 대한 답을 찾았습니다. 다음 호는 내 이슈가있는 사람이 나에게 더 좋은 시간을 줄 수 있도록 여기에 넣을 것입니다. DocumentFilter를에서 그런

public static class SwashbuckleExtensions 
{ 
    public static IEnumerable<Operation> EnumerateOperations(this PathItem pathItem) 
    { 
     if (pathItem == null) 
     { 
      yield break; 
     } 
     yield return pathItem.get; 
     yield return pathItem.post; 
     yield return pathItem.put; 
     yield return pathItem.delete; 
     yield return pathItem.options; 
     yield return pathItem.head; 
    } 


    public static IEnumerable<Schema> EnumerateSchema(this Operation operation) 
    { 
     if (operation == null) 
     { 
      yield break; 
     } 
        foreach (var response in operation.responses ?? new Dictionary<string, Response>()) 
     { 
      yield return response.Value.schema; 
      if (response.Value.schema.items != null) 
      { 
       yield return response.Value.schema.items; 
      } 
     } 
     foreach (var parameter in operation.parameters ?? new List<Parameter>()) 
     { 
      yield return parameter.schema; 
     } 
    } 


    public static IEnumerable<Schema> FindAdditionalSchema(this Schema schema, IDictionary<string, Schema> listOfDefinition) 
    { 
     if (!string.IsNullOrEmpty([email protected])) 
     { 
      Schema definition; 
      if (listOfDefinition.TryGetValue([email protected]("#/definitions/", String.Empty), out definition)) 
      { 
       foreach (var propertySchema in definition.properties) 
       { 
        yield return propertySchema.Value; 
       } 
      } 
     } 
     if (!string.IsNullOrEmpty([email protected])) 
     { 
      Schema definition; 
      if (listOfDefinition.TryGetValue([email protected]("#/definitions/", String.Empty), out definition)) 
      { 
       foreach (var propertySchema in definition.properties) 
       { 
        yield return propertySchema.Value; 
       } 
      } 
     } 
    } 

    public static IEnumerable<Schema> EnumerateSchema(this Schema schema,IDictionary<string,Schema> listOfDefinition, int dept = 0) 
    { 
     if (schema == null) 
     { 
      yield break; 
     } 
     if (dept > 10) 
     { 
      yield break; 
     } 
     if (dept == 0) 
     { 
      yield return schema; 
     } 

     var ListOfAdditionalSchema = schema.FindAdditionalSchema(listOfDefinition) ?? new List<Schema>(); 
     foreach (var additionalSchema in ListOfAdditionalSchema) 
     { 
      yield return additionalSchema; 
      foreach (var childSchema in additionalSchema.EnumerateSchema(listOfDefinition,dept++) ?? new List<Schema>()) 
      { 
       yield return childSchema; 
      } 
     } 
     } 
} 

public void Apply(SwaggerDocument swaggerDoc, SchemaRegistry schemaRegistry, IApiExplorer apiExplorer) 
     { 


      foreach (var value in swaggerDoc.paths.Values) 
      { 
       if (value.post != null && value.post.tags.Contains(ToHide)) 
        value.post = null; 

       if (value.get != null && value.get.tags.Contains(ToHide)) 
        value.get = null; 

       if (value.put != null && value.put.tags.Contains(ToHide)) 
        value.put = null; 

       if (value.delete != null && value.delete.tags.Contains(ToHide)) 
        value.delete = null; 

       if (value.head != null && value.head.tags.Contains(ToHide)) 
        value.head = null; 

       if (value.options != null && value.options.tags.Contains(ToHide)) 
        value.options = null; 
      } 

      var pathToDelete = swaggerDoc.paths.Where(x => !x.Value.EnumerateOperations().Any(y=>y != null)) 
               .ToList();//Deleting item from source need list 
      foreach (var item in pathToDelete) 
      { 
       swaggerDoc.paths.Remove(item.Key); 
      } 


      var listOfSchemaWithReference = swaggerDoc.paths.SelectMany(x => x.Value.EnumerateOperations())//Find operation by path 
              .SelectMany(x => x.EnumerateSchema()) //Find schema by operation 
              .SelectMany(x => x.EnumerateSchema(swaggerDoc.definitions))//Find Schema by schema (dependent schema) 
              .Where(x=> [email protected] != null || [email protected] != null)//I only wany the schema that reference a definition. 
              .Select(x=> (([email protected]) ?? ([email protected])).Replace("#/definitions/", String.Empty))//remove the path and keep the Model name 
              .Distinct()//I dont like duplicates 
              .ToList();//commit to memory 

      //Not finding a definition in the built list of reference means its unreferenced and can be removed. 
      var listOfUnreferencedDefinition = swaggerDoc.definitions.Where(x => !listOfSchemaWithReference.Any(y => y == x.Key)) 
                    .ToList();//Deleting item from source need list 

      foreach (var unreferencedDefinition in listOfUnreferencedDefinition) 
      { 
       swaggerDoc.definitions.Remove(unreferencedDefinition.Key); 
      } 
     } 
    } 
관련 문제