|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +#if NET7_0_OR_GREATER |
| 5 | + |
| 6 | +using System.Text.Json; |
| 7 | +using System.Text.Json.Serialization; |
| 8 | +using OneOf; |
| 9 | + |
| 10 | +#nullable enable |
| 11 | + |
| 12 | +namespace Docfx.Build.ApiPage; |
| 13 | + |
| 14 | +struct LinkSpan |
| 15 | +{ |
| 16 | + public required string text { get; init; } |
| 17 | + public string? url { get; init; } |
| 18 | +} |
| 19 | + |
| 20 | +[GenerateOneOf] |
| 21 | +partial class Span : OneOfBase<string, LinkSpan> { } |
| 22 | + |
| 23 | +[GenerateOneOf] |
| 24 | +partial class Inline : OneOfBase<Span, Span[]> { } |
| 25 | + |
| 26 | +struct Markdown |
| 27 | +{ |
| 28 | + public required string markdown { get; init; } |
| 29 | +} |
| 30 | + |
| 31 | +struct H1 |
| 32 | +{ |
| 33 | + public required string h1 { get; init; } |
| 34 | + public string? id { get; init; } |
| 35 | +} |
| 36 | + |
| 37 | +struct H2 |
| 38 | +{ |
| 39 | + public required string h2 { get; init; } |
| 40 | + public string? id { get; init; } |
| 41 | +} |
| 42 | + |
| 43 | +struct H3 |
| 44 | +{ |
| 45 | + public required string h3 { get; init; } |
| 46 | + public string? id { get; init; } |
| 47 | +} |
| 48 | + |
| 49 | +struct H4 |
| 50 | +{ |
| 51 | + public required string h4 { get; init; } |
| 52 | + public string? id { get; init; } |
| 53 | +} |
| 54 | + |
| 55 | +struct H5 |
| 56 | +{ |
| 57 | + public required string h5 { get; init; } |
| 58 | + public string? id { get; init; } |
| 59 | +} |
| 60 | + |
| 61 | +struct H6 |
| 62 | +{ |
| 63 | + public required string h6 { get; init; } |
| 64 | + public string? id { get; init; } |
| 65 | +} |
| 66 | + |
| 67 | +[GenerateOneOf] |
| 68 | +partial class Heading : OneOfBase<H1, H2, H3, H4, H5, H6> { } |
| 69 | + |
| 70 | +abstract class ApiBase |
| 71 | +{ |
| 72 | + public string? id { get; init; } |
| 73 | + public OneOf<bool, string>? deprecated { get; init; } |
| 74 | + public string? src { get; init; } |
| 75 | + public Dictionary<string, string>? metadata { get; init; } |
| 76 | +} |
| 77 | + |
| 78 | +class Api1 : ApiBase |
| 79 | +{ |
| 80 | + public required string api1 { get; init; } |
| 81 | +} |
| 82 | + |
| 83 | +class Api2 : ApiBase |
| 84 | +{ |
| 85 | + public required string api2 { get; init; } |
| 86 | +} |
| 87 | + |
| 88 | +class Api3 : ApiBase |
| 89 | +{ |
| 90 | + public required string api3 { get; init; } |
| 91 | +} |
| 92 | + |
| 93 | +class Api4 : ApiBase |
| 94 | +{ |
| 95 | + public required string api4 { get; init; } |
| 96 | +} |
| 97 | + |
| 98 | + |
| 99 | +[GenerateOneOf] |
| 100 | +partial class Api : OneOfBase<Api1, Api2, Api3, Api4> { } |
| 101 | + |
| 102 | +record struct Fact(string name, Inline value); |
| 103 | + |
| 104 | +struct Facts |
| 105 | +{ |
| 106 | + public required Fact[] facts { get; init; } |
| 107 | +} |
| 108 | + |
| 109 | +struct List |
| 110 | +{ |
| 111 | + public required Inline[] list { get; init; } |
| 112 | +} |
| 113 | + |
| 114 | +struct Inheritance |
| 115 | +{ |
| 116 | + public required Inline[] inheritance { get; init; } |
| 117 | +} |
| 118 | + |
| 119 | +struct Code |
| 120 | +{ |
| 121 | + public required string code { get; init; } |
| 122 | + public string? languageId { get; init; } |
| 123 | +} |
| 124 | + |
| 125 | +class Parameter |
| 126 | +{ |
| 127 | + public string? name { get; init; } |
| 128 | + public Inline? type { get; init; } |
| 129 | + public string? @default { get; init; } |
| 130 | + public string? description { get; init; } |
| 131 | + public OneOf<bool, string>? deprecated { get; init; } |
| 132 | + public bool? optional { get; init; } |
| 133 | +} |
| 134 | + |
| 135 | +struct Parameters |
| 136 | +{ |
| 137 | + public required Parameter[] parameters { get; init; } |
| 138 | +} |
| 139 | + |
| 140 | +[GenerateOneOf] |
| 141 | +partial class Block : OneOfBase<Heading, Api, Markdown, Facts, Parameters, List, Inheritance, Code> { } |
| 142 | + |
| 143 | +record ApiPage |
| 144 | +{ |
| 145 | + public static JsonSerializerOptions JsonSerializerOptions { get; } = new() |
| 146 | + { |
| 147 | + DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull | JsonIgnoreCondition.WhenWritingDefault, |
| 148 | + }; |
| 149 | + |
| 150 | + static ApiPage() |
| 151 | + { |
| 152 | + JsonSerializerOptions.Converters.Add(new OneOfJsonConverterFactory()); |
| 153 | + } |
| 154 | + |
| 155 | + public required string title { get; init; } |
| 156 | + public required Block[] body { get; init; } |
| 157 | + |
| 158 | + public string? languageId { get; init; } |
| 159 | + public Dictionary<string, OneOf<string, string[]>>? metadata { get; init; } |
| 160 | +} |
| 161 | + |
| 162 | +#endif |
0 commit comments