11using System ;
22using System . Collections . Generic ;
33using System . Diagnostics ;
4- using System . IO ;
54using System . Linq ;
65using System . Net . Http ;
7- using System . Text . Json ;
86using Corvus . Json ;
97using Microsoft . CodeAnalysis ;
108using Microsoft . CodeAnalysis . Text ;
@@ -22,12 +20,13 @@ public sealed class ApiGenerator : IIncrementalGenerator
2220 public void Initialize ( IncrementalGeneratorInitializationContext context )
2321 {
2422 // Debugger.Launch();
25-
2623 var provider = context . AdditionalTextsProvider
27- . Where ( additionalText => Path . GetFileName ( additionalText . Path ) . EndsWith ( ".json" ) )
24+ . Where ( additionalText => additionalText . IsOpenApiFileFormat ( ) )
2825 . Collect ( ) ;
2926
30- var openapiDocumentProvider = provider . Select ( ( array , _ ) => array . First ( ) ) ;
27+ var openapiDocumentProvider = provider . Select ( ( array , _ ) =>
28+ array . FirstOrDefault ( ) ??
29+ throw new InvalidOperationException ( $ "No OpenAPI specification found in AdditionalFiles. Expected any file with extension { string . Join ( " ," , AdditionalTextExtensions . OpenApiFileExtensions ) } ") ) ;
3130
3231 var openApiProvider = openapiDocumentProvider
3332 . Combine ( context . CompilationProvider )
@@ -48,40 +47,21 @@ private static void GenerateCode(SourceProductionContext context, (
4847 var rootNamespace = compilation . Assembly . Name ;
4948
5049 var openApiDocumentFile = generatorContext . OpenApiDocument ;
51- var jsonValidationExceptionGenerator = new JsonValidationExceptionGenerator ( rootNamespace ) ;
52- jsonValidationExceptionGenerator . GenerateJsonValidationExceptionClass ( ) . AddTo ( context ) ;
50+ var openApiDocumentStream = openApiDocumentFile . AsOpenApiStream ( ) ;
5351
54- var endpointGenerator = new OperationGenerator ( compilation , jsonValidationExceptionGenerator ) ;
55- var openApiResult = OpenApiDocument . Load ( openApiDocumentFile . AsStream ( ) , "json" ) ;
56- var openApiVersion = openApiResult . Diagnostic ? . SpecificationVersion ??
57- throw new InvalidOperationException ( "Unknown openapi version" ) ;
58- if ( openApiResult . Diagnostic . Errors . Any ( ) )
59- {
60- throw new InvalidOperationException (
61- openApiResult . Diagnostic . Errors . AggregateToString (
62- "Errors while parsing OpenAPI specification: " ,
63- error => $ "{ ( error . Pointer == null ? "" : $ "{ error . Pointer } : ") } { error . Message } ") ) ;
64- }
65- var openApi = openApiResult . Document ??
66- throw new InvalidOperationException (
67- $ "Could not load OpenAPI document { openApiDocumentFile . Path } ") ;
52+ var openApiSpecification = openApiDocumentStream . LoadOpenApiDocument ( ) ;
53+ var openApiVersion = openApiSpecification . Version ;
54+ var openApi = openApiSpecification . Document ;
6855
69-
70- var openApiUri = new JsonReference ( openApi . BaseUri . ToString ( ) ) ;
71- var documentResolver = new PrepopulatedDocumentResolver ( ) ;
72- var openApiDocument = JsonDocument . Parse ( generatorContext . OpenApiDocument . AsStream ( ) ) ;
73- if ( ! documentResolver . AddDocument ( openApiUri , openApiDocument ) )
74- {
75- throw new InvalidOperationException ( "Could not add OpenApi document" ) ;
76- }
7756 var schemaGenerator = SchemaGenerator . For (
78- openApiVersion ,
79- documentResolver ,
80- rootNamespace ,
81- context ) ;
57+ openApiSpecification , rootNamespace , context ) ;
8258
83- var openApiReference = new OpenApiReference < OpenApiDocument > ( openApi , openApiDocument , openApiUri ) ;
84- var openApiVisitor = OpenApiVisitor . V ( openApiVersion , openApiReference ) ;
59+ var openApiVisitor = OpenApiVisitor . V ( openApiSpecification ) ;
60+
61+ var jsonValidationExceptionGenerator = new JsonValidationExceptionGenerator ( rootNamespace ) ;
62+ jsonValidationExceptionGenerator . GenerateJsonValidationExceptionClass ( ) . AddTo ( context ) ;
63+
64+ var endpointGenerator = new OperationGenerator ( compilation , jsonValidationExceptionGenerator ) ;
8565
8666 var httpRequestExtensionsGenerator = new HttpRequestExtensionsGenerator (
8767 openApiVersion ,
0 commit comments