private void GenerateObjectSchema(JSchema schema, Type type, JsonObjectContract contract) { foreach (JsonProperty property in contract.Properties) { if (!property.Ignored) { bool optional = property.NullValueHandling == NullValueHandling.Ignore || HasFlag(property.DefaultValueHandling.GetValueOrDefault(), DefaultValueHandling.Ignore) || property.ShouldSerialize != null || property.GetIsSpecified != null; Required required = property.Required; if (DataAnnotationHelpers.GetRequired(property)) { required = Required.Always; } JSchema propertySchema = GenerateInternal(property.PropertyType, required, property, contract); if (property.DefaultValue != null) { propertySchema.Default = JToken.FromObject(property.DefaultValue); } schema.Properties.Add(property.PropertyName, propertySchema); if (!optional) { schema.Required.Add(property.PropertyName); } } } if (type.IsSealed()) { schema.AllowAdditionalProperties = false; } }
private void GenerateObjectSchema(JSchema schema, Type type, JsonObjectContract contract) { IEnumerable <JsonProperty> properties; if (_generator.SchemaPropertyOrderHandling == SchemaPropertyOrderHandling.Alphabetical) { properties = contract.Properties .OrderBy(p => p.Order ?? 0) .ThenBy(p => p.PropertyName); } else { properties = contract.Properties; } foreach (JsonProperty property in properties) { if (!property.Ignored) { Required?required = property._required; if (DataAnnotationHelpers.GetRequired(property)) { required = Required.Always; } JSchema propertySchema = GenerateInternal(property.PropertyType, required, property, contract, null); if (property.DefaultValue != null) { propertySchema.Default = JToken.FromObject(property.DefaultValue); } schema.Properties.Add(property.PropertyName, propertySchema); Required resolvedRequired = required ?? _generator.DefaultRequired; bool optional; switch (resolvedRequired) { case Required.Default: case Required.DisallowNull: optional = true; break; case Required.Always: case Required.AllowNull: optional = property.NullValueHandling == NullValueHandling.Ignore || HasFlag(property.DefaultValueHandling.GetValueOrDefault(), DefaultValueHandling.Ignore) || property.ShouldSerialize != null || property.GetIsSpecified != null; break; default: throw new ArgumentOutOfRangeException("required"); } if (!optional) { schema.Required.Add(property.PropertyName); } } } if (type.IsSealed()) { schema.AllowAdditionalProperties = false; } }