Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions src/NJsonSchema.CodeGeneration.CSharp/Models/EnumTemplateModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,15 @@ public IEnumerable<EnumerationItemModel> Enums
var value = _schema.Enumeration.ElementAt(i);
if (value != null)
{
var description = _schema.EnumerationDescriptions.Count > i ?
_schema.EnumerationDescriptions.ElementAt(i) : null;
var description = _schema.EnumerationDescriptions.Count > i
? _schema.EnumerationDescriptions[i]
: null;

if (_schema.Type.IsInteger())
{
var name = _schema.EnumerationNames.Count > i ?
_schema.EnumerationNames.ElementAt(i) : "_" + value;
var name = _schema.EnumerationNames.Count > i
? _schema.EnumerationNames[i]
: "_" + value;

if (_schema.IsFlagEnumerable && TryGetInt64(value, out long valueInt64))
{
Expand Down Expand Up @@ -100,8 +103,9 @@ public IEnumerable<EnumerationItemModel> Enums
}
else
{
var name = _schema.EnumerationNames.Count > i ?
_schema.EnumerationNames.ElementAt(i) : value.ToString();
var name = _schema.EnumerationNames.Count > i
? _schema.EnumerationNames[i]
: value.ToString();

entries.Add(new EnumerationItemModel
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,12 @@ public List<EnumerationItemModel> Enums
if (value != null)
{
var name = _schema.EnumerationNames.Count > i
? _schema.EnumerationNames.ElementAt(i)
? _schema.EnumerationNames[i]
: _schema.Type.IsInteger() ? "_" + value : value.ToString()!;
var description = _schema.EnumerationDescriptions.Count > i ?
_schema.EnumerationDescriptions.ElementAt(i) : null;

var description = _schema.EnumerationDescriptions.Count > i
? _schema.EnumerationDescriptions[i]
: null;

entries.Add(new EnumerationItemModel
{
Expand Down
5 changes: 2 additions & 3 deletions src/NJsonSchema.CodeGeneration/ValueGeneratorBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
//-----------------------------------------------------------------------

using System.Globalization;
using System.Linq;
using System.Text.RegularExpressions;

namespace NJsonSchema.CodeGeneration
Expand Down Expand Up @@ -97,9 +96,9 @@ protected virtual string GetEnumDefaultValue(JsonSchema schema, JsonSchema actua
{
var typeName = typeResolver.Resolve(actualSchema, false, typeNameHint);

var index = actualSchema.Enumeration.ToList().IndexOf(schema.Default);
var index = actualSchema.Enumeration.IndexOf(schema.Default);
var enumName = index >= 0 && actualSchema.EnumerationNames?.Count > index
? actualSchema.EnumerationNames.ElementAt(index)
? actualSchema.EnumerationNames[index]
: schema.Default?.ToString();

return typeName.Trim('?') + "." + _settings.EnumNameGenerator.Generate(index, enumName, schema.Default, actualSchema);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,13 @@ protected virtual Type GetDiscriminatorType(JObject jObject, Type objectType, st
var type = objectType;
do
{
var knownTypeAttributes = type.GetCustomAttributes(false)
.Where(a => a.GetType().Name == "KnownTypeAttribute");
foreach (dynamic attribute in knownTypeAttributes)
foreach (dynamic attribute in type.GetCustomAttributes(inherit: false))
{
if (attribute.GetType().Name != "KnownTypeAttribute")
{
continue;
}

if (attribute.Type != null && attribute.Type.Name == discriminator)
{
return attribute.Type;
Expand Down Expand Up @@ -286,22 +289,28 @@ protected virtual Type GetDiscriminatorType(JObject jObject, Type objectType, st

private static Type? GetObjectSubtype(Type baseType, string discriminatorName)
{
var jsonInheritanceAttributes = baseType

.GetCustomAttributes(true)
.OfType<JsonInheritanceAttribute>();
foreach (var a in baseType.GetCustomAttributes<JsonInheritanceAttribute>(inherit: true))
{
if (a.Key == discriminatorName)
{
return a.Type;
}
}

return jsonInheritanceAttributes.SingleOrDefault(a => a.Key == discriminatorName)?.Type;
return null;
}

private static string? GetSubtypeDiscriminator(Type objectType)
{
var jsonInheritanceAttributes = objectType

.GetCustomAttributes(true)
.OfType<JsonInheritanceAttribute>();
foreach (var a in objectType.GetCustomAttributes<JsonInheritanceAttribute>(inherit: true))
{
if (a.Type == objectType)
{
return a.Key;
}
}

return jsonInheritanceAttributes.SingleOrDefault(a => a.Type == objectType)?.Key;
return null;
}
}
}
31 changes: 31 additions & 0 deletions src/NJsonSchema/Infrastructure/CollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace NJsonSchema;

internal static class CollectionExtensions
{
public static int IndexOf<T>(this ICollection<T> collection, T item)
{
if (collection is List<T> l)
{
return l.IndexOf(item);
}

if (collection is Collection<T> c)
{
return c.IndexOf(item);
}

int index = 0;
foreach (var element in collection)
{
if (EqualityComparer<T>.Default.Equals(element, item))
{
return index;
}
index++;
}
return -1; // Item not found
}
}
15 changes: 10 additions & 5 deletions src/NJsonSchema/Validation/JsonSchemaValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -518,15 +518,20 @@ private void ValidateArray(JToken token, JsonSchema schema, SchemaType schemaTyp

private void ValidateAdditionalItems(JToken item, JsonSchema schema, SchemaType schemaType, int index, string? propertyPath, List<ValidationError> errors)
{
if (schema.Items.Count > 0)
var items = schema._items;
if (items.Count > 0)
{
var propertyIndex = $"[{index}]";
if (schema.Items.Count > index)
if (items.Count > index)
{
var error = TryCreateChildSchemaError(item,
schema.Items.ElementAt(index),
var error = TryCreateChildSchemaError(
item,
items[index],
schemaType,
ValidationErrorKind.ArrayItemNotValid, propertyIndex, propertyPath + propertyIndex);
ValidationErrorKind.ArrayItemNotValid,
propertyIndex,
propertyPath + propertyIndex);

if (error != null)
{
errors.Add(error);
Expand Down