JsonProperty
A description of a JSON property of a JSON object.
using Newtonsoft.Json;
using System.ComponentModel;
using System.Linq;
namespace NJsonSchema
{
public class JsonProperty : JsonSchema4
{
private JsonSchema4 _parentSchema;
[JsonIgnore]
public string Name { get; set; }
[JsonIgnore]
public override JsonSchema4 ParentSchema {
get {
return _parentSchema;
}
internal set {
bool num = _parentSchema == null;
_parentSchema = value;
if (num && InitialIsRequired)
IsRequired = InitialIsRequired;
}
}
[JsonIgnore]
public bool IsRequired {
get {
return ParentSchema.RequiredProperties.Contains(Name);
}
set {
if (ParentSchema == null)
InitialIsRequired = value;
else if (value) {
if (!ParentSchema.RequiredProperties.Contains(Name))
ParentSchema.RequiredProperties.Add(Name);
} else if (ParentSchema.RequiredProperties.Contains(Name)) {
ParentSchema.RequiredProperties.Remove(Name);
}
}
}
[JsonIgnore]
internal bool InitialIsRequired { get; set; }
[DefaultValue(false)]
[JsonProperty("readonly", DefaultValueHandling = DefaultValueHandling.Ignore)]
public bool IsReadOnly { get; set; }
[JsonIgnore]
public JsonSchema4 ActualPropertySchema {
get {
return base.OneOf.FirstOrDefault((JsonSchema4 o) => !o.IsNullable(NullHandling.JsonSchema))?.ActualSchema ?? ActualSchema;
}
}
[JsonIgnore]
public bool IsInheritanceDiscriminator {
get {
return ParentSchema.Discriminator == Name;
}
}
internal static JsonProperty FromJsonSchema(string name, JsonSchema4 type)
{
JsonProperty jsonProperty = JsonConvert.DeserializeObject<JsonProperty>(JsonConvert.SerializeObject(type));
jsonProperty.Name = name;
return jsonProperty;
}
public override bool IsNullable(NullHandling nullHandling)
{
if (nullHandling == NullHandling.Swagger)
return !IsRequired;
return base.IsNullable(nullHandling);
}
}
}