13
13
import java .util .LinkedHashSet ;
14
14
import java .util .List ;
15
15
import java .util .Map ;
16
+ import java .util .Optional ;
16
17
import java .util .Map .Entry ;
17
18
import java .util .Set ;
18
19
import java .util .stream .Collectors ;
48
49
*/
49
50
public class CrdResourceGen {
50
51
52
+ private static final String ATTR_PROPS = "properties" ;
53
+
54
+
55
+
51
56
private static class FieldNameType {
52
57
String name ;
53
58
String type ;
@@ -110,19 +115,29 @@ public void create() {
110
115
}
111
116
112
117
private void mapProperties (JsonNode jsonNodeTree , JSONSchemaPropsBuilder specBuilder , JSONSchemaPropsBuilder statusBuilder ) {
113
- mapper .getCreateSchema ().ifPresent (crtSch -> mapper .getPatchSchema ().ifPresent (uptSch -> {
114
- JsonNode crtSchema = jsonNodeTree .at (removeLeadingHash (crtSch .getRef ()));
115
- JsonNode uptSchema = jsonNodeTree .at (removeLeadingHash (uptSch .getRef ()));
116
- Set <Entry <String , JsonNode >> unionOfFields = unionOfFields (crtSchema .get ("properties" ), uptSchema .get ("properties" ));
118
+ Set <Entry <String , JsonNode >> allSpecFields = mapper .getCreateSchema ().flatMap (crtSch -> mapper .getPatchSchema ().or (mapper ::getPutSchema ).map (uptSch -> {
119
+ JsonNode crtSchemaProps = Optional .ofNullable (crtSch .getRef ())
120
+ .map (s -> jsonNodeTree .at (removeLeadingHash (s )))
121
+ .map (n -> n .get (ATTR_PROPS )).orElse (null );
122
+ JsonNode uptSchemaProps = Optional .ofNullable (uptSch .getRef ())
123
+ .map (s -> jsonNodeTree .at (removeLeadingHash (s )))
124
+ .map (n -> n .get (ATTR_PROPS )).orElse (null );
125
+ Set <Entry <String , JsonNode >> unionOfFields = unionOfFields (crtSchemaProps , uptSchemaProps );
117
126
addMissingFieldsFromPathParamMappings (unionOfFields , "spec" );
118
127
mapProperties (specBuilder , unionOfFields , jsonNodeTree );
119
- Set <Entry <String , JsonNode >> fieldsOfNotIn = fieldsOfNotIn (jsonNodeTree .at (removeLeadingHash (mapper .getByIdSchema ().getRef ())).get ("properties" ), unionOfFields .stream ().map (Entry ::getKey ).toList ());
120
- addMissingFieldsFromPathParamMappings (fieldsOfNotIn , "status" );
121
- mapProperties (statusBuilder , fieldsOfNotIn , jsonNodeTree );
122
-
123
- }));
128
+ return unionOfFields ;
129
+ })).orElse (Collections .emptySet ());
130
+
131
+ JsonNode getSchemaProps = Optional .ofNullable (mapper .getByIdSchema ().getRef ())
132
+ .map (s -> jsonNodeTree .at (removeLeadingHash (s )))
133
+ .map (n -> n .get (ATTR_PROPS )).orElse (null );
134
+ Set <Entry <String , JsonNode >> fieldsOfNotIn = fieldsOfNotIn (getSchemaProps , allSpecFields .stream ().map (Entry ::getKey ).toList ());
135
+ addMissingFieldsFromPathParamMappings (fieldsOfNotIn , "status" );
136
+ mapProperties (statusBuilder , fieldsOfNotIn , jsonNodeTree );
124
137
}
125
138
139
+
140
+
126
141
private void addMissingFieldsFromPathParamMappings (Set <Entry <String , JsonNode >> fields , String prefix ) {
127
142
resolver .getPathParamMappingKeys ().stream ()
128
143
.filter (this ::oneOfCrudPathsMatches )
@@ -156,6 +171,7 @@ private boolean oneOfCrudPathsMatches(String s) {
156
171
return mapper .getByIdPath ().filter (p -> s .equals (p .getKey ())).isPresent ()
157
172
|| mapper .createPath ().filter (p -> s .equals (p .getKey ())).isPresent ()
158
173
|| mapper .patchPath ().filter (p -> s .equals (p .getKey ())).isPresent ()
174
+ || mapper .putPath ().filter (p -> s .equals (p .getKey ())).isPresent ()
159
175
|| mapper .deletePath ().filter (p -> s .equals (p .getKey ())).isPresent ();
160
176
}
161
177
@@ -173,24 +189,29 @@ private void mapProperties(JSONSchemaPropsBuilder builder, Set<Entry<String, Jso
173
189
174
190
private void mapProperties (JSONSchemaPropsBuilder builder , Set <Entry <String , JsonNode >> fields , JsonNode jsonNodeTree , Set <JsonNode > visitedNodes ) {
175
191
mapPrimitiveTypeProps (builder , fields );
176
- mapArrayTypeProps (builder , fields , jsonNodeTree );
177
- mapObjectTypeProps (builder , jsonNodeTree , visitedNodes , fields );
192
+ mapArrayTypeProps (builder , fields , jsonNodeTree , visitedNodes );
193
+ mapObjectTypeProps (builder , fields , jsonNodeTree , visitedNodes );
178
194
}
179
195
180
- private void mapArrayTypeProps (JSONSchemaPropsBuilder builder , Set <Entry <String , JsonNode >> fields , JsonNode jsonNodeTree ) {
196
+ private void mapArrayTypeProps (JSONSchemaPropsBuilder builder , Set <Entry <String , JsonNode >> fields , JsonNode jsonNodeTree , Set < JsonNode > visitedNodes ) {
181
197
fields .stream ()
182
198
.filter (f -> f .getValue ().get ("type" ) != null )
183
199
.filter (f -> "array" .equals (f .getValue ().get ("type" ).asText ()))
184
200
.filter (f -> f .getValue ().get ("items" ) != null )
185
201
.forEach (f -> {
202
+ LOG .info ("Array {} " ,f .getKey ());
186
203
JSONSchemaPropsBuilder jsonSchemaPropsBuilder = new JSONSchemaPropsBuilder ();
187
204
JSONSchemaPropsBuilder itemPropsBuilder = new JSONSchemaPropsBuilder ();
188
205
if (f .getValue ().get ("items" ).get ("type" ) != null ) {
189
206
itemPropsBuilder .withType (f .getValue ().get ("items" ).get ("type" ).asText ());
190
207
}
191
208
if (f .getValue ().get ("items" ).get ("$ref" ) != null ) {
192
209
JsonNode schema = jsonNodeTree .at (removeLeadingHash (f .getValue ().get ("items" ).get ("$ref" ).asText ()));
193
- mapProperties (itemPropsBuilder , fields (schema .get ("properties" )), jsonNodeTree , new HashSet <>());
210
+ LOG .info ("Schema {} " , f .getValue ().get ("items" ).get ("$ref" ));
211
+ if (!visitedNodes .contains (schema )) {
212
+ visitedNodes .add (schema );
213
+ mapProperties (itemPropsBuilder , fields (schema .get (ATTR_PROPS )), jsonNodeTree , visitedNodes );
214
+ }
194
215
itemPropsBuilder .withType ("object" );
195
216
}
196
217
builder .addToProperties (f .getKey (),
@@ -206,16 +227,16 @@ private void mapPrimitiveTypeProps(JSONSchemaPropsBuilder builder, Set<Entry<Str
206
227
new JSONSchemaPropsBuilder ().withType (f .getValue ().get ("type" ).asText ()).build ()));
207
228
}
208
229
209
- private void mapObjectTypeProps (JSONSchemaPropsBuilder builder , JsonNode jsonNodeTree ,
210
- Set <JsonNode > visitedNodes , Set < Entry < String , JsonNode >> fields ) {
230
+ private void mapObjectTypeProps (JSONSchemaPropsBuilder builder , Set < Entry < String , JsonNode >> fields , JsonNode jsonNodeTree ,
231
+ Set <JsonNode > visitedNodes ) {
211
232
fields .stream ()
212
233
.filter (f -> f .getValue ().get ("$ref" ) != null )
213
234
.forEach (f -> {
214
235
JsonNode schema = jsonNodeTree .at (removeLeadingHash (f .getValue ().get ("$ref" ).asText ()));
215
236
if (!visitedNodes .contains (schema )) {
216
237
visitedNodes .add (schema );
217
238
JSONSchemaPropsBuilder objectTypeBuilder = new JSONSchemaPropsBuilder ();
218
- mapProperties (objectTypeBuilder , fields (schema .get ("properties" )), jsonNodeTree , visitedNodes );
239
+ mapProperties (objectTypeBuilder , fields (schema .get (ATTR_PROPS )), jsonNodeTree , visitedNodes );
219
240
builder .addToProperties (f .getKey (),
220
241
objectTypeBuilder .withType ("object" ).build ());
221
242
}
@@ -224,24 +245,32 @@ private void mapObjectTypeProps(JSONSchemaPropsBuilder builder, JsonNode jsonNod
224
245
225
246
private Set <Entry <String , JsonNode >> unionOfFields (JsonNode a , JsonNode b ) {
226
247
Set <Entry <String , JsonNode >> union = new LinkedHashSet <>();
227
- a .fields ().forEachRemaining (union ::add );
228
- b .fields ().forEachRemaining (union ::add );
248
+ if (a != null ) {
249
+ a .fields ().forEachRemaining (union ::add );
250
+ }
251
+ if (b != null ) {
252
+ b .fields ().forEachRemaining (union ::add );
253
+ }
229
254
return union ;
230
255
}
231
256
232
257
private Set <Entry <String , JsonNode >> fields (JsonNode a ) {
233
258
Set <Entry <String , JsonNode >> fields = new LinkedHashSet <>();
234
- a .fields ().forEachRemaining (fields ::add );
259
+ if (a != null ) {
260
+ a .fields ().forEachRemaining (fields ::add );
261
+ }
235
262
return fields ;
236
263
}
237
264
238
265
private Set <Entry <String , JsonNode >> fieldsOfNotIn (JsonNode a , Collection <String > b ) {
239
266
Set <Entry <String , JsonNode >> exclusiveSet = new LinkedHashSet <>();
240
- a .fields ().forEachRemaining (e -> {
241
- if (!b .contains (e .getKey ())) {
242
- exclusiveSet .add (e );
243
- }
244
- });
267
+ if (a != null ) {
268
+ a .fields ().forEachRemaining (e -> {
269
+ if (!b .contains (e .getKey ())) {
270
+ exclusiveSet .add (e );
271
+ }
272
+ });
273
+ }
245
274
return exclusiveSet ;
246
275
}
247
276
0 commit comments