private void CreateArrayOrListObject(string name, object value, Type type, Type nestedType, OpenApiObject openApiObject) { if (value == null) { openApiObject.Add(name, new OpenApiNull()); return; } var arrayObject = new OpenApiArray(); foreach (var item in value as IEnumerable) { if (nestedType.IsSimpleType()) { var node = CreateOpenApiObject(nestedType, item); arrayObject.Add(node); } else { var arrayItemObject = new OpenApiObject(); var properties = nestedType.GetProperties(BindingFlags.Public | BindingFlags.Instance); foreach (var property in properties) { var nodeValue = property.GetValue(item); ConvertRec(GetName(property.Name), nodeValue, property.PropertyType, arrayItemObject); } arrayObject.Add(arrayItemObject); } } openApiObject.Add(GetName(name), arrayObject); }
private static IOpenApiAny ToOpenApiAny(Type type, object instance) { var arrayResult = ToOpenApiArray(type, instance); if (arrayResult != null) { return(arrayResult); } var result = new OpenApiObject(); foreach (var property in type.GetRuntimeProperties()) { var value = property.GetValue(instance); if (value != null) { var openValue = GetStructValue(property.PropertyType, value); var key = char.ToLower(property.Name[0]) + property.Name.Substring(1); if (openValue != null) { if (result.ContainsKey(key)) { result[key] = openValue; } else { result.Add(key, openValue); } continue; } var array = default(OpenApiArray); if ((array = ToOpenApiArray(property.PropertyType, value)) != null) { if (result.ContainsKey(key)) { result[key] = array; } else { result.Add(key, array); } continue; } var openObject = ToOpenApiAny(property.PropertyType, value); if (result.ContainsKey(key)) { result[key] = openObject; } else { result.Add(key, openObject); } } } return(result); }
public static OpenApiObject ToOpenApiObject(this object model) { // Get all properties on the object var properties = model.GetType().GetProperties() .Where(x => x.CanRead) .ToDictionary(x => x.Name, x => x.GetValue(model, null)); var opi = new OpenApiObject(); foreach (var prop in properties) { object value = null; value = prop.Value switch { null => new OpenApiNull(), int as_int => new OpenApiInteger(as_int), bool as_bool => new OpenApiBoolean(as_bool), byte as_byte => new OpenApiByte(as_byte), float as_float => new OpenApiFloat(as_float), _ => new OpenApiString($"{prop.Value}"), }; opi.Add(prop.Key, value as IOpenApiAny); } return(opi); }
private static IOpenApiAny CreateStructuredTypePropertiesExample(ODataContext context, IEdmStructuredType structuredType) { OpenApiObject example = new OpenApiObject(); IEdmEntityType entityType = structuredType as IEdmEntityType; // properties foreach (var property in structuredType.Properties()) { // IOpenApiAny item; IEdmTypeReference propertyType = property.Type; IOpenApiAny item = GetTypeNameForExample(context, propertyType); EdmTypeKind typeKind = propertyType.TypeKind(); if (typeKind == EdmTypeKind.Primitive && item is OpenApiString) { OpenApiString stringAny = item as OpenApiString; string value = stringAny.Value; if (entityType != null && entityType.Key().Any(k => k.Name == property.Name)) { value += " (identifier)"; } if (propertyType.IsDateTimeOffset() || propertyType.IsDate() || propertyType.IsTimeOfDay()) { value += " (timestamp)"; } item = new OpenApiString(value); } example.Add(property.Name, item); } return(example); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services .AddMvc() .SetCompatibilityVersion(CompatibilityVersion.Version_3_0) .AddNewtonsoftJson(options => { options.SerializerSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore; }); services.AddAuthentication(Constants.SignAuthenticationScheme). AddScheme <StsSettings, SignAuthenticationHandler>(Constants.SignAuthenticationScheme, options => Configuration.Bind("StsSettings", options)); services.AddAuthentication(BasicAuthenticationHandler.AuthenticationScheme). AddScheme <StsSettings, BasicAuthenticationHandler>(BasicAuthenticationHandler.AuthenticationScheme, options => Configuration.Bind("StsSettings", options)); services.AddAuthentication(SrsAuthenticationScheme.SessionAuthenticationScheme) .AddScheme <AuthenticationSchemeOptions, SessionAuthenticationHandler>(SrsAuthenticationScheme.SessionAuthenticationScheme, null); var packageNameExtension = new OpenApiObject(); packageNameExtension.Add("package-name", new OpenApiString("com.vmware.srs")); services.AddSwaggerGen( c => { c.SwaggerDoc( "srs", new OpenApiInfo { Description = APIGatewayResources.ProductApiDescription, Title = APIGatewayResources.ProductName, Version = APIGatewayResources.ProductVersion, Contact = new OpenApiContact() { Name = "Script Runtime Service for vSphere", Url = new Uri(@"https://github.com/vmware/script-runtime-service-for-vsphere"), }, Extensions = { { "x-vmw-vapi-codegenconfig", packageNameExtension } } }); var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml"; var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile); c.IncludeXmlComments(xmlPath); GlobalTagsSchemeFilter.Configure(c); TagsOperationFilter.Configure(c); VMwareVapiVendorExtensionsOperationFilter.Configure(c); SecurityRequirementsOperationFilter.Configure(c); ScriptExecutionParameterDocumentFilter.Configure(c); ScriptExecutionParameterSchemaFilter.Configure(c); //ServersDocumentFilter.Configure(c); //VMwarePrintingPressExtensionsOperationFilter.Configure(c); //VMwarePrintingPressPathExtensionsDocumentFilter.Configure(c); ReadOnlySchemaFilter.Configure(c); }); services.AddSwaggerGenNewtonsoftSupport(); }
public void Apply([NotNull] OpenApiSchema schema, [NotNull] SchemaFilterContext context) { if ((schema == null) || (context == null)) { throw new ArgumentNullException(nameof(schema) + " || " + nameof(context)); } if (!context.Type.IsEnum) { return; } if (context.Type.IsDefined(typeof(FlagsAttribute), false)) { schema.Format = "flags"; } OpenApiObject definition = new OpenApiObject(); foreach (object enumValue in context.Type.GetEnumValues()) { string enumName = Enum.GetName(context.Type, enumValue); if (string.IsNullOrEmpty(enumName)) { enumName = enumValue.ToString(); if (string.IsNullOrEmpty(enumName)) { continue; } } IOpenApiAny enumObject; if (TryCast(enumValue, out int intValue)) { enumObject = new OpenApiInteger(intValue); } else if (TryCast(enumValue, out long longValue)) { enumObject = new OpenApiLong(longValue); } else if (TryCast(enumValue, out ulong ulongValue)) { // OpenApi spec doesn't support ulongs as of now enumObject = new OpenApiString(ulongValue.ToString()); } else { throw new ArgumentOutOfRangeException(nameof(enumValue)); } definition.Add(enumName, enumObject); } schema.AddExtension("x-definition", definition); }
private void Transform(string key, OpenApiSchema schema, OpenApiObject openApiObject) { var type = schema.Type; var format = schema.Format; if (type == "object") { var item = new OpenApiObject(); foreach (var(itemKey, itemProperty) in schema.Properties) { Transform(itemKey, itemProperty, item); } openApiObject.Add(key, item); } else if (type == "array") { if (schema.Items.Type == "object") { var item = new OpenApiObject(); foreach (var(itemKey, itemProperty) in schema.Items.Properties) { Transform(itemKey, itemProperty, item); } var items = new OpenApiArray { item }; openApiObject.Add(key, items); } else { var items = new OpenApiArray(); var item = GetOpenApiValue(schema.Items); items.Add(item); openApiObject.Add(key, items); } } else { var openApiValue = GetOpenApiValue(schema); openApiObject.Add(key, openApiValue); } }
public override IOpenApiAny ParseIntoAny() { var obj = new OpenApiObject(); foreach (var node in childNodes) { obj.Add(node.Name, node.Value.ParseIntoAny()); } return(obj); }
/// <summary> /// Create a <see cref="OpenApiObject"/> /// </summary> /// <returns>The created Any object.</returns> public override IOpenApiAny CreateAny() { var apiObject = new OpenApiObject(); foreach (var node in this) { apiObject.Add(node.Name, node.Value.CreateAny()); } return(apiObject); }
private static IOpenApiAny CreateOpenApiObject(JsonElement jsonElement) { var openApiObject = new OpenApiObject(); foreach (var property in jsonElement.EnumerateObject()) { openApiObject.Add(property.Name, CreateFromJsonElement(property.Value)); } return(openApiObject); }
private void ConvertRec(string name, object value, Type type, OpenApiObject openApiObject) { if (type.IsArray) { var nestedType = type.GetElementType(); CreateArrayOrListObject(name, value, type, nestedType, openApiObject); } else if (type.IsListType()) { var nestedType = type.GetGenericArguments()[0]; CreateArrayOrListObject(name, value, type, nestedType, openApiObject); } else if (type.IsDictionary()) { // fix it later } else if (!type.IsSimpleType()) { if (value == null) { openApiObject.Add(name, new OpenApiNull()); return; } var node = new OpenApiObject(); foreach (var property in value.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance)) { var itemValue = property.GetValue(value); ConvertRec(GetName(property.Name), itemValue, property.PropertyType, node); } openApiObject.Add(name, node); } else { var node = CreateOpenApiObject(type, value); openApiObject.Add(name, node); } }
static public IOpenApiAny GetExample(Type parameter, TypeMaps maps, OpenApiComponents components) { if (components.Schemas.ContainsKey(parameter.Name)) { return(components.Schemas[parameter.Name].Example); } if (maps.ContainsMap(parameter)) { return(maps.GetMap(parameter).OpenApiExample); } else if (parameter == typeof(string)) { int randomNum = new Random().Next() % 3; var words = new string[] { "foo", "bar", "baz" }; return(new OpenApiString(words[randomNum])); } else if (IsNumericType(parameter)) { int randomNum = new Random().Next() % 400; return(new OpenApiInteger(randomNum)); } else if (parameter == typeof(bool)) { int randomNum = new Random().Next() % 1; return(new OpenApiBoolean(randomNum == 0)); } else if (parameter.GetInterfaces().Contains(typeof(IEnumerable))) { var exampleArr = new OpenApiArray(); int randomNum = new Random().Next() % 3; for (int _ = 0; _ < randomNum + 1; _++) { var innerType = parameter.GetElementType() ?? parameter.GenericTypeArguments[0]; exampleArr.Add(GetExample(innerType, maps, components)); } return(exampleArr); } else { if (parameter.GetProperties().Length == 0) { return(new OpenApiNull()); } var example = new OpenApiObject(); foreach (var prop in parameter.GetProperties()) { example.Add(prop.Name, GetExample(prop.PropertyType, maps, components)); } return(example); } }
} // GetConvertedType public OpenApiObject GenerateExample(Ac4yClass ac4yClass) { OpenApiObject resultExample = new OpenApiObject(); foreach (Ac4yProperty property in ac4yClass.PropertyList) { if (!property.Name.Equals("Id")) { if (GetConvertedType(property.TypeName, FormaKonverziok).Equals("int32") || GetConvertedType(property.TypeName, FormaKonverziok).Equals("int64")) { resultExample.Add(property.Name, new OpenApiInteger( Int32.Parse(property.ExampleValue) )); } if (GetConvertedType(property.TypeName, FormaKonverziok).Equals("float")) { resultExample.Add(property.Name, new OpenApiFloat( float.Parse(property.ExampleValue) )); } if (GetConvertedType(property.TypeName, FormaKonverziok).Equals("double")) { resultExample.Add(property.Name, new OpenApiDouble( Double.Parse(property.ExampleValue) )); } if (GetConvertedType(property.TypeName, FormaKonverziok).Equals("date")) { resultExample.Add(property.Name, new OpenApiDate(DateTime.Now.Date)); } if (GetConvertedType(property.TypeName, FormaKonverziok).Equals("date-time")) { resultExample.Add(property.Name, new OpenApiDateTime(DateTime.Now)); } if (GetConvertedType(property.TypeName, TipusKonverziok).Equals("string") && GetConvertedType(property.TypeName, FormaKonverziok).Equals("")) { resultExample.Add(property.Name, new OpenApiString(property.ExampleValue)); } if (GetConvertedType(property.TypeName, TipusKonverziok).Equals("boolean") && GetConvertedType(property.TypeName, FormaKonverziok).Equals("")) { resultExample.Add(property.Name, new OpenApiBoolean( Boolean.Parse(property.ExampleValue) )); } } } ; return(resultExample); }
/// <summary> /// 获取或创建接口初始类 /// </summary> /// <param name="type"></param> /// <param name="innerModel">处理内部模型</param> /// <returns></returns> public static IOpenApiAny GetOrNullFor(this Type type, bool innerModel = true) { if (CacheExtention.OpenApiObjectDic.ContainsKey(type.FullName)) { return(CacheExtention.OpenApiObjectDic[type.FullName]); } var example = new OpenApiObject(); var propertyDic = type.GetPropertysOfTypeDic(false); type.FilterModel((_type, prop) => { IOpenApiAny any = prop.PropertyType.IsArray || prop.PropertyType.IsGenericType ? new OpenApiArray { prop.CreateFor(innerModel) } : prop.CreateFor(innerModel); example.Add(prop.Name, any); if (!propertyDic.ContainsKey(_type.FullName)) { propertyDic.Add(_type.FullName, new List <string>() { prop.Name }); } else { propertyDic[_type.FullName].Add(prop.Name); } if (!CacheExtention.AssemblyOfTypeDic.ContainsKey(_type.FullName)) { CacheExtention.AssemblyOfTypeDic.AddOrUpdate(_type.FullName, _type.Assembly.FullName, (key, old) => _type.Assembly.FullName); } }); //type.SetPropertysOfTypeDic(propertyDic); if (!CacheExtention.OpenApiObjectDic.ContainsKey(type.FullName)) { CacheExtention.OpenApiObjectDic.AddOrUpdate(type.FullName, example, (key, old) => example); } return(example); }
private static IOpenApiAny CreateOpenApiObject(JsonElement jsonElement) { var openApiObject = new OpenApiObject(); foreach (var property in jsonElement.EnumerateObject()) { var valueAsJson = (property.Value.ValueKind == JsonValueKind.String) ? $"\"{property.Value}\"" : property.Value.ToString(); openApiObject.Add(property.Name, CreateFromJson(valueAsJson)); } return(openApiObject); }
internal override IDictionary <string, IOpenApiAny> ToDictionary() { var children = new OpenApiObject(); var result = new Dictionary <string, IOpenApiAny>(); if (RequestValidators != null && RequestValidators.Any()) { foreach (var validator in RequestValidators) { children.Add(validator.ToDictionaryItem()); } result[RequestValidatorsRootKey] = children; } return(result); }
private static OpenApiExample CreateStructuredTypeExample(IEdmStructuredType structuredType) { OpenApiExample example = new OpenApiExample(); OpenApiObject value = new OpenApiObject(); IEdmEntityType entityType = structuredType as IEdmEntityType; // properties foreach (var property in structuredType.DeclaredProperties.OrderBy(p => p.Name)) { // IOpenApiAny item; IEdmTypeReference propertyType = property.Type; IOpenApiAny item = GetTypeNameForExample(propertyType); EdmTypeKind typeKind = propertyType.TypeKind(); if (typeKind == EdmTypeKind.Primitive && item is OpenApiString) { OpenApiString stringAny = item as OpenApiString; string propertyValue = stringAny.Value; if (entityType != null && entityType.Key().Any(k => k.Name == property.Name)) { propertyValue += " (identifier)"; } if (propertyType.IsDateTimeOffset() || propertyType.IsDate() || propertyType.IsTimeOfDay()) { propertyValue += " (timestamp)"; } item = new OpenApiString(propertyValue); } value.Add(property.Name, item); } example.Value = value; return(example); }