Skip to content
Merged
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 @@ -27,7 +27,8 @@

<ItemGroup>
<PackageReference Include="NJsonSchema.CodeGeneration.CSharp" Version="$(NJsonSchemaVersion)" />
<PackageReference Include="YamlDotNet" Version="11.2.1" />
<PackageReference Include="NJsonSchema.Yaml" Version="$(NJsonSchemaVersion)" />
<PackageReference Include="YamlDotNet" Version="16.2.0" />
</ItemGroup>

<ItemGroup>
Expand Down
21 changes: 17 additions & 4 deletions src/ApiCodeGenerator.AsyncApi/AsyncApiContentGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,23 @@ private static async Task<AsyncApiDocument> LoadDocumentAsync(GeneratorContext c
var data = await context.DocumentReader!.ReadToEndAsync();
data = InvokePreprocessors<string>(data, context.Preprocessors, context.DocumentPath, context.Logger);

var documentTask = data.StartsWith("{")
? AsyncApiDocument.FromJsonAsync(data)
: AsyncApiDocument.FromYamlAsync(data);
var document = await documentTask.ConfigureAwait(false);
AsyncApiDocument document;
try
{
document = await AsyncApiDocument.FromJsonAsync(data, context.DocumentPath).ConfigureAwait(false);
}
catch (JsonException ex)
{
try
{
document = await AsyncApiDocument.FromYamlAsync(data, context.DocumentPath).ConfigureAwait(false);
}
catch (YamlDotNet.Core.YamlException ex2)
{
throw new InvalidOperationException(
$"Can not read document as JSON ({ex.Message}) or YAML ({ex2.Message}).");
}
}

document = InvokePreprocessors<AsyncApiDocument>(document, context.Preprocessors, context.DocumentPath, context.Logger);
return document;
Expand Down
38 changes: 32 additions & 6 deletions src/ApiCodeGenerator.AsyncApi/DOM/AsyncApiDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
using Newtonsoft.Json.Linq;
using NJsonSchema;
using NJsonSchema.Generation;
using NJsonSchema.Yaml;
using YamlDotNet.Serialization;

namespace ApiCodeGenerator.AsyncApi.DOM
{
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
public class AsyncApiDocument
public class AsyncApiDocument : IDocumentPathProvider
{
private static readonly JsonSerializerSettings JSONSERIALIZERSETTINGS = new()
{
Expand Down Expand Up @@ -41,14 +42,27 @@ public class AsyncApiDocument
[JsonProperty("externalDocs", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
public ICollection<ExternalDocumentation>? ExternalDocs { get; set; }

[JsonIgnore]
public string? DocumentPath { get; set; }

/// <summary>
/// Load document from JSON text.
/// </summary>
/// <param name="data">JSON text.</param>
/// <returns>AsyncApi document object model.</returns>
public static Task<AsyncApiDocument> FromJsonAsync(string data)
=> FromJsonAsync(data, null);

/// <summary>
/// Load document from JSON text.
/// </summary>
/// <param name="data">JSON text.</param>
/// <param name="documentPath"> Path to document. </param>
/// <returns>AsyncApi document object model.</returns>
public static Task<AsyncApiDocument> FromJsonAsync(string data, string? documentPath)
{
var document = JsonConvert.DeserializeObject<AsyncApiDocument>(data, JSONSERIALIZERSETTINGS)!;
document.DocumentPath = documentPath;
return UpdateSchemaReferencesAsync(document);
}

Expand All @@ -58,6 +72,15 @@ public static Task<AsyncApiDocument> FromJsonAsync(string data)
/// <param name="data">YAML text.</param>
/// <returns>AsyncApi document object model.</returns>
public static Task<AsyncApiDocument> FromYamlAsync(string data)
=> FromYamlAsync(data, null);

/// <summary>
/// Load document from YAML text.
/// </summary>
/// <param name="data">YAML text.</param>
/// <param name="documentPath"> Path to document. </param>
/// <returns>AsyncApi document object model.</returns>
public static Task<AsyncApiDocument> FromYamlAsync(string data, string? documentPath)
{
var deserializer = new DeserializerBuilder().Build();
using var reader = new StringReader(data);
Expand All @@ -66,14 +89,17 @@ public static Task<AsyncApiDocument> FromYamlAsync(string data)
var jObject = JObject.FromObject(yamlDocument)!;
var serializer = JsonSerializer.Create(JSONSERIALIZERSETTINGS);
var doc = jObject.ToObject<AsyncApiDocument>(serializer)!;
doc.DocumentPath = documentPath;
return UpdateSchemaReferencesAsync(doc);
}

private static Task<AsyncApiDocument> UpdateSchemaReferencesAsync(AsyncApiDocument document)
=> JsonSchemaReferenceUtilities.UpdateSchemaReferencesAsync(
document,
new(new AsyncApiSchemaResolver(document, new SystemTextJsonSchemaGeneratorSettings())))
.ContinueWith(t => document);
private static async Task<AsyncApiDocument> UpdateSchemaReferencesAsync(AsyncApiDocument document)
{
await JsonSchemaReferenceUtilities.UpdateSchemaReferencesAsync(
document,
new JsonAndYamlReferenceResolver(new AsyncApiSchemaResolver(document, new SystemTextJsonSchemaGeneratorSettings())));
return document;
}
}
#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
}
3 changes: 2 additions & 1 deletion src/ApiCodeGenerator.OpenApi/ApiCodeGenerator.OpenApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@
<ItemGroup>
<PackageReference Include="NSwag.CodeGeneration.CSharp" Version="$(NswagVersion)" />
<PackageReference Include="NSwag.Core.Yaml" Version="$(NswagVersion)" />
<PackageReference Include="YamlDotNet" Version="16.2.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\ApiCodeGenerator.Abstraction\ApiCodeGenerator.Abstraction.csproj" />
</ItemGroup>
</Project>
</Project>
22 changes: 19 additions & 3 deletions src/ApiCodeGenerator.OpenApi/ContentGeneratorBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using NSwag;
using NSwag.CodeGeneration;
using NSwag.CodeGeneration.CSharp;
using YamlDotNet.Core;

namespace ApiCodeGenerator.OpenApi
{
Expand Down Expand Up @@ -126,9 +127,24 @@ protected static async Task<OpenApiDocument> ReadAndProcessOpenApiDocument(Gener
var documentStr = context.DocumentReader!.ReadToEnd();
documentStr = InvokePreprocessors<string>(documentStr, context.Preprocessors, context.DocumentPath, context.Logger);

var openApiDocument = !(documentStr.StartsWith("{") && documentStr.EndsWith("}"))
? await OpenApiYamlDocument.FromYamlAsync(documentStr)
: await OpenApiDocument.FromJsonAsync(documentStr);
OpenApiDocument openApiDocument;

try
{
openApiDocument = await OpenApiDocument.FromJsonAsync(documentStr, context.DocumentPath);
}
catch (JsonException ex)
{
try
{
openApiDocument = await OpenApiYamlDocument.FromYamlAsync(documentStr, context.DocumentPath);
}
catch (YamlException ex2)
{
throw new InvalidOperationException(
$"Can not read document as JSON ({ex.Message}) or YAML ({ex2.Message}).");
}
}

openApiDocument = InvokePreprocessors<OpenApiDocument>(openApiDocument, context.Preprocessors, context.DocumentPath, context.Logger);
return openApiDocument;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,22 @@ public async Task LoadApiDocument_WithModelPreprocess()
Assert.That(sch, Is.EqualTo("{\"$schema\":\"http://json-schema.org/draft-04/schema#\",\"properties\":{\"processedModel\":{}}}"));
}

[TestCase("externalRef.json")]
[TestCase("externalRef.yaml")]
public async Task LoadApiDocument_WithExternalRef(string documentPath)
{
var settingsJson = new JObject();
var context = CreateContext(settingsJson);
context.DocumentReader = await TestHelpers.LoadApiDocumentAsync(documentPath);
context.DocumentPath = documentPath;

var contentGenerator = (FakeContentGenerator)await FakeContentGenerator.CreateAsync(context);

var document = contentGenerator.Document;

Assert.NotNull(document.Components?.Messages["lightMeasured"].Reference);
}

private static Func<Type, Newtonsoft.Json.JsonSerializer?, IReadOnlyDictionary<string, string>?, object?> GetSettingsFactory(string json)
=> (t, s, v) => (s ?? new()).Deserialize(new StringReader(json), t);

Expand Down
15 changes: 15 additions & 0 deletions test/ApiCodeGenerator.AsyncApi.Tests/asyncApi/externalRef.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"asyncapi": "2.6.0",
"info": {
"title": "Document with reference to external schema",
"version": "1.0"
},
"channels": {},
"components": {
"messages": {
"lightMeasured": {
"$ref": "asyncapi.json#/components/messages/lightMeasured"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
asyncapi: "2.6.0"
info:
title: Document with reference to external schema
version: "1.0"
channels: {}
components:
messages:
lightMeasured:
$ref: asyncapi.yml#/components/messages/lightMeasured
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,32 @@ public async Task LoadOpenApiDocument_WithModelPreprocess()
public async Task LoadOpenApiDocument_FromYaml()
{
var context = CreateContext(new());
var schemaText = await File.ReadAllTextAsync(TestHelpers.GetSwaggerPath("testSchema.yaml"));
context.DocumentPath = TestHelpers.GetSwaggerPath("testSchema.yaml");
var schemaText = await File.ReadAllTextAsync(context.DocumentPath);
context.DocumentReader = new StringReader(schemaText);

var gen = (CSharpClientContentGenerator)await CSharpClientContentGenerator.CreateAsync(context);

var openApiDocument = GetDocument(gen.Generator);

Assert.NotNull(openApiDocument);
Assert.NotNull(openApiDocument!.DocumentPath);
}

[TestCase("externalRef.json")]
public async Task LoadOpenApiDocument_ExternalRef(string documentPath)
{
var context = CreateContext(new());
context.DocumentPath = TestHelpers.GetSwaggerPath(documentPath);
var documentContent = await File.ReadAllTextAsync(context.DocumentPath);
context.DocumentReader = new StringReader(documentContent);

var gen = (CSharpClientContentGenerator)await CSharpClientContentGenerator.CreateAsync(context);

var openApiDocument = GetDocument(gen.Generator);

Assert.NotNull(openApiDocument);
Assert.NotNull(openApiDocument!.Definitions["test"].AllOf.Single().Reference);
}

private GeneratorContext CreateContext(JObject settingsJson, Core.ExtensionManager.Extensions? extension = null)
Expand Down
19 changes: 19 additions & 0 deletions test/ApiCodeGenerator.OpenApi.Tests/swagger/externalRef.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"openapi": "3.0.0",
"components": {
"schemas": {
"test": {
"allOf": [
{
"$ref": "testSchema.json#/definitions/testOperResponse"
}
],
"properties": {
"prop": {
"type": "integer"
}
}
}
}
}
}
Loading