Skip to content

Commit 846fa34

Browse files
committed
generate schemas from v3 specs
1 parent 788cb0e commit 846fa34

3 files changed

Lines changed: 41 additions & 26 deletions

File tree

src/OpenAPI.WebApiGenerator/ApiGenerator.cs

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Collections.Immutable;
43
using System.Diagnostics;
54
using System.IO;
65
using System.Linq;
76
using System.Net.Http;
87
using System.Text.Json;
98
using Corvus.Json;
10-
using Corvus.Json.SourceGeneratorTools;
119
using Microsoft.CodeAnalysis;
1210
using Microsoft.CodeAnalysis.Text;
1311
using Microsoft.OpenApi;
@@ -31,35 +29,21 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
3129

3230
var openapiDocumentProvider = provider.Select((array, _) => array.First());
3331

34-
// Get global options
35-
var globalOptions =
36-
context.AnalyzerConfigOptionsProvider.Select((optionsProvider, token) =>
37-
new SourceGeneratorHelpers.GlobalOptions(
38-
fallbackVocabulary: Corvus.Json.CodeGeneration.Draft4.VocabularyAnalyser.DefaultVocabulary,
39-
optionalAsNullable: true,
40-
useOptionalNameHeuristics: true,
41-
alwaysAssertFormat: true,
42-
ImmutableArray<string>.Empty));
43-
44-
var openApiProvider = globalOptions
45-
.Combine(openapiDocumentProvider)
32+
var openApiProvider = openapiDocumentProvider
4633
.Combine(context.CompilationProvider)
4734
.Select((tuple, _) => (
48-
Options: tuple.Left.Left,
49-
OpenApiDocument: tuple.Left.Right,
35+
OpenApiDocument: tuple.Left,
5036
Compilation: tuple.Right
5137
));
5238

5339
context.RegisterSourceOutput(openApiProvider,
54-
WithExceptionReporting<(SourceGeneratorHelpers.GlobalOptions, AdditionalText, Compilation)>(GenerateCode));
40+
WithExceptionReporting<(AdditionalText, Compilation)>(GenerateCode));
5541
}
5642

5743
private static void GenerateCode(SourceProductionContext context, (
58-
SourceGeneratorHelpers.GlobalOptions Options,
5944
AdditionalText OpenApiDocument,
6045
Compilation Compilation) generatorContext)
6146
{
62-
var globalOptions = generatorContext.Options;
6347
var compilation = generatorContext.Compilation;
6448
var rootNamespace = compilation.Assembly.Name;
6549

@@ -82,18 +66,19 @@ private static void GenerateCode(SourceProductionContext context, (
8266
throw new InvalidOperationException(
8367
$"Could not load OpenAPI document {openApiDocumentFile.Path}");
8468

69+
8570
var openApiUri = new JsonReference(openApi.BaseUri.ToString());
8671
var documentResolver = new PrepopulatedDocumentResolver();
8772
var openApiDocument = JsonDocument.Parse(generatorContext.OpenApiDocument.AsStream());
8873
if (!documentResolver.AddDocument(openApiUri, openApiDocument))
8974
{
9075
throw new InvalidOperationException("Could not add OpenApi document");
9176
}
92-
var generationContext = new SourceGeneratorHelpers.GenerationContext(documentResolver, globalOptions);
93-
var schemaGenerator = new SchemaGenerator(
94-
rootNamespace,
95-
context,
96-
generationContext);
77+
var schemaGenerator = SchemaGenerator.For(
78+
openApiVersion,
79+
documentResolver,
80+
rootNamespace,
81+
context);
9782

9883
var openApiReference = new OpenApiReference<OpenApiDocument>(openApi, openApiDocument, openApiUri);
9984
var openApiVisitor = OpenApiVisitor.V(openApiVersion, openApiReference);

src/OpenAPI.WebApiGenerator/CodeGeneration/SchemaGenerator.cs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,55 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Collections.Immutable;
34
using System.IO;
45
using System.Linq;
56
using Corvus.Json;
67
using Corvus.Json.CodeGeneration;
78
using Corvus.Json.CodeGeneration.CSharp;
89
using Corvus.Json.SourceGeneratorTools;
910
using Microsoft.CodeAnalysis;
11+
using Microsoft.OpenApi;
1012
using OpenAPI.WebApiGenerator.OpenApi;
1113
using JsonPointer = OpenAPI.WebApiGenerator.Json.JsonPointer;
1214

1315
namespace OpenAPI.WebApiGenerator.CodeGeneration;
1416

15-
internal sealed class SchemaGenerator(string rootNamespace,
17+
internal sealed class SchemaGenerator(
18+
string rootNamespace,
1619
SourceProductionContext context,
1720
SourceGeneratorHelpers.GenerationContext generationContext)
1821
{
1922
private static readonly IDocumentResolver MetaSchemaResolver = SourceGeneratorHelpers.CreateMetaSchemaResolver();
2023
private static readonly VocabularyRegistry VocabularyRegistry = SourceGeneratorHelpers.CreateVocabularyRegistry(MetaSchemaResolver);
2124
private readonly Dictionary<string, TypeDeclaration> _typeCache = new();
2225
private readonly HashSet<string> _fileCache = [];
26+
27+
internal static SchemaGenerator For(
28+
OpenApiSpecVersion openApiSpecVersion,
29+
IDocumentResolver documentResolver,
30+
string rootNamespace,
31+
SourceProductionContext context)
32+
{
33+
var vocabulary = openApiSpecVersion switch
34+
{
35+
OpenApiSpecVersion.OpenApi2_0 =>
36+
Corvus.Json.CodeGeneration.Draft4.VocabularyAnalyser.DefaultVocabulary,
37+
OpenApiSpecVersion.OpenApi3_0 =>
38+
Corvus.Json.CodeGeneration.OpenApi30.VocabularyAnalyser.DefaultVocabulary,
39+
OpenApiSpecVersion.OpenApi3_1 =>
40+
Corvus.Json.CodeGeneration.Draft202012.VocabularyAnalyser.DefaultVocabulary,
41+
_ => throw new InvalidOperationException($"OpenAPI specification {openApiSpecVersion} is not supported")
42+
};
43+
var globalOptions =
44+
new SourceGeneratorHelpers.GlobalOptions(
45+
fallbackVocabulary: vocabulary,
46+
optionalAsNullable: true,
47+
useOptionalNameHeuristics: true,
48+
alwaysAssertFormat: true,
49+
ImmutableArray<string>.Empty);
50+
var generationContext = new SourceGeneratorHelpers.GenerationContext(documentResolver, globalOptions);
51+
return new SchemaGenerator(rootNamespace, context, generationContext);
52+
}
2353

2454
internal TypeDeclaration Generate(JsonReference reference)
2555
{

src/OpenAPI.WebApiGenerator/OpenAPI.WebApiGenerator.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252

5353
<ItemGroup>
5454
<PackageReference Include="Microsoft.Bcl.HashCode" Version="1.1.1" PrivateAssets="all" GeneratePathProperty="true" />
55-
<PackageReference Include="Corvus.Json.SourceGeneratorTools" Version="4.4.2" PrivateAssets="all" GeneratePathProperty="true" />
55+
<PackageReference Include="Corvus.Json.SourceGeneratorTools" Version="4.4.3" PrivateAssets="all" GeneratePathProperty="true" />
5656
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="4.14.0">
5757
<PrivateAssets>all</PrivateAssets>
5858
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>

0 commit comments

Comments
 (0)