1313 limitations under the License. */
1414package com .predic8 .membrane .annot .generator ;
1515
16- import com .fasterxml .jackson .databind .node .* ;
17- import com .predic8 .membrane .annot .* ;
18- import com .predic8 .membrane .annot .generator .kubernetes .* ;
16+ import com .fasterxml .jackson .databind .node .JsonNodeFactory ;
17+ import com .predic8 .membrane .annot .ProcessingException ;
18+ import com .predic8 .membrane .annot .generator .kubernetes .AbstractGrammar ;
1919import com .predic8 .membrane .annot .generator .kubernetes .model .*;
2020import com .predic8 .membrane .annot .model .*;
21- import com .predic8 .membrane .annot .model .doc .*;
22- import org .jetbrains .annotations .*;
23-
24- import javax .annotation .processing .*;
25- import javax .lang .model .element .*;
26- import javax .tools .*;
27- import java .io .*;
28- import java .util .*;
29- import java .util .stream .*;
21+ import com .predic8 .membrane .annot .model .doc .Doc ;
22+ import org .jetbrains .annotations .NotNull ;
23+
24+ import javax .annotation .processing .ProcessingEnvironment ;
25+ import javax .lang .model .element .Element ;
26+ import javax .lang .model .element .TypeElement ;
27+ import javax .tools .FileObject ;
28+ import java .io .BufferedWriter ;
29+ import java .io .IOException ;
30+ import java .util .ArrayList ;
31+ import java .util .HashMap ;
32+ import java .util .List ;
33+ import java .util .Map ;
3034
3135import static com .predic8 .membrane .annot .generator .kubernetes .model .SchemaFactory .*;
32- import static com .predic8 .membrane .annot .generator .util .SchemaGeneratorUtil .* ;
33- import static javax .tools .StandardLocation .* ;
36+ import static com .predic8 .membrane .annot .generator .util .SchemaGeneratorUtil .escapeJsonContent ;
37+ import static javax .tools .StandardLocation .CLASS_OUTPUT ;
3438
3539/**
3640 * TODOs:
@@ -76,13 +80,20 @@ private void assemble(Model m, MainInfo main) throws IOException {
7680
7781 private void addTopLevelProperties (Model m , MainInfo main ) {
7882 schema .additionalProperties (false );
79- List <AbstractSchema <?>> kinds = main .getElements ().values ().stream ()
83+
84+ var top = main .getElements ().values ().stream ()
8085 .filter (e -> e .getAnnotation ().topLevel ())
81- .map (e -> createTopLevelProperty (e , m ))
82- .collect (Collectors .toUnmodifiableList ());
86+ .toList ();
87+
88+ for (ElementInfo e : top ) {
89+ String name = e .getAnnotation ().name ();
90+ String refName = "#/$defs/" + e .getXSDTypeName (m );
91+ schema .property (ref (name ).ref (refName ));
92+ }
8393
84- if (!kinds .isEmpty ())
85- schema .oneOf (kinds );
94+ if (!top .isEmpty ()) {
95+ schema .minProperties (1 ).maxProperties (1 );
96+ }
8697 }
8798
8899 private AbstractSchema <?> createTopLevelProperty (ElementInfo e , Model m ) {
@@ -93,6 +104,7 @@ private AbstractSchema<?> createTopLevelProperty(ElementInfo e, Model m) {
93104 schema .property (ref (name ).ref (refName ));
94105
95106 return object ()
107+ .title (name )
96108 .additionalProperties (false )
97109 .property (ref (name )
98110 .ref (refName )
@@ -251,17 +263,18 @@ private void processMCChilds(Model m, MainInfo main, ElementInfo i, AbstractSche
251263 continue ;
252264
253265 sos .add (object ()
266+ .title (ei .getAnnotation ().name ())
254267 .additionalProperties (false )
255268 .property (ref (ei .getAnnotation ().name ())
256- .ref ("#/$defs/" + ei .getXSDTypeName (m ))
257- .required (true )));
269+ .ref ("#/$defs/" + ei .getXSDTypeName (m ))));
258270 }
259271 // Allow referencing a component instance directly on list-item level:
260272 // flow:
261273 // - $ref: ...
262274 sos .add (object ()
275+ .title ("componentRef" )
263276 .additionalProperties (false )
264- .property ( string ("$ref" ). required ( true ) ));
277+ .property ( string ("$ref" )));
265278 return sos ;
266279 }
267280
@@ -377,17 +390,17 @@ private SchemaObject createComponentsMapParser(Model m, MainInfo main, ElementIn
377390 var variants = new ArrayList <SchemaObject >();
378391
379392 for (ElementInfo comp : main .getElements ().values ()) {
380- if (!comp .getAnnotation ().component ())
381- continue ;
393+ if (!comp .getAnnotation ().component ()) continue ;
394+ if ( comp . getAnnotation (). topLevel ()) continue ;
382395
383- if (comp .getAnnotation ().topLevel ())
384- continue ;
396+ String n = comp .getAnnotation ().name ();
385397
386398 variants .add (object ()
399+ .title (n )
387400 .additionalProperties (false )
388- .property ( ref ( comp . getAnnotation (). name () )
389- . ref ("#/$defs/" + comp . getXSDTypeName ( m ) )
390- .required ( true )));
401+ .minProperties ( 1 )
402+ . property ( ref (n )
403+ .ref ( "#/$defs/" + comp . getXSDTypeName ( m ) )));
391404 }
392405 return variants ;
393406 }
0 commit comments