Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ internal sealed class JsonSchema
internal const string AnyOfPropertyName = "anyOf";
internal const string ConstPropertyName = "const";
internal const string DefaultPropertyName = "default";
internal const string ContentEncodingPropertyName = "contentEncoding";
internal const string MinLengthPropertyName = "minLength";
internal const string MaxLengthPropertyName = "maxLength";
internal const string DeprecatedPropertyName = "deprecated";
Expand Down Expand Up @@ -81,6 +82,8 @@ public JsonSchema() { }

public bool? Deprecated { get; set { VerifyMutable(); field = value; } }

public string? ContentEncoding { get; set { VerifyMutable(); field = value; } }

public JsonSchemaExporterContext? ExporterContext { get; set; }

public int KeywordCount
Expand Down Expand Up @@ -111,6 +114,7 @@ public int KeywordCount
Count(MinLength is not null);
Count(MaxLength is not null);
Count(Deprecated is not null);
Count(ContentEncoding is not null);

return count;

Expand Down Expand Up @@ -247,6 +251,11 @@ public JsonNode ToJsonNode(JsonSchemaExporterOptions options)
objSchema.Add(DeprecatedPropertyName, (JsonNode)deprecated);
}

if (ContentEncoding is not null)
{
objSchema.Add(ContentEncodingPropertyName, ContentEncoding);
}

return CompleteSchema(objSchema);

JsonNode CompleteSchema(JsonNode schema)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public override void Write(Utf8JsonWriter writer, byte[]? value, JsonSerializerO
}
}

internal override JsonSchema? GetSchema(JsonNumberHandling _) => new() { Type = JsonSchemaType.String };
internal override JsonSchema? GetSchema(JsonNumberHandling _) => new() { Type = JsonSchemaType.String, ContentEncoding = "base64" };

internal override JsonValueType GetSupportedJsonValueTypes(JsonNumberHandling _) => JsonValueType.String;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public override void Write(Utf8JsonWriter writer, Memory<byte> value, JsonSerial
writer.WriteBase64StringValue(value.Span);
}

internal override JsonSchema? GetSchema(JsonNumberHandling _) => new() { Type = JsonSchemaType.String };
internal override JsonSchema? GetSchema(JsonNumberHandling _) => new() { Type = JsonSchemaType.String, ContentEncoding = "base64" };

internal override JsonValueType GetSupportedJsonValueTypes(JsonNumberHandling _) => JsonValueType.String;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public override void Write(Utf8JsonWriter writer, ReadOnlyMemory<byte> value, Js
writer.WriteBase64StringValue(value.Span);
}

internal override JsonSchema? GetSchema(JsonNumberHandling _) => new() { Type = JsonSchemaType.String };
internal override JsonSchema? GetSchema(JsonNumberHandling _) => new() { Type = JsonSchemaType.String, ContentEncoding = "base64" };

internal override JsonValueType GetSupportedJsonValueTypes(JsonNumberHandling _) => JsonValueType.String;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ public static IEnumerable<ITestData> GetTestDataCore()
yield return new TestData<byte[]>(
Value: [1, 2, 3],
AdditionalValues: [[]],
ExpectedJsonSchema: """{"type":["string","null"]}""");
ExpectedJsonSchema: """{"type":["string","null"],"contentEncoding":"base64"}""");

yield return new TestData<Memory<byte>>(new byte[] { 1, 2, 3 }, ExpectedJsonSchema: """{"type":"string"}""");
yield return new TestData<ReadOnlyMemory<byte>>(new byte[] { 1, 2, 3 }, ExpectedJsonSchema: """{"type":"string"}""");
yield return new TestData<Memory<byte>>(new byte[] { 1, 2, 3 }, ExpectedJsonSchema: """{"type":"string","contentEncoding":"base64"}""");
yield return new TestData<ReadOnlyMemory<byte>>(new byte[] { 1, 2, 3 }, ExpectedJsonSchema: """{"type":"string","contentEncoding":"base64"}""");
yield return new TestData<DateTime>(
Value: new(2024, 06, 06, 21, 39, 42, DateTimeKind.Utc),
ExpectedJsonSchema: """{"type":"string","format":"date-time"}""");
Expand Down
Loading