-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix code generation enum handling (#836)
Signed-off-by: Thomas Farr <[email protected]>
- Loading branch information
Showing
5 changed files
with
611 additions
and
434 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
using System.Collections.Generic; | ||
using System.Collections.Immutable; | ||
using System.Linq; | ||
using NJsonSchema; | ||
|
||
namespace ApiGenerator.Generator; | ||
|
||
public static class OpenApiUtils | ||
{ | ||
public static bool HasOneOf(this JsonSchema schema) => schema.OneOf.Count > 0; | ||
|
||
public static bool IsEnum(this JsonSchema schema) | ||
{ | ||
if (schema.Type == JsonObjectType.String) | ||
return schema.Enumeration.Count > 0 || schema.Const<string>() != null; | ||
|
||
return schema.HasOneOf() && schema.OneOf.All(IsEnum); | ||
} | ||
|
||
public static IImmutableList<string> GetEnumValues(this JsonSchema schema) | ||
{ | ||
var values = new SortedSet<string>(); | ||
Visit(schema); | ||
return values.ToImmutableList(); | ||
|
||
void Visit(JsonSchema s) | ||
{ | ||
if (s.HasOneOf()) | ||
foreach (var oneOf in schema.OneOf) Visit(oneOf); | ||
else if (s.Enumeration.Count > 0) | ||
foreach (var v in s.Enumeration.Where(v => v != null)) values.Add((string) v); | ||
else if (s.Const<string>() != null) | ||
values.Add(s.Const<string>()); | ||
} | ||
} | ||
|
||
public static T Const<T>(this JsonSchema schema) where T: class => | ||
schema.GetExtension("const") as T; | ||
|
||
public static object GetExtension(this IJsonExtensionObject schema, string key) => | ||
schema.ExtensionData?.TryGetValue(key, out var value) ?? false ? value : null; | ||
} |
Oops, something went wrong.