-
-
Save INTERNALINTERFERENCE/34b809883d74a252ac4326a18fbce93b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ApiCommands] | |
abstract public class Commands | |
{ | |
[ApiArgument( typeof( SubjectsListArguments ), isOptional: true )] | |
[ApiReturn( typeof( SubjectsListResponse ) )] | |
[Title] | |
[Description] | |
public const string SubjectsList = "subjects_list"; | |
} | |
private IDictionary<string, OpenApiSchema> GenerateProperties(Type arguments) | |
{ | |
var properties = new Dictionary<string, OpenApiSchema>(); | |
GeneratePropertiesRecursive(arguments, properties); | |
return properties; | |
} | |
private void GeneratePropertiesRecursive( | |
Type type, | |
IDictionary<string, OpenApiSchema> properties) | |
{ | |
if (type == null) return; | |
var propertyInfos = type.GetProperties(); | |
foreach (var propertyInfo in propertyInfos) | |
{ | |
var attributeValue = propertyInfo | |
.GetCustomAttribute<JsonPropertyAttribute>(); | |
if (attributeValue == null) | |
continue; | |
var propertyName = attributeValue.PropertyName; | |
var propertyType = propertyInfo.PropertyType; | |
var propertySchema = new OpenApiSchema(); | |
if (propertyType.IsClass && !_excludedTypes.Contains(propertyType)) | |
{ | |
if (ImplementsEnumerableTypes(propertyType, out var genericTypes)) | |
{ | |
var itemsSchema = new OpenApiSchema(); | |
foreach (var genericType in genericTypes) | |
{ | |
if (_excludedTypes.Contains(genericType)) | |
{ | |
itemsSchema = genericType.MapTypeToOpenApiType(propertySchema); | |
break; | |
} | |
GeneratePropertiesRecursive(genericType, itemsSchema.Properties); | |
} | |
propertySchema.Type = "array"; | |
propertySchema.Items = itemsSchema; | |
properties.Add(propertyName, propertySchema); | |
continue; | |
} | |
GeneratePropertiesRecursive(propertyType, propertySchema.Properties); | |
} | |
if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(OptionalValue<>)) | |
{ | |
var genericType = propertyType.GetGenericArguments()[0]; | |
GeneratePropertiesRecursive(genericType, propertySchema.Properties); | |
propertyType = genericType; | |
} | |
if (IsNullableType(propertyType, out var nullableType)) | |
propertyType = nullableType; | |
if (propertyType.IsEnum) | |
{ | |
var types = propertyType.GetOpenApiTypesFromEnumPropertyType(); | |
propertySchema.Type = "string"; | |
propertySchema.Enum = types; | |
properties.Add( propertyName, propertySchema ); | |
continue; | |
} | |
propertySchema = propertyType.MapTypeToOpenApiType(propertySchema); | |
properties.Add(propertyName, propertySchema); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment