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
83 changes: 41 additions & 42 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,64 +10,63 @@ on:
default: true
type: boolean
required: true
description: Set as pre-release
push:
default: false
type: boolean
description: Push to nuget.org

push:
branches: [ "main" ]
branches: ["master"]

jobs:
publish:

runs-on: ubuntu-latest
env:
PackageOutputDir: ".packages"
Prerelease: ${{ github.event.inputs.prerelease || 'false' }}
PushPackage: ${{ (github.event_name == 'push' && github.ref == 'refs/heads/master') || github.event.inputs.push }}

steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: 9.0.x

- name: Install GitVersion
uses: gittools/actions/gitversion/[email protected]
with:
versionSpec: '6.0.5'

- name: Determine Version
run: |
${GITVERSION_PATH}/dotnet-gitversion /output buildserver /output json

- name: Pack
run: |
dotnet pack --nologo -o ${PackageOutputDir} \
-p:PackageVersion=${GitVersion_SemVer} \
-p:AssemblyVersion=${GitVersion_AssemblySemFileVer} \
-p:FileVersion=${GitVersion_MajorMinorPatch} \
-p:AssemblyVersion=${GitVersion_AssemblySemVer} \
-p:FileVersion=${GitVersion_MajorMinorPatch} \
-p:Configuration=Release
- uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: 9.0.x

- name: Install GitVersion
uses: gittools/actions/gitversion/[email protected]
with:
versionSpec: "6.0.5"

- name: Determine Version
run: |
${GITVERSION_PATH}/dotnet-gitversion /output buildserver /output json

- name: Nuget Push
if: env.PushPackage == 'true'
working-directory: ${{ env.PackageOutputDir }}
run: dotnet nuget push *.nupkg -s https://api.nuget.org/v3/index.json -k ${{secrets.NUGET_API_KEY}}
- name: Pack
run: |
dotnet pack --nologo -o ${PackageOutputDir} \
-p:PackageVersion=${GitVersion_SemVer} \
-p:AssemblyVersion=${GitVersion_AssemblySemFileVer} \
-p:FileVersion=${GitVersion_MajorMinorPatch} \
-p:AssemblyVersion=${GitVersion_AssemblySemVer} \
-p:FileVersion=${GitVersion_MajorMinorPatch} \
-p:Configuration=Release

- name: Create Release
uses: ncipollo/release-action@v1
with:
name: v${{ env.GitVersion_SemVer }}
tag: v${{ env.GitVersion_SemVer }}
artifacts: ${{ env.PackageOutputDir }}/*.nupkg
generateReleaseNotes: true
commit: ${{ env.GitVersion_Sha}}
prerelease: ${{ env.Prerelease }}
- name: Nuget Push
if: env.PushPackage == 'true'
working-directory: ${{ env.PackageOutputDir }}
run: dotnet nuget push *.nupkg -s https://api.nuget.org/v3/index.json -k ${{secrets.NUGET_API_KEY}}

- name: Create Release
uses: ncipollo/release-action@v1
with:
name: v${{ env.GitVersion_SemVer }}
tag: v${{ env.GitVersion_SemVer }}
artifacts: ${{ env.PackageOutputDir }}/*.nupkg
generateReleaseNotes: true
commit: ${{ env.GitVersion_Sha}}
prerelease: ${{ env.Prerelease }}
168 changes: 90 additions & 78 deletions schemas/nswag.json
Original file line number Diff line number Diff line change
@@ -1,108 +1,120 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"runtime": {
"type": "string",
"enum": [
"Default",
"WinX64",
"WinX86",
"NetCore10",
"NetCore11",
"NetCore20",
"NetCore21",
"NetCore22",
"NetCore30",
"Debug"
]
},
"defaultVariables": {
"type": [
"string",
"null"
]
"allOf": [
{
"$ref": "#/definitions/nswagDocument"
},
"documentGenerator": {
{
"properties": {
"codeGenerators": {
"$ref": "#/definitions/codeGenerators"
}
}
}
],
"definitions": {
"nswagDocument": {
"type": "object",
"properties": {
"fromDocument": {
"runtime": {
"type": "string",
"enum": [
"Default",
"WinX64",
"WinX86",
"NetCore10",
"NetCore11",
"NetCore20",
"NetCore21",
"NetCore22",
"NetCore30",
"Debug"
]
},
"defaultVariables": {
"type": [
"string",
"null"
]
},
"documentGenerator": {
"type": "object",
"properties": {
"json": {
"type": "string",
"description": "Path to file or JSON text."
"fromDocument": {
"type": "object",
"properties": {
"json": {
"type": "string",
"description": "Path to file or JSON text."
},
"url": {
"type": "string",
"description": "Document URL. Dont used if 'json' property is set."
},
"output": {
"type": "null"
}
},
"anyOf": [
{
"required": [
"json"
]
},
{
"required": [
"url"
]
}
]
},
"url": {
"type": "string",
"description": "Document URL. Dont used if 'json' property is set."
"jsonSchemaToOpenApi": {
"properties": {
"name": {
"type": "string",
"description": "Name of schema`"
},
"schema": {
"type": "string",
"description": "Path to file or text of schema."
}
},
"required": [
"name",
"schema"
]
},
"output": {
"type": "null"
"preprocessors": {
"additionalProperties": {
"type": "object"
},
"description": "List of using preprocessors."
}
},
"anyOf": [
{
"required": [
"json"
"fromDocument"
]
},
{
"required": [
"url"
"jsonSchemaToOpenApi"
]
}
]
},
"jsonSchemaToOpenApi": {
"properties": {
"name": {
"type": "string",
"description": "Name of schema`"
},
"schema": {
"type": "string",
"description": "Path to file or text of schema."
{
"required": [
"preprocessors"
]
}
},
"required": [
"name",
"schema"
]
},
"preprocessors": {
"additionalProperties": {
"type": "object"
},
"description": "List of using preprocessors."
}
},
"anyOf": [
{
"required": [
"fromDocument"
]
},
{
"required": [
"jsonSchemaToOpenApi"
]
},
{
"required": [
"preprocessors"
]
}
"required": [
"codeGenerators"
]
},
"codeGenerators": {
"$ref": "#/definitions/codeGenerators"
}
},
"required": [
"codeGenerators"
],
"definitions": {
"codeGenerators": {
"description": "Generator used for code generation.",
"type": "object",
Expand Down
42 changes: 35 additions & 7 deletions src/ApiCodeGenerator.AsyncApi/DOM/AsyncApiDocument.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NJsonSchema;
using NJsonSchema.CodeGeneration;
using NJsonSchema.Generation;
using NJsonSchema.Yaml;
using YamlDotNet.Serialization;
Expand Down Expand Up @@ -59,11 +60,13 @@ public static Task<AsyncApiDocument> FromJsonAsync(string data)
/// <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)
public static async Task<AsyncApiDocument> FromJsonAsync(string data, string? documentPath)
{
var document = JsonConvert.DeserializeObject<AsyncApiDocument>(data, JSONSERIALIZERSETTINGS)!;
document.DocumentPath = documentPath;
return UpdateSchemaReferencesAsync(document);
await UpdateSchemaReferencesAsync(document);
BuildAsyncApiDescriminatorMapping(document);
return document;
}

/// <summary>
Expand All @@ -80,7 +83,7 @@ public static Task<AsyncApiDocument> FromYamlAsync(string data)
/// <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)
public static async Task<AsyncApiDocument> FromYamlAsync(string data, string? documentPath)
{
var deserializer = new DeserializerBuilder().Build();
using var reader = new StringReader(data);
Expand All @@ -90,15 +93,40 @@ public static Task<AsyncApiDocument> FromYamlAsync(string data, string? document
var serializer = JsonSerializer.Create(JSONSERIALIZERSETTINGS);
var doc = jObject.ToObject<AsyncApiDocument>(serializer)!;
doc.DocumentPath = documentPath;
return UpdateSchemaReferencesAsync(doc);
await UpdateSchemaReferencesAsync(doc);
BuildAsyncApiDescriminatorMapping(doc);
return doc;
}

private static async Task<AsyncApiDocument> UpdateSchemaReferencesAsync(AsyncApiDocument document)
private static Task UpdateSchemaReferencesAsync(AsyncApiDocument document)
{
await JsonSchemaReferenceUtilities.UpdateSchemaReferencesAsync(
return JsonSchemaReferenceUtilities.UpdateSchemaReferencesAsync(
document,
new JsonAndYamlReferenceResolver(new AsyncApiSchemaResolver(document, new SystemTextJsonSchemaGeneratorSettings())));
return document;
}

private static void BuildAsyncApiDescriminatorMapping(AsyncApiDocument document)
{
foreach (var schema in document.Components?.Schemas.Values ?? [])
{
var discriminatorPropName = schema.DiscriminatorObject?.PropertyName;
if (discriminatorPropName != null)
{
var derivedSchemas = schema.GetDerivedSchemas(document);
foreach (var item in derivedSchemas)
{
var derivedSchema = item.Key;
if ((derivedSchema.Properties.TryGetValue(discriminatorPropName, out var discriminatorProp)
|| derivedSchema.AllOf?.FirstOrDefault(i => i != schema && i.Properties.ContainsKey(discriminatorPropName))?.Properties.TryGetValue(discriminatorPropName, out discriminatorProp) == true)
&& discriminatorProp.ExtensionData?.TryGetValue("const", out var constValue) == true)
{
var constValueStr = constValue!.ToString();
discriminatorProp.ParentSchema!.Properties.Remove(discriminatorPropName);
schema.DiscriminatorObject!.Mapping.Add(constValueStr, derivedSchema);
}
}
}
}
}
}
#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
Expand Down
Loading
Loading