-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathcopy-constants.php
More file actions
executable file
·202 lines (177 loc) · 8.01 KB
/
Copy pathcopy-constants.php
File metadata and controls
executable file
·202 lines (177 loc) · 8.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/usr/bin/env php
<?php
require_once __DIR__ . '/../vendor/autoload.php';
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
set_error_handler(function ($level, $msg) {
echo "Error: {$msg}";
exit(1);
});
/**
* Post-generation patch for SubFormFieldRuleAction.cs.
*
* Between openapi-generator v7.8.0 and v7.12.0 a change was made to the
* way a few generators create constant names from values. The original
* way was broken: "change-field-visibility" generated a constant named
* "TYPE_FIELD_VISIBILITY", dropping the "change" part. The fix generates
* the correct name, "TYPE_CHANGE_FIELD_VISIBILITY", but also removes the
* previous (incorrect) names, which is a BC break.
*
* To preserve source compatibility this script adds the old names back
* as aliases (FieldVisibility = ChangeFieldVisibility, etc.). The aliases
* share a numeric value with their canonical counterparts, which breaks
* StringEnumConverter: Enum.GetName is not guaranteed to pick the
* canonical name for ties, and Newtonsoft rejects duplicate
* [EnumMember] values on the same enum. To make the aliases serialize
* to the correct OpenAPI wire values under all circumstances, this
* script also:
* - swaps StringEnumConverter for a dedicated TypeEnumJsonConverter
* that maps by underlying numeric value, and
* - injects that nested converter class into SubFormFieldRuleAction.
*/
class CopyConstants
{
public function run(): void
{
$file = __DIR__ . '/../src/Dropbox.Sign/Model/SubFormFieldRuleAction.cs';
$contents = file_get_contents($file);
$contents = $this->addAliases($contents);
$contents = $this->swapConverter($contents);
$contents = $this->injectConverterClass($contents);
file_put_contents($file, $contents);
$this->stopEmittingDefaultIntValues(__DIR__ . '/../src/Dropbox.Sign/Model');
}
/**
* For optional non-nullable int properties on request-side models
* openapi-generator emits
* [DataMember(Name = "foo", EmitDefaultValue = true)]
* public int Foo { get; set; }
* which forces `"foo": 0` onto the wire whenever the caller never
* set a value (C# default for int is 0). For fields like font_size
* the server rejects the default with a 400 ("'font_size' must be
* between 7 and 49"), and more generally any optional int whose
* server-side default is not 0 is misrepresented.
*
* Scope:
* - Only request-side models are patched. Response models keep
* EmitDefaultValue = true so that round-trip serialization
* preserves explicit zero values that the server sent (tests
* under Dropbox.Sign.Test.Api rely on this fidelity). Response
* models are identified by the "Response" substring in the
* file name, which is the convention for all generated
* response types in this SDK.
* - Required int properties are left alone: openapi-generator
* inserts `IsRequired = true,` between the Name and
* EmitDefaultValue tokens, which this regex does not match.
*/
private function stopEmittingDefaultIntValues(string $modelDir): void
{
$pattern = '/(\[DataMember\(Name = "[^"]+", EmitDefaultValue = )true(\)\]\s*\r?\n\s+public int [A-Za-z_][A-Za-z0-9_]* \{ get; set; \})/';
foreach (glob($modelDir . '/*.cs') as $path) {
if (strpos(basename($path), 'Response') !== false) {
continue;
}
$contents = file_get_contents($path);
$patched = preg_replace($pattern, '$1false$2', $contents);
if ($patched !== null && $patched !== $contents) {
file_put_contents($path, $patched);
}
}
}
private function addAliases(string $contents): string
{
$constant_1 = " ChangeFieldVisibility = 1,";
$replace_1 = implode("\n", [
$constant_1,
' FieldVisibility = ChangeFieldVisibility,',
]);
$constant_2 = " ChangeGroupVisibility = 2";
$replace_2 = implode(",\n", [
$constant_2,
' GroupVisibility = ChangeGroupVisibility',
]);
$contents = str_replace($constant_1, $replace_1, $contents);
$contents = str_replace($constant_2, $replace_2, $contents);
return $contents;
}
private function swapConverter(string $contents): string
{
$needle = " [JsonConverter(typeof(StringEnumConverter))]\n"
. " public enum TypeEnum";
$replacement = <<<'CS'
// A dedicated converter is used instead of StringEnumConverter
// because this enum intentionally carries alias members
// (FieldVisibility, GroupVisibility) that share a numeric value
// with their canonical counterparts. StringEnumConverter goes
// through Enum.GetName, which is not guaranteed to pick the
// canonical name when multiple members share a value, and
// Newtonsoft rejects duplicate [EnumMember] values on the same
// enum. Mapping by numeric value here is unambiguous: both the
// canonical name and its alias produce the same wire string.
[JsonConverter(typeof(TypeEnumJsonConverter))]
public enum TypeEnum
CS;
return str_replace($needle, $replacement, $contents);
}
private function injectConverterClass(string $contents): string
{
// Inject the nested converter immediately after the enum's
// closing brace. Anchor on the enum's tail, which is unique in
// the file.
$needle = " GroupVisibility = ChangeGroupVisibility\n"
. " }\n";
$converter = <<<'CS'
GroupVisibility = ChangeGroupVisibility
}
/// <summary>
/// Serializes SubFormFieldRuleAction.TypeEnum to and from the
/// OpenAPI wire values (change-field-visibility,
/// change-group-visibility). The switch is driven by the
/// underlying numeric value so that legacy alias members
/// (FieldVisibility, GroupVisibility) serialize identically to
/// their canonical counterparts.
/// </summary>
public class TypeEnumJsonConverter : JsonConverter<TypeEnum>
{
public override void WriteJson(JsonWriter writer, TypeEnum value, JsonSerializer serializer)
{
switch (value)
{
case TypeEnum.ChangeFieldVisibility:
writer.WriteValue("change-field-visibility");
return;
case TypeEnum.ChangeGroupVisibility:
writer.WriteValue("change-group-visibility");
return;
default:
throw new JsonSerializationException(
$"Unknown value for SubFormFieldRuleAction.TypeEnum: {(int)value}");
}
}
public override TypeEnum ReadJson(JsonReader reader, Type objectType, TypeEnum existingValue, bool hasExistingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.Null)
{
throw new JsonSerializationException(
"Cannot deserialize null into SubFormFieldRuleAction.TypeEnum");
}
var raw = reader.Value?.ToString();
switch (raw)
{
case "change-field-visibility":
return TypeEnum.ChangeFieldVisibility;
case "change-group-visibility":
return TypeEnum.ChangeGroupVisibility;
default:
throw new JsonSerializationException(
$"Unknown wire value for SubFormFieldRuleAction.TypeEnum: {raw}");
}
}
}
CS;
return str_replace($needle, $converter, $contents);
}
}
$copier = new CopyConstants();
$copier->run();