Skip to content

Commit 3e4c377

Browse files
authored
Merge pull request #5 from pregress/feat_examples
feat: examples
2 parents 6a173a5 + bc01498 commit 3e4c377

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

src/Program.cs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
var openApiDoc = deserializer.Deserialize<object>(yamlContent);
5858

5959
RemoveLinksRecursive(openApiDoc);
60+
ReplaceExamplesRecursive(openApiDoc);
6061

6162
var modifiedYaml = serializer.Serialize(openApiDoc);
6263

@@ -82,7 +83,9 @@
8283
static void RemoveLinksRecursive(object? node)
8384
{
8485
if (node is null)
86+
{
8587
return;
88+
}
8689

8790
if (node is System.Collections.IDictionary dict)
8891
{
@@ -103,4 +106,78 @@ static void RemoveLinksRecursive(object? node)
103106
RemoveLinksRecursive(item);
104107
}
105108
}
109+
}
110+
111+
static void ReplaceExamplesRecursive(object? node)
112+
{
113+
if (node == null)
114+
return;
115+
116+
if (node is System.Collections.IDictionary dict)
117+
{
118+
var keys = new List<object?>(dict.Keys.Cast<object?>());
119+
120+
foreach (var key in keys)
121+
{
122+
string? keyStr = key?.ToString();
123+
if (keyStr != null && string.Equals(keyStr, "examples", StringComparison.OrdinalIgnoreCase))
124+
{
125+
var examplesValue = dict[key];
126+
if (examplesValue != null)
127+
{
128+
object? firstExample = null;
129+
130+
// Handle different types that YamlDotNet might use
131+
if (examplesValue is System.Collections.IDictionary examplesDict && examplesDict.Count > 0)
132+
{
133+
// Get first example from dictionary
134+
var firstKey = examplesDict.Keys.Cast<object?>().FirstOrDefault();
135+
if (firstKey != null)
136+
{
137+
firstExample = examplesDict[firstKey];
138+
}
139+
}
140+
else if (examplesValue is System.Collections.IList examplesList && examplesList.Count > 0)
141+
{
142+
// Get first example from list
143+
firstExample = examplesList[0];
144+
}
145+
else
146+
{
147+
// Just use the value directly as the example
148+
firstExample = examplesValue;
149+
}
150+
151+
if (firstExample != null)
152+
{
153+
// Remove the "examples" key and add "example" with the first example
154+
dict.Remove(key);
155+
dict["example"] = firstExample;
156+
}
157+
}
158+
}
159+
else if (dict[key] != null)
160+
{
161+
// Recursively process this value
162+
ReplaceExamplesRecursive(dict[key]);
163+
}
164+
}
165+
}
166+
// For lists/arrays in YAML
167+
else if (node is System.Collections.IList list)
168+
{
169+
// Process each item in the list
170+
foreach (var item in list)
171+
{
172+
ReplaceExamplesRecursive(item);
173+
}
174+
}
175+
// For dictionary values we need to examine (could be wrapped in another type)
176+
else if (node is System.Collections.IEnumerable enumerable && !(node is string))
177+
{
178+
foreach (var item in enumerable)
179+
{
180+
ReplaceExamplesRecursive(item);
181+
}
182+
}
106183
}

0 commit comments

Comments
 (0)