2017-09-08 69 views
1

我在隱藏調用時遇到了Swashbuckle問題鏈接到調用的模型的定義仍保留在JSON生成的定義中。Swashbuckle隱藏未提及的模型

使用文件過濾器,我可以從接口中刪除呼叫。

調用保留在生成的JSON中,但在Swagger UI中不可見。 我們還可以看到與這些調用相關的Model和Enum的定義。

這些是內部調用,需要在JSON中隱藏外部眼睛。

如何隱藏所有電話及其參考?

使用[ApiExplorerSettings(IgnoreApi = true)]將解決我的問題,但我需要過濾一個現有的屬性。

回答

2

所以我通過努力找到了我的問題的答案。 我把它放在這裏,以便我的問題的下一個人將有一個更好的時間,然後我。

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; 
      } 
     } 
     } 
} 
中的DocumentFilter

然後

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); 
      } 
     } 
    }