protected override OpenApiSchema GenerateSchemaFor(Type type, SchemaRepository schemaRepository) { var jsonDictionaryContract = (JsonDictionaryContract)ContractResolver.ResolveContract(type); var keyType = jsonDictionaryContract.DictionaryKeyType ?? typeof(object); var valueType = jsonDictionaryContract.DictionaryValueType ?? typeof(object); if (keyType.IsEnum) { // This is a special case where we can include named properties based on the enum values return(new OpenApiSchema { Type = "object", Properties = jsonDictionaryContract.DictionaryKeyType.GetEnumNames() .ToDictionary( name => name, name => RootGenerator.GenerateSchema(valueType, schemaRepository) ) }); } return(new OpenApiSchema { Type = "object", AdditionalPropertiesAllowed = true, AdditionalProperties = RootGenerator.GenerateSchema(valueType, schemaRepository) }); }
private OpenApiSchema GeneratePropertySchema( JsonProperty jsonProperty, MemberInfo memberInfo, object[] attributes, SchemaRepository schemaRepository) { var schema = RootGenerator.GenerateSchema(jsonProperty.PropertyType, schemaRepository); schema.WriteOnly = jsonProperty.Writable && !jsonProperty.Readable; schema.ReadOnly = jsonProperty.Readable && !jsonProperty.Writable; foreach (var attribute in attributes) { if (attribute is DefaultValueAttribute defaultValue) { schema.Default = OpenApiAnyFactory.TryCreateFor(schema, defaultValue.Value, out IOpenApiAny openApiAny) ? openApiAny : schema.Default; } else if (attribute is RegularExpressionAttribute regex) { schema.Pattern = regex.Pattern; } else if (attribute is RangeAttribute range) { schema.Maximum = decimal.TryParse(range.Maximum.ToString(), out decimal maximum) ? maximum : schema.Maximum; schema.Minimum = decimal.TryParse(range.Minimum.ToString(), out decimal minimum) ? minimum : schema.Minimum; } else if (attribute is MinLengthAttribute minLength) { schema.MinLength = minLength.Length; } else if (attribute is MaxLengthAttribute maxLength) { schema.MaxLength = maxLength.Length; } else if (attribute is StringLengthAttribute stringLength) { schema.MinLength = stringLength.MinimumLength; schema.MaxLength = stringLength.MaximumLength; } else if (attribute is DataTypeAttribute dataTypeAttribute && schema.Type == "string") { schema.Format = DataTypeFormatMap.TryGetValue(dataTypeAttribute.DataType, out string format) ? format : schema.Format; } } return(schema); }
protected override OpenApiSchema GenerateSchemaFor(Type type, SchemaRepository schemaRepository) { var subTypes = Options.SubTypesResolver(type); return(new OpenApiSchema { OneOf = subTypes .Select(subType => RootGenerator.GenerateSchema(subType, schemaRepository)) .ToList() }); }
protected override OpenApiSchema GenerateSchemaFor(Type type, SchemaRepository schemaRepository) { var jsonArrayContract = (JsonArrayContract)ContractResolver.ResolveContract(type); return(new OpenApiSchema { Type = "array", Items = RootGenerator.GenerateSchema(jsonArrayContract.CollectionItemType, schemaRepository), UniqueItems = jsonArrayContract.UnderlyingType.IsSet() ? (bool?)true : null }); }
private OpenApiSchema CreateExceptionSchema(SchemaRepository schemaRepository) { return(new OpenApiSchema { Type = "object", Properties = new Dictionary <string, OpenApiSchema> { ["Message"] = RootGenerator.GenerateSchema(typeof(string), schemaRepository), ["StackTraceString"] = RootGenerator.GenerateSchema(typeof(string), schemaRepository), ["Source"] = RootGenerator.GenerateSchema(typeof(string), schemaRepository), } }); }
protected override OpenApiSchema GenerateSchemaFor(Type type, SchemaRepository schemaRepository) { var jsonArrayContract = (JsonArrayContract)ContractResolver.ResolveContract(type); //var properties = new Dictionary<string, OpenApiSchema>(); //ObjectSchemaGenerator.AddTypeAttribute(_serializerSettings, type, properties); return(new OpenApiSchema { Type = "array", Items = RootGenerator.GenerateSchema(jsonArrayContract.CollectionItemType, schemaRepository), UniqueItems = jsonArrayContract.UnderlyingType.IsSet() ? (bool?)true : null, }); }
protected override OpenApiSchema GenerateSchemaFor(Type type, SchemaRepository schemaRepository) { var jsonObjectContract = (JsonObjectContract)ContractResolver.ResolveContract(type); var properties = new Dictionary <string, OpenApiSchema>(); AddTypeAttribute(_serializerSettings, type, properties); var requiredPropertyNames = new List <string>(); foreach (var jsonProperty in jsonObjectContract.Properties) { if (jsonProperty.Ignored) { continue; } var attributes = jsonProperty.TryGetMemberInfo(out MemberInfo memberInfo) ? memberInfo.GetCustomAttributes(true) : new object[] { }; if (Options.IgnoreObsoleteProperties && attributes.OfType <ObsoleteAttribute>().Any()) { continue; } properties.Add(jsonProperty.PropertyName, GeneratePropertySchema(jsonProperty, memberInfo, attributes, schemaRepository)); if (jsonProperty.Required == Required.AllowNull || jsonProperty.Required == Required.Always || attributes.OfType <RequiredAttribute>().Any()) { requiredPropertyNames.Add(jsonProperty.PropertyName); } } var additionalProperties = (jsonObjectContract.ExtensionDataValueType != null) ? RootGenerator.GenerateSchema(jsonObjectContract.ExtensionDataValueType, schemaRepository) : null; var schema = new OpenApiSchema { Type = "object", Properties = properties, Required = new SortedSet <string>(requiredPropertyNames), AdditionalPropertiesAllowed = (additionalProperties != null), AdditionalProperties = additionalProperties }; return(schema); }
private OpenApiSchema CreateDataTableSchema(SchemaRepository schemaRepository) { var schema = new OpenApiSchema { Type = "array", Items = new OpenApiSchema { Properties = new Dictionary <string, OpenApiSchema> { ["Column1"] = RootGenerator.GenerateSchema(typeof(int), schemaRepository), ["Column2"] = RootGenerator.GenerateSchema(typeof(double), schemaRepository), ["Column3"] = RootGenerator.GenerateSchema(typeof(short), schemaRepository), ["Column4"] = RootGenerator.GenerateSchema(typeof(long), schemaRepository), ["Column5"] = RootGenerator.GenerateSchema(typeof(string), schemaRepository), } } }; return(schema); }