forked from asyncapi/saunter
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
asyncapi#207 generate code from spec: yml support + generate extensio…
…n for AsyncApi document info
- Loading branch information
Senn Geerts
authored and
Senn Geerts
committed
Jul 20, 2024
1 parent
23b8e4c
commit 8f6dd34
Showing
6 changed files
with
106 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 25 additions & 12 deletions
37
src/AsyncAPI.Saunter.Generator.Cli/FromSpec/DataTypes/OpenApiCompatibility.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,36 @@ | ||
using Newtonsoft.Json.Linq; | ||
using Newtonsoft.Json; | ||
using System.Diagnostics; | ||
using System.Text.Json; | ||
using System.Text.Json.Nodes; | ||
using YamlDotNet.RepresentationModel; | ||
using Yaml2JsonNode; | ||
|
||
namespace AsyncAPI.Saunter.Generator.Cli.FromSpec.DataTypes; | ||
|
||
internal static class OpenApiCompatibility | ||
{ | ||
internal static string PrepareSpecFile(string spec) | ||
{ | ||
var json = (JObject)JsonConvert.DeserializeObject(spec); | ||
// the type is important for NSwag | ||
if (!json.ContainsKey("openapi")) | ||
{ | ||
json.Add("openapi", "3.0.1"); | ||
} | ||
// NSwag doesn't understand the servers format of AsyncApi, and it is not needed anyway. | ||
if (json.ContainsKey("servers")) | ||
Debugger.Launch(); | ||
var reader = new StringReader(spec); | ||
var yamlStream = new YamlStream(); | ||
yamlStream.Load(reader); | ||
|
||
if (yamlStream.Documents[0].ToJsonNode() is JsonObject json) | ||
{ | ||
json.Remove("servers"); | ||
// the type is important for NSwag | ||
if (!json.ContainsKey("openapi")) | ||
{ | ||
json.Add("openapi", "3.0.1"); | ||
} | ||
|
||
//// NSwag doesn't understand the servers format of AsyncApi, not needed. | ||
if (json.ContainsKey("servers")) | ||
{ | ||
json.Remove("servers"); | ||
} | ||
return JsonSerializer.Serialize(json); | ||
} | ||
return JsonConvert.SerializeObject(json); | ||
|
||
return spec; | ||
} | ||
} |