|
6 | 6 | namespace VirtoCommerce.Platform.Web.Swagger
|
7 | 7 | {
|
8 | 8 |
|
9 |
| - public class PolymorphismSchemaFilter<T> : ISchemaFilter |
| 9 | + public class PolymorphismSchemaFilter : ISchemaFilter |
10 | 10 | {
|
11 |
| - private readonly Lazy<HashSet<Type>> derivedTypes = new Lazy<HashSet<Type>>(Init); |
| 11 | + private readonly Type[] _types; |
| 12 | + private readonly Lazy<HashSet<Type>> _derivedTypes; |
12 | 13 |
|
13 |
| - private static HashSet<Type> Init() |
| 14 | + public PolymorphismSchemaFilter(Type[] types) |
14 | 15 | {
|
15 |
| - var abstractType = typeof(T); |
16 |
| - var dTypes = abstractType.Assembly |
17 |
| - .GetTypes() |
18 |
| - .Where(x => abstractType != x && abstractType.IsAssignableFrom(x)); |
| 16 | + _types = types; |
| 17 | + _derivedTypes = new Lazy<HashSet<Type>>(Init); |
| 18 | + } |
19 | 19 |
|
| 20 | + private HashSet<Type> Init() |
| 21 | + { |
20 | 22 | var result = new HashSet<Type>();
|
21 | 23 |
|
22 |
| - foreach (var item in dTypes) |
| 24 | + var derivedTypes = _types.SelectMany(x => |
| 25 | + x.Assembly |
| 26 | + .GetTypes() |
| 27 | + .Where(y => x != y && y.IsAssignableFrom(x))); |
| 28 | + |
| 29 | + foreach (var item in derivedTypes) |
| 30 | + { |
23 | 31 | result.Add(item);
|
| 32 | + } |
24 | 33 |
|
25 | 34 | return result;
|
26 | 35 | }
|
27 | 36 |
|
28 | 37 | [CLSCompliant(false)]
|
29 | 38 | public void Apply(Schema schema, SchemaRegistry schemaRegistry, Type type)
|
30 | 39 | {
|
31 |
| - if (!derivedTypes.Value.Contains(type)) |
32 |
| - return; |
33 |
| - |
34 |
| - var clonedSchema = new Schema |
| 40 | + if (_derivedTypes.Value.Contains(type)) |
35 | 41 | {
|
36 |
| - properties = schema.properties, |
37 |
| - type = schema.type, |
38 |
| - required = schema.required |
39 |
| - }; |
| 42 | + var clonedSchema = new Schema |
| 43 | + { |
| 44 | + properties = schema.properties, |
| 45 | + type = schema.type, |
| 46 | + required = schema.required |
| 47 | + }; |
40 | 48 |
|
41 |
| - //schemaRegistry.Definitions[typeof(T).Name]; does not work correctly in SwashBuckle |
42 |
| - var parentSchema = new Schema { @ref = "#/definitions/" + typeof(T).Name }; |
| 49 | + //schemaRegistry.Definitions[typeof(T).Name]; does not work correctly in SwashBuckle |
| 50 | + var parentSchema = new Schema { @ref = "#/definitions/" + type.BaseType.Name }; |
43 | 51 |
|
44 |
| - schema.allOf = new List<Schema> { parentSchema, clonedSchema }; |
| 52 | + schema.allOf = new List<Schema> { parentSchema, clonedSchema }; |
45 | 53 |
|
46 |
| - //reset properties for they are included in allOf, should be null but code does not handle it |
47 |
| - schema.properties = new Dictionary<string, Schema>(); |
48 |
| - } |
49 |
| - } |
50 |
| - |
51 |
| - |
52 |
| - public class PolymorphismDocumentFilter<T> : IDocumentFilter |
53 |
| - { |
54 |
| - [CLSCompliant(false)] |
55 |
| - public void Apply(SwaggerDocument swaggerDoc, SchemaRegistry schemaRegistry, System.Web.Http.Description.IApiExplorer apiExplorer) |
56 |
| - { |
57 |
| - RegisterSubClasses(schemaRegistry, typeof(T)); |
58 |
| - } |
59 |
| - |
60 |
| - |
61 |
| - private static void RegisterSubClasses(SchemaRegistry schemaRegistry, Type abstractType) |
62 |
| - { |
63 |
| - const string discriminatorName = "type"; |
64 |
| - |
65 |
| - var typeName = schemaRegistry.Definitions.ContainsKey(abstractType.FullName) ? abstractType.FullName : abstractType.FriendlyId(); |
66 |
| - var parentSchema = schemaRegistry.Definitions[typeName]; |
67 |
| - |
68 |
| - //set up a discriminator property (it must be required) |
69 |
| - parentSchema.discriminator = discriminatorName; |
70 |
| - parentSchema.required = new List<string> { discriminatorName }; |
71 |
| - |
72 |
| - if (!parentSchema.properties.ContainsKey(discriminatorName)) |
73 |
| - parentSchema.properties.Add(discriminatorName, new Schema { type = "string" }); |
74 |
| - |
75 |
| - //register all subclasses |
76 |
| - var derivedTypes = abstractType.Assembly |
77 |
| - .GetTypes() |
78 |
| - .Where(x => abstractType != x && abstractType.IsAssignableFrom(x)); |
79 |
| - |
80 |
| - foreach (var item in derivedTypes) |
81 |
| - schemaRegistry.GetOrRegister(item); |
| 54 | + //reset properties for they are included in allOf, should be null but code does not handle it |
| 55 | + schema.properties = new Dictionary<string, Schema>(); |
| 56 | + } |
82 | 57 | }
|
83 | 58 | }
|
84 | 59 | }
|
0 commit comments