Skip to content
Open
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 @@ -76,13 +76,14 @@
</ItemGroup>

<ItemGroup>
<!-- System.Text.Json 8.x has a dependency on Microsoft.Bcl.AsyncInterfaces 8.0.0 -->
<!-- System.Text.Json 9.x has a dependency on Microsoft.Bcl.AsyncInterfaces 9.0.0 -->
<!-- Roslyn 4.10 has a dependency on AsyncInterfaces 8.0.0: https://github.com/dotnet/roslyn/blob/3bdcd8b471bef9a522c16a0ada911eb9890b1a6c/eng/Versions.props#L39 -->
<!-- So, stick with 8.x of STJ. -->
<PackageReference Include="System.Text.Json" Version="8.0.6" PrivateAssets="all" GeneratePathProperty="true" />
<!-- System.Text.Json 8.x has a dependency on Microsoft.Bcl.AsyncInterfaces 8.0.0 -->
<!-- System.Text.Json 9.x has a dependency on Microsoft.Bcl.AsyncInterfaces 9.0.0 -->
<!-- Roslyn 4.10 has a dependency on AsyncInterfaces 8.0.0: https://github.com/dotnet/roslyn/blob/3bdcd8b471bef9a522c16a0ada911eb9890b1a6c/eng/Versions.props#L39 -->
<!-- So, stick with 8.x of STJ. -->
<PackageReference Include="System.Text.Json" Version="8.0.6" PrivateAssets="all" GeneratePathProperty="true" />

<PackageReference Include="Microsoft.Bcl.HashCode" Version="1.1.1" PrivateAssets="all" GeneratePathProperty="true" />
<PackageReference Include="YamlDotNet" Version="16.3.0" PrivateAssets="all" GeneratePathProperty="true" />
</ItemGroup>

<PropertyGroup>
Expand All @@ -93,6 +94,7 @@
<ItemGroup>
<TargetPathWithTargetPlatformMoniker Include="$(PKGSystem_Text_Json)\lib\netstandard2.0\System.Text.Json.dll" IncludeRuntimeDependency="false" />
<TargetPathWithTargetPlatformMoniker Include="$(PKGMicrosoft_Bcl_HashCode)\lib\netstandard2.0\Microsoft.Bcl.HashCode.dll" IncludeRuntimeDependency="false" />
<TargetPathWithTargetPlatformMoniker Include="$(PKGYamlDotNet)\lib\netstandard2.0\YamlDotNet.dll" IncludeRuntimeDependency="false" />
</ItemGroup>
</Target>

Expand Down Expand Up @@ -130,8 +132,9 @@
<ItemGroup>
<!-- Package the generator in the analyzer directory of the nuget package -->
<None Include="$(OutputPath)\$(AssemblyName).dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
<None Include="$(PKGSystem_Text_Json)\lib\netstandard2.0\System.Text.Json.dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
<None Include="$(PKGMicrosoft_Bcl_HashCode)\lib\netstandard2.0\Microsoft.Bcl.HashCode.dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
<None Include="$(PKGSystem_Text_Json)\lib\netstandard2.0\System.Text.Json.dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
<None Include="$(PKGMicrosoft_Bcl_HashCode)\lib\netstandard2.0\Microsoft.Bcl.HashCode.dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
<None Include="$(PKGYamlDotNet)\lib\netstandard2.0\YamlDotNet.dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
<!-- Package the props file -->
<None Include="Corvus.Json.SourceGenerator.props" Pack="true" PackagePath="build" Visible="false" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Text;
using Yaml = YamlDotNet.Serialization;

namespace Corvus.Json.SourceGenerator;

Expand Down Expand Up @@ -44,14 +45,25 @@ public class IncrementalSourceGenerator : IIncrementalGenerator

private static readonly IVocabulary Corvus202012Vocab = CodeGeneration.Draft202012.VocabularyAnalyser.DefaultVocabularyWith([CodeGeneration.CorvusVocabulary.SchemaVocabulary.DefaultInstance]);

private static readonly Lazy<Yaml.IDeserializer> YamlDeserializer =
new(static () => new Yaml.DeserializerBuilder().WithAttemptingUnquotedStringTypeDeserialization().Build());

private static readonly Lazy<Yaml.ISerializer> YamlSerializer =
new(static () => new Yaml.SerializerBuilder().JsonCompatible().Build());

/// <inheritdoc/>
public void Initialize(IncrementalGeneratorInitializationContext initializationContext)
{
EmitGeneratorAttribute(initializationContext);

IncrementalValueProvider<GlobalOptions> globalOptions = initializationContext.AnalyzerConfigOptionsProvider.Select(GetGlobalOptions);

IncrementalValuesProvider<AdditionalText> jsonSourceFiles = initializationContext.AdditionalTextsProvider.Where(p => p.Path.EndsWith(".json"));
IncrementalValuesProvider<AdditionalText> jsonSourceFiles = initializationContext.AdditionalTextsProvider
.Where(p =>
{
string path = p.Path;
return path.EndsWith(".json") || path.EndsWith(".yaml") || path.EndsWith(".yml");
});

IncrementalValueProvider<CompoundDocumentResolver> documentResolver = jsonSourceFiles.Collect().Select(BuildDocumentResolver);

Expand Down Expand Up @@ -304,6 +316,17 @@ private static CompoundDocumentResolver BuildDocumentResolver(ImmutableArray<Add
{
try
{
string extensions = Path.GetExtension(additionalText.Path);
if (string.Equals(extensions, ".yaml", StringComparison.InvariantCultureIgnoreCase)
|| string.Equals(extensions, ".yml", StringComparison.InvariantCultureIgnoreCase))
{
object? yamlObject = YamlDeserializer.Value.Deserialize(json);

using var writer = new StringWriter();
YamlSerializer.Value.Serialize(writer, yamlObject);
j = writer.ToString();
}

var doc = JsonDocument.Parse(j);
if (SchemaReferenceNormalization.TryNormalizeSchemaReference(additionalText.Path, string.Empty, out string? normalizedReference))
{
Expand All @@ -322,6 +345,10 @@ private static CompoundDocumentResolver BuildDocumentResolver(ImmutableArray<Add
{
// We ignore bad JSON files.
}
catch (YamlDotNet.Core.YamlException)
{
// We ignore bad YAML files.
}
}
}

Expand Down
8 changes: 8 additions & 0 deletions Solutions/Sandbox.SourceGenerator/Model/FlimFlamYaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using Corvus.Json;

namespace Sandbox.SourceGenerator.Model;

[JsonSchemaTypeGenerator("../test.yaml#/$defs/FlimFlamYaml")]
internal readonly partial struct FlimFlamYaml
{
}
8 changes: 8 additions & 0 deletions Solutions/Sandbox.SourceGenerator/Model/Test2Yml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using Corvus.Json;

namespace Sandbox.SourceGenerator.ModelYaml;

[JsonSchemaTypeGenerator("../test2.yml")]
internal readonly partial struct Test2Yml
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
<ItemGroup>
<AdditionalFiles Include="Model\keycloak-realm-26.0.2.json" />
<AdditionalFiles Include="test.json" />
<AdditionalFiles Include="test.yaml" />
<AdditionalFiles Include="test2.json" />
<AdditionalFiles Include="test2.yml" />
</ItemGroup>

<ItemGroup>
Expand Down
17 changes: 17 additions & 0 deletions Solutions/Sandbox.SourceGenerator/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
$schema: https://corvus-oss.org/json-schema/2020-12/schema
$defs:
FlimFlamYaml:
type: object
properties:
theArray: { $ref: '#/%24defs/someArrayYaml' }
someArrayYaml:
type: array
prefixItems:
- $corvusTypeName: PositiveInt32
type: integer
format: int32
minimum: 0
- { "type": "string", "minLength": 0 }
- type: number
minimum: 3.5
unevaluatedItems: false
21 changes: 21 additions & 0 deletions Solutions/Sandbox.SourceGenerator/test2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
$schema: http://json-schema.org/draft-07/schema
type: object
anyOf:
- type: object
required: [ "kind", 'data' ]
additionalProperties: false
properties:
kind: { const: foo }
data:
anyOf:
- type: object
required: [ kind ]
additionalProperties: false
properties:
kind: { const: "bar" }
- type: object
required: [ kind ]
additionalProperties: false
properties:
kind: { const: "barz" }
- $ref: '#/anyOf/0/properties/data/anyOf/0'
Loading