20
20
import static org .hamcrest .Matchers .*;
21
21
22
22
import java .io .IOException ;
23
+ import java .lang .reflect .ParameterizedType ;
24
+ import java .lang .reflect .Type ;
23
25
24
26
import org .jsonschema2pojo .Schema ;
25
27
import org .jsonschema2pojo .integration .util .Jsonschema2PojoRule ;
@@ -40,7 +42,7 @@ public class FragmentRefIT {
40
42
private static Class <?> fragmentRefsClass ;
41
43
42
44
@ BeforeClass
43
- public static void generateAndCompileEnum () throws ClassNotFoundException {
45
+ public static void generateAndCompile () throws ClassNotFoundException {
44
46
45
47
ClassLoader fragmentRefsClassLoader = classSchemaRule .generateAndCompile ("/schema/ref/fragmentRefs.json" , "com.example" );
46
48
@@ -113,4 +115,73 @@ public void refToInnerFragmentThatHasRefToOuterFragmentWithoutParentFile() throw
113
115
new RuleFactory ().getSchemaRule ().apply ("Example" , schema , null , p , new Schema (null , schema , null ));
114
116
}
115
117
118
+ @ Test
119
+ public void refToInnerFragmentThatHasRefToAnotherFragmentWithoutParentFile () throws IOException {
120
+ JCodeModel codeModel = new JCodeModel ();
121
+ JsonNode schema = new ObjectMapper ().readTree ("{\n "
122
+ + " \" $schema\" : \" http://json-schema.org/draft-07/schema#\" ,\n "
123
+ + " \" title\" : \" Inbox Item Datalake DTO\" ,\n "
124
+ + " \" definitions\" : {\n "
125
+ + " \" PropertyA\" : {\n "
126
+ + " \" type\" : \" object\" ,\n "
127
+ + " \" properties\" : {\n "
128
+ + " \" value\" : {\n "
129
+ + " \" type\" : \" string\" \n "
130
+ + " }\n "
131
+ + " }\n "
132
+ + " },\n "
133
+ + " \" PropertyB\" : {\n "
134
+ + " \" type\" : \" object\" ,\n "
135
+ + " \" properties\" : {\n "
136
+ + " \" data\" : {\n "
137
+ + " \" type\" : \" array\" ,\n "
138
+ + " \" items\" : {\n "
139
+ + " \" $ref\" : \" #/definitions/PropertyA\" \n "
140
+ + " },\n "
141
+ + " \" default\" : []\n "
142
+ + " }\n "
143
+ + " }\n "
144
+ + " }\n "
145
+ + " },\n "
146
+ + " \" properties\" : {\n "
147
+ + " \" FinalProperty\" : {\n "
148
+ + " \" type\" : \" array\" ,\n "
149
+ + " \" items\" : {\n "
150
+ + " \" $ref\" : \" #/definitions/PropertyB\" \n "
151
+ + " },\n "
152
+ + " \" default\" : []\n "
153
+ + " }\n "
154
+ + " }\n "
155
+ + "}" );
156
+
157
+ JPackage p = codeModel ._package ("com.example" );
158
+ new RuleFactory ().getSchemaRule ().apply ("Example" , schema , null , p , new Schema (null , schema , null ));
159
+ }
160
+
161
+
162
+ @ Test
163
+ public void refToInnerFragmentThatHasRefToAnotherFragment () throws IOException , ClassNotFoundException , NoSuchMethodException , SecurityException {
164
+ final ClassLoader fragmentRefsClassLoader = classSchemaRule .generateAndCompile ("/schema/ref/refToRefToDefinitions.json" , "com.example" );
165
+
166
+ final Class <?> finalPropertyClass = fragmentRefsClassLoader .loadClass ("com.example.RefToRefToDefinitions" );
167
+
168
+ Class <?> finalPropertyType = finalPropertyClass .getMethod ("getFinalProperty" ).getReturnType ();
169
+ assertThat (finalPropertyType .getName (), is ("java.util.List" ));
170
+
171
+ Type finalPropertyItemType = ((ParameterizedType )finalPropertyClass .getMethod ("getFinalProperty" ).getGenericReturnType ()).getActualTypeArguments ()[0 ];
172
+ assertThat (finalPropertyItemType .getTypeName (), is ("com.example.PropertyB" ));
173
+
174
+ final Class <?> propertyBClass = fragmentRefsClassLoader .loadClass ("com.example.PropertyB" );
175
+
176
+ Class <?> dataType = propertyBClass .getMethod ("getData" ).getReturnType ();
177
+ assertThat (dataType .getName (), is ("java.util.List" ));
178
+
179
+ Type dataItemType = ((ParameterizedType )propertyBClass .getMethod ("getData" ).getGenericReturnType ()).getActualTypeArguments ()[0 ];
180
+ assertThat (dataItemType .getTypeName (), is ("com.example.PropertyA" ));
181
+
182
+ final Class <?> propertyAClass = fragmentRefsClassLoader .loadClass ("com.example.PropertyA" );
183
+
184
+ Class <?> valueType = propertyAClass .getMethod ("getValue" ).getReturnType ();
185
+ assertThat (valueType .getName (), is ("java.lang.String" ));
186
+ }
116
187
}
0 commit comments