Skip to content

Commit

Permalink
Add binary handling, RicoSuter/NSwag#2016
Browse files Browse the repository at this point in the history
  • Loading branch information
RicoSuter committed Mar 18, 2019
1 parent 1d77820 commit 8535145
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public string Resolve(JsonSchema4 schema, bool isNullable, string typeNameHint,
if (type.HasFlag(JsonObjectType.String) && !schema.ActualTypeSchema.IsEnumeration)
return ResolveString(schema.ActualTypeSchema, isNullable, typeNameHint);

if (type.HasFlag(JsonObjectType.File))
if (schema.IsBinary)
return "byte[]";

// Type generating schemas
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ private string Resolve(JsonSchema4 schema, string typeNameHint, bool addInterfac
if (type.HasFlag(JsonObjectType.String) && !schema.ActualTypeSchema.IsEnumeration)
return ResolveString(schema.ActualTypeSchema, typeNameHint);

if (type.HasFlag(JsonObjectType.File))
if (schema.IsBinary)
return "any";

// Type generating schemas
Expand Down
17 changes: 13 additions & 4 deletions src/NJsonSchema/Generation/DefaultReflectionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,17 @@ public virtual JsonTypeDescription GetDescription(Type type, IEnumerable<Attribu
return JsonTypeDescription.Create(type, JsonObjectType.None, isNullable, null);
}

if (IsFileType(type, parentAttributes))
return JsonTypeDescription.Create(type, JsonObjectType.File, isNullable, null);
if (IsBinary(type, parentAttributes))
{
if (settings.SchemaType == SchemaType.Swagger2)
{
return JsonTypeDescription.Create(type, JsonObjectType.File, isNullable, null);
}
else
{
return JsonTypeDescription.Create(type, JsonObjectType.String, isNullable, JsonFormatStrings.Binary);
}
}

var contract = settings.ResolveContract(type);
if (IsDictionaryType(type, parentAttributes) && contract is JsonDictionaryContract)
Expand Down Expand Up @@ -179,11 +188,11 @@ public virtual bool IsNullable(Type type, IEnumerable<Attribute> parentAttribute
return isValueType == false && settings.DefaultReferenceTypeNullHandling == ReferenceTypeNullHandling.Null;
}

/// <summary>Checks whether the given type is a file type.</summary>
/// <summary>Checks whether the given type is a file/binary type.</summary>
/// <param name="type">The type.</param>
/// <param name="parentAttributes">The parent attributes.</param>
/// <returns>true or false.</returns>
protected virtual bool IsFileType(Type type, IEnumerable<Attribute> parentAttributes)
protected virtual bool IsBinary(Type type, IEnumerable<Attribute> parentAttributes)
{
// TODO: Move all file handling to NSwag. How?

Expand Down
3 changes: 3 additions & 0 deletions src/NJsonSchema/JsonFormatStrings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ public static class JsonFormatStrings

/// <summary>Format for a byte if used with numeric type or for base64 encoded value otherwise.</summary>
public const string Byte = "byte";

/// <summary>Format for a binary value.</summary>
public const string Binary = "binary";

/// <summary>Format for a hostname (DNS name).</summary>
public const string Hostname = "hostname";
Expand Down
11 changes: 11 additions & 0 deletions src/NJsonSchema/JsonSchema4.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,17 @@ internal static JsonSchema4 FromJsonWithoutReferenceHandling(string data)
return schema;
}

/// <summary>Gets a value indicating whether the schema is binary (file or binary format).</summary>
[JsonIgnore]
public bool IsBinary
{
get
{
return Type.HasFlag(JsonObjectType.File) ||
(Type.HasFlag(JsonObjectType.String) && Format == JsonFormatStrings.Binary);
}
}

/// <summary>Gets the inherited/parent schema (most probable base schema in allOf).</summary>
/// <remarks>Used for code generation.</remarks>
[JsonIgnore]
Expand Down

0 comments on commit 8535145

Please sign in to comment.