@@ -24,6 +24,7 @@ public ExampleGenerator(Map<String, Model> examples) {
24
24
25
25
public List <Map <String , String >> generate (Map <String , String > examples , List <String > mediaTypes , Property property ) {
26
26
List <Map <String , String >> output = new ArrayList <Map <String , String >>();
27
+ Set <String > processedModels = new HashSet <String >();
27
28
if (examples == null ) {
28
29
if (mediaTypes == null ) {
29
30
// assume application/json for this
@@ -33,7 +34,7 @@ public List<Map<String, String>> generate(Map<String, String> examples, List<Str
33
34
Map <String , String > kv = new HashMap <String , String >();
34
35
kv .put ("contentType" , mediaType );
35
36
if (property != null && mediaType .startsWith ("application/json" )) {
36
- String example = Json .pretty (resolvePropertyToExample (mediaType , property ));
37
+ String example = Json .pretty (resolvePropertyToExample (mediaType , property , processedModels ));
37
38
38
39
if (example != null ) {
39
40
example = example .replaceAll ("\n " , "\\ \\ n" );
@@ -69,7 +70,7 @@ else if(property != null && mediaType.startsWith("application/xml")) {
69
70
return output ;
70
71
}
71
72
72
- protected Object resolvePropertyToExample (String mediaType , Property property ) {
73
+ protected Object resolvePropertyToExample (String mediaType , Property property , Set < String > processedModels ) {
73
74
if (property .getExample () != null ) {
74
75
return property .getExample ();
75
76
}
@@ -83,7 +84,7 @@ else if(property instanceof ArrayProperty) {
83
84
Property innerType = ((ArrayProperty )property ).getItems ();
84
85
if (innerType != null ) {
85
86
Object [] output = new Object []{
86
- resolvePropertyToExample (mediaType , innerType )
87
+ resolvePropertyToExample (mediaType , innerType , processedModels )
87
88
};
88
89
return output ;
89
90
}
@@ -116,21 +117,22 @@ else if(property instanceof MapProperty) {
116
117
Map <String , Object > mp = new HashMap <String , Object >();
117
118
if (property .getName () != null ) {
118
119
mp .put (property .getName (),
119
- resolvePropertyToExample (mediaType , ((MapProperty )property ).getAdditionalProperties ()));
120
+ resolvePropertyToExample (mediaType , ((MapProperty )property ).getAdditionalProperties (), processedModels ));
120
121
}
121
122
else {
122
123
mp .put ("key" ,
123
- resolvePropertyToExample (mediaType , ((MapProperty )property ).getAdditionalProperties ()));
124
+ resolvePropertyToExample (mediaType , ((MapProperty )property ).getAdditionalProperties (), processedModels ));
124
125
}
125
126
return mp ;
126
127
}
127
128
else if (property instanceof ObjectProperty ) {
128
129
return "{}" ;
129
130
}
130
131
else if (property instanceof RefProperty ) {
131
- Model model = examples .get (((RefProperty )property ).getSimpleRef ());
132
+ String simpleName = ((RefProperty )property ).getSimpleRef ();
133
+ Model model = examples .get (simpleName );
132
134
if (model != null )
133
- return resolveModelToExample (mediaType , model );
135
+ return resolveModelToExample (simpleName , mediaType , model , processedModels );
134
136
}
135
137
else if (property instanceof UUIDProperty ) {
136
138
return "046b6c7f-0b8a-43b9-b35d-6489e6daee91" ;
@@ -139,14 +141,20 @@ else if(property instanceof UUIDProperty) {
139
141
return "" ;
140
142
}
141
143
142
- public Object resolveModelToExample (String mediaType , Model model ) {
144
+ public Object resolveModelToExample (String name , String mediaType , Model model , Set <String > processedModels ) {
145
+ if (processedModels .contains (name )) {
146
+ return "" ;
147
+ }
143
148
if (model instanceof ModelImpl ) {
149
+ processedModels .add (name );
144
150
ModelImpl impl = (ModelImpl ) model ;
145
151
Map <String , Object > values = new HashMap <String , Object >();
146
152
147
- for (String name : impl .getProperties ().keySet ()) {
148
- Property property = impl .getProperties ().get (name );
149
- values .put (name , resolvePropertyToExample (mediaType , property ));
153
+ if (impl != null && impl .getProperties () != null ) {
154
+ for (String propertyName : impl .getProperties ().keySet ()) {
155
+ Property property = impl .getProperties ().get (propertyName );
156
+ values .put (propertyName , resolvePropertyToExample (mediaType , property , processedModels ));
157
+ }
150
158
}
151
159
152
160
return values ;
0 commit comments