Skip to content

Commit b44ace7

Browse files
committed
Add support for ref array item type
1 parent 2561333 commit b44ace7

File tree

4 files changed

+260
-3
lines changed

4 files changed

+260
-3
lines changed

cdt-java-protocol-builder/src/main/java/com/github/kklisura/cdt/definition/builder/support/protocol/builder/TypesBuilder.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,20 @@ protected String addRefImportStatement(
500500
importAwareBuilder.addImport(UTILS_PACKAGE, LIST_CLASS_NAME);
501501

502502
ArrayType arrayType = (ArrayType) type;
503-
return buildArrayJavaType(getArrayItemJavaType(arrayType.getItems()));
503+
String arrayItemType = null;
504+
505+
if (isRefArrayItemType(arrayType.getItems())) {
506+
com.github.kklisura.cdt.protocol.definition.types.type.array.items.RefArrayItem refArrayItem = (com.github.kklisura.cdt.protocol.definition.types.type.array.items.RefArrayItem) arrayType.getItems();
507+
508+
arrayItemType = addRefImportStatement(importAwareBuilder, refArrayItem.getRef(),
509+
objectType,
510+
domain,
511+
domainTypeResolver);
512+
} else {
513+
arrayItemType = getArrayItemJavaType(arrayType.getItems());
514+
}
515+
516+
return buildArrayJavaType(arrayItemType);
504517
}
505518
if (!isComplexType(type)) {
506519
return getTypeJavaType(type);
@@ -556,6 +569,11 @@ private static String getArrayItemJavaType(ArrayItem arrayItem) {
556569
return ARRAY_ITEM_TYPE_TO_JAVA_TYPE_MAP.get(arrayItem.getClass());
557570
}
558571

572+
protected static boolean isRefArrayItemType(
573+
com.github.kklisura.cdt.protocol.definition.types.type.array.ArrayItem arrayItem) {
574+
return arrayItem instanceof com.github.kklisura.cdt.protocol.definition.types.type.array.items.RefArrayItem;
575+
}
576+
559577
protected static String getArrayItemJavaType(
560578
com.github.kklisura.cdt.protocol.definition.types.type.array.ArrayItem arrayItem) {
561579
return ARRAY_TYPE_ITEM_TYPE_TO_JAVA_TYPE_MAP.get(arrayItem.getClass());

cdt-java-protocol-builder/src/test/java/com/github/kklisura/cdt/definition/builder/support/protocol/builder/CommandBuilderTest.java

Lines changed: 218 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@
2020
* #L%
2121
*/
2222

23-
import static org.easymock.EasyMock.*;
23+
import static org.easymock.EasyMock.anyString;
24+
import static org.easymock.EasyMock.capture;
25+
import static org.easymock.EasyMock.eq;
26+
import static org.easymock.EasyMock.expect;
27+
import static org.easymock.EasyMock.expectLastCall;
2428
import static org.junit.Assert.assertEquals;
2529
import static org.junit.Assert.assertTrue;
2630

@@ -35,6 +39,9 @@
3539
import com.github.kklisura.cdt.protocol.definition.types.Command;
3640
import com.github.kklisura.cdt.protocol.definition.types.Domain;
3741
import com.github.kklisura.cdt.protocol.definition.types.Event;
42+
import com.github.kklisura.cdt.protocol.definition.types.type.ArrayType;
43+
import com.github.kklisura.cdt.protocol.definition.types.type.StringType;
44+
import com.github.kklisura.cdt.protocol.definition.types.type.array.items.StringArrayItem;
3845
import com.github.kklisura.cdt.protocol.definition.types.type.object.ObjectType;
3946
import com.github.kklisura.cdt.protocol.definition.types.type.object.Property;
4047
import com.github.kklisura.cdt.protocol.definition.types.type.object.properties.ArrayProperty;
@@ -43,6 +50,7 @@
4350
import com.github.kklisura.cdt.protocol.definition.types.type.object.properties.RefProperty;
4451
import com.github.kklisura.cdt.protocol.definition.types.type.object.properties.StringProperty;
4552
import com.github.kklisura.cdt.protocol.definition.types.type.object.properties.array.items.EnumArrayItem;
53+
import com.github.kklisura.cdt.protocol.definition.types.type.object.properties.array.items.RefArrayItem;
4654
import java.util.Arrays;
4755
import java.util.Collections;
4856
import java.util.List;
@@ -367,6 +375,215 @@ public void testBuildCommandWithMethodWithComplexParams()
367375
assertEquals(arrayProperty.getName(), allParams.get(1).getAnnotations().get(2).getValue());
368376
}
369377

378+
@Test
379+
public void testBuildCommandWithMethodWithArrayStringParam() {
380+
final Domain domain = new Domain();
381+
domain.setDomain("domainName");
382+
domain.setDescription("Description");
383+
384+
final Command command = new Command();
385+
command.setName("command");
386+
command.setDescription("command description");
387+
388+
final ArrayProperty arrayProperty = new ArrayProperty();
389+
arrayProperty.setName("enumParam1");
390+
arrayProperty.setOptional(Boolean.FALSE);
391+
arrayProperty.setDescription("enum param 1 description");
392+
393+
RefArrayItem enumArrayItem = new RefArrayItem();
394+
enumArrayItem.setRef("SomeRefType");
395+
arrayProperty.setItems(enumArrayItem);
396+
command.setParameters(Collections.singletonList(arrayProperty));
397+
398+
domain.setCommands(Collections.singletonList(command));
399+
400+
expect(javaBuilderFactory.createInterfaceBuilder(BASE_PACKAGE_NAME, "DomainName"))
401+
.andReturn(interfaceBuilder);
402+
interfaceBuilder.setJavaDoc("Description");
403+
404+
Capture<List<MethodParam>> methodParamCapture = Capture.newInstance();
405+
interfaceBuilder.addMethod(
406+
eq("command"),
407+
anyString(),
408+
capture(methodParamCapture),
409+
eq(null));
410+
411+
final ArrayType resolvedRefType = new ArrayType();
412+
resolvedRefType.setId("arrayRefType");
413+
414+
final StringArrayItem stringArrayItem = new StringArrayItem();
415+
resolvedRefType.setItems(stringArrayItem);
416+
417+
expect(resolver.resolve("domainName", "SomeRefType")).andReturn(resolvedRefType);
418+
419+
interfaceBuilder.addImport("java.util", "List");
420+
expectLastCall().times(2);
421+
422+
replayAll();
423+
424+
Builder build = commandBuilder.build(domain, resolver);
425+
426+
verifyAll();
427+
428+
assertTrue(build instanceof JavaInterfaceBuilder);
429+
assertEquals(interfaceBuilder, build);
430+
431+
List<MethodParam> params = methodParamCapture.getValue();
432+
assertEquals(1, params.size());
433+
434+
Assert.assertEquals(arrayProperty.getName(), params.get(0).getName());
435+
assertEquals("List<List<String>>", params.get(0).getType());
436+
assertEquals("ParamName", params.get(0).getAnnotations().get(0).getName());
437+
assertEquals(arrayProperty.getName(), params.get(0).getAnnotations().get(0).getValue());
438+
}
439+
440+
@Test
441+
public void testBuildCommandWithMethodWithArrayRefParam() {
442+
final Domain domain = new Domain();
443+
domain.setDomain("domainName");
444+
domain.setDescription("Description");
445+
446+
final Command command = new Command();
447+
command.setName("command");
448+
command.setDescription("command description");
449+
450+
final ArrayProperty arrayProperty = new ArrayProperty();
451+
arrayProperty.setName("enumParam1");
452+
arrayProperty.setOptional(Boolean.FALSE);
453+
arrayProperty.setDescription("enum param 1 description");
454+
455+
RefArrayItem enumArrayItem = new RefArrayItem();
456+
enumArrayItem.setRef("SomeRefType");
457+
arrayProperty.setItems(enumArrayItem);
458+
command.setParameters(Collections.singletonList(arrayProperty));
459+
460+
domain.setCommands(Collections.singletonList(command));
461+
462+
expect(javaBuilderFactory.createInterfaceBuilder(BASE_PACKAGE_NAME, "DomainName"))
463+
.andReturn(interfaceBuilder);
464+
interfaceBuilder.setJavaDoc("Description");
465+
466+
Capture<List<MethodParam>> methodParamCapture = Capture.newInstance();
467+
interfaceBuilder.addMethod(
468+
eq("command"),
469+
anyString(),
470+
capture(methodParamCapture),
471+
eq(null));
472+
473+
final ArrayType resolvedRefType = new ArrayType();
474+
resolvedRefType.setId("arrayRefType");
475+
476+
final com.github.kklisura.cdt.protocol.definition.types.type.array.items.RefArrayItem refArrayItem = new com.github.kklisura.cdt.protocol.definition.types.type.array.items.RefArrayItem();
477+
refArrayItem.setRef("TestRefArrayItem");
478+
resolvedRefType.setItems(refArrayItem);
479+
480+
final StringType stringType = new StringType();
481+
stringType.setId("stringType");
482+
483+
expect(resolver.resolve("domainName", "SomeRefType")).andReturn(resolvedRefType);
484+
expect(resolver.resolve("domainName", "TestRefArrayItem")).andReturn(stringType);
485+
486+
interfaceBuilder.addImport("java.util", "List");
487+
expectLastCall().times(2);
488+
489+
replayAll();
490+
491+
Builder build = commandBuilder.build(domain, resolver);
492+
493+
verifyAll();
494+
495+
assertTrue(build instanceof JavaInterfaceBuilder);
496+
assertEquals(interfaceBuilder, build);
497+
498+
List<MethodParam> params = methodParamCapture.getValue();
499+
assertEquals(1, params.size());
500+
501+
Assert.assertEquals(arrayProperty.getName(), params.get(0).getName());
502+
assertEquals("List<List<String>>", params.get(0).getType());
503+
assertEquals("ParamName", params.get(0).getAnnotations().get(0).getName());
504+
assertEquals(arrayProperty.getName(), params.get(0).getAnnotations().get(0).getValue());
505+
}
506+
507+
@Test
508+
public void testBuildCommandWithMethodWithDeepArrayRefParam() {
509+
final Domain domain = new Domain();
510+
domain.setDomain("domainName");
511+
domain.setDescription("Description");
512+
513+
final Command command = new Command();
514+
command.setName("command");
515+
command.setDescription("command description");
516+
517+
final ArrayProperty arrayProperty = new ArrayProperty();
518+
arrayProperty.setName("enumParam1");
519+
arrayProperty.setOptional(Boolean.FALSE);
520+
arrayProperty.setDescription("enum param 1 description");
521+
522+
RefArrayItem enumArrayItem = new RefArrayItem();
523+
enumArrayItem.setRef("SomeRefType");
524+
arrayProperty.setItems(enumArrayItem);
525+
command.setParameters(Collections.singletonList(arrayProperty));
526+
527+
domain.setCommands(Collections.singletonList(command));
528+
529+
expect(javaBuilderFactory.createInterfaceBuilder(BASE_PACKAGE_NAME, "DomainName"))
530+
.andReturn(interfaceBuilder);
531+
interfaceBuilder.setJavaDoc("Description");
532+
533+
Capture<List<MethodParam>> methodParamCapture = Capture.newInstance();
534+
interfaceBuilder.addMethod(
535+
eq("command"),
536+
anyString(),
537+
capture(methodParamCapture),
538+
eq(null));
539+
540+
final ArrayType resolvedRefType = new ArrayType();
541+
resolvedRefType.setId("arrayRefType");
542+
543+
final com.github.kklisura.cdt.protocol.definition.types.type.array.items.RefArrayItem refArrayItem = new com.github.kklisura.cdt.protocol.definition.types.type.array.items.RefArrayItem();
544+
refArrayItem.setRef("TestRefArrayItem");
545+
resolvedRefType.setItems(refArrayItem);
546+
547+
final ArrayType resolvedRefType2 = new ArrayType();
548+
resolvedRefType2.setId("arrayRefType2");
549+
550+
final com.github.kklisura.cdt.protocol.definition.types.type.array.items.RefArrayItem refArrayItem2 = new com.github.kklisura.cdt.protocol.definition.types.type.array.items.RefArrayItem();
551+
refArrayItem2.setRef("SomeOtherDomain.TestRefArrayItem2");
552+
resolvedRefType2.setItems(refArrayItem2);
553+
554+
final ObjectType objectType = new ObjectType();
555+
objectType.setId("objectType");
556+
final StringProperty stringProperty = new StringProperty();
557+
stringProperty.setName("stringProperty");
558+
objectType.setProperties(Collections.singletonList(stringProperty));
559+
560+
expect(resolver.resolve("domainName", "SomeRefType")).andReturn(resolvedRefType);
561+
expect(resolver.resolve("domainName", "TestRefArrayItem")).andReturn(resolvedRefType2);
562+
expect(resolver.resolve("SomeOtherDomain", "TestRefArrayItem2")).andReturn(objectType);
563+
564+
interfaceBuilder.addImport("com.github.kklisura.types.someotherdomain", "TestRefArrayItem2");
565+
566+
interfaceBuilder.addImport("java.util", "List");
567+
expectLastCall().times(3);
568+
569+
replayAll();
570+
571+
Builder build = commandBuilder.build(domain, resolver);
572+
573+
verifyAll();
574+
575+
assertTrue(build instanceof JavaInterfaceBuilder);
576+
assertEquals(interfaceBuilder, build);
577+
578+
List<MethodParam> params = methodParamCapture.getValue();
579+
assertEquals(1, params.size());
580+
581+
Assert.assertEquals(arrayProperty.getName(), params.get(0).getName());
582+
assertEquals("List<List<List<TestRefArrayItem2>>>", params.get(0).getType());
583+
assertEquals("ParamName", params.get(0).getAnnotations().get(0).getName());
584+
assertEquals(arrayProperty.getName(), params.get(0).getAnnotations().get(0).getValue());
585+
}
586+
370587
@Test
371588
public void testBuildCommandWithMethodWithComplexReturnParams() {
372589
final Domain domain = new Domain();

cdt-protocol-parser/src/main/java/com/github/kklisura/cdt/protocol/definition/types/type/array/ArrayItem.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import com.github.kklisura.cdt.protocol.definition.deserializers.impl.types.TypeArrayItemSubTypeJsonDeserializer;
2626
import com.github.kklisura.cdt.protocol.definition.types.type.array.items.IntegerArrayItem;
2727
import com.github.kklisura.cdt.protocol.definition.types.type.array.items.NumberArrayItem;
28+
import com.github.kklisura.cdt.protocol.definition.types.type.array.items.RefArrayItem;
2829
import com.github.kklisura.cdt.protocol.definition.types.type.array.items.StringArrayItem;
2930
import lombok.Getter;
3031

@@ -37,7 +38,8 @@
3738
@JsonSubTypes({
3839
@JsonSubTypes.Type(value = StringArrayItem.class, name = "string"),
3940
@JsonSubTypes.Type(value = IntegerArrayItem.class, name = "integer"),
40-
@JsonSubTypes.Type(value = NumberArrayItem.class, name = "number")
41+
@JsonSubTypes.Type(value = NumberArrayItem.class, name = "number"),
42+
@JsonSubTypes.Type(value = RefArrayItem.class, name = "ref"),
4143
})
4244
@JsonDeserialize(using = TypeArrayItemSubTypeJsonDeserializer.class)
4345
public abstract class ArrayItem {
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.github.kklisura.cdt.protocol.definition.types.type.array.items;
2+
3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
import com.fasterxml.jackson.databind.JsonDeserializer;
5+
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
6+
import lombok.Getter;
7+
import lombok.Setter;
8+
9+
/**
10+
* Ref array item type.
11+
*
12+
* @author Kenan Klisura
13+
*/
14+
@Getter
15+
@Setter
16+
@JsonDeserialize(using = JsonDeserializer.None.class)
17+
public class RefArrayItem extends TypedArrayItem {
18+
@JsonProperty("$ref")
19+
private String ref;
20+
}

0 commit comments

Comments
 (0)