Skip to content
This repository was archived by the owner on Feb 12, 2022. It is now read-only.

Commit

Permalink
raml-for-jax-rs-295 - Discriminator field is 'null' in subtype (#295)
Browse files Browse the repository at this point in the history
  • Loading branch information
jpbelang committed Aug 22, 2017
1 parent 7c3f2ff commit 3f74535
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ types:
(ramltojaxrs.types):
abstract: true
discriminator: humanType
# discriminatorValue: human
properties:
humanType: string
limbs: Limb
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ private TypeSpec.Builder buildTypeImplementation(TypeContext context, V10GType o

ClassName className = originalType.javaImplementationName(context.getModelPackage());


TypeSpec.Builder typeSpec = TypeSpec
.classBuilder(className)
.addModifiers(Modifier.PUBLIC);
Expand Down Expand Up @@ -140,7 +139,8 @@ private void buildPropertiesForImplementation(final TypeContext context, final V
.addModifiers(Modifier.PRIVATE);

if (propertyInfo.getName().equals(objectType.discriminator())) {
fieldSpec.initializer("$S", objectType.discriminatorValue().or(calculateValue(context, objectType, typeSpec, propertyInfo)));
fieldSpec.initializer("$S",
objectType.discriminatorValue().or(calculateValue(context, objectType, typeSpec, propertyInfo)));
}

fieldSpec =
Expand Down Expand Up @@ -198,14 +198,17 @@ private void buildPropertiesForImplementation(final TypeContext context, final V
}
}

private Supplier<String> calculateValue(final TypeContext context, final V10GType objectType, final TypeSpec.Builder typeSpec, final PropertyInfo propertyInfo) {
private Supplier<String> calculateValue(final TypeContext context, final V10GType objectType, final TypeSpec.Builder typeSpec,
final PropertyInfo propertyInfo) {
return new Supplier<String>() {

@Override
public String get() {
if (propertyInfo.resolve(context).toString().equals("java.lang.String")) {
return typeSpec.build().name;
return originalType.defaultJavaTypeName(context.getModelPackage()).toString();
} else {
throw new GenerationException("while generating type " + objectType.name() + " discriminatorValue not defined and not of type string ( can't map java type name to anything but string )" );
throw new GenerationException("while generating type " + objectType.name()
+ " discriminatorValue not defined and not of type string ( can't map java type name to anything but string )");
}
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,37 +208,37 @@ public void withParentTypeImplementationAndDiscriminatorNoValue() throws Excepti
allOf(
FieldSpecMatchers.fieldName(equalTo("mimi")),
FieldSpecMatchers.fieldType(equalTo(ClassName.get(String.class))),
FieldSpecMatchers.initializer(equalTo("\"Foo\""))
FieldSpecMatchers.initializer(equalTo("\"pack.Daddy\""))
)
)
)));
}

@Test(expected = GenerationException.class)
public void withParentTypeImplementationAndDiscriminatorNoValueBadType() throws Exception {
@Test(expected = GenerationException.class)
public void withParentTypeImplementationAndDiscriminatorNoValueBadType() throws Exception {

setupMocking(BuildPhase.IMPLEMENTATION);
setupMocking(BuildPhase.IMPLEMENTATION);

when(type.parentTypes()).thenReturn(Collections.singletonList(parentType));
when(type.parentTypes()).thenReturn(Collections.singletonList(parentType));

when(type.properties()).thenReturn(Collections.singletonList(property));
when(property.name()).thenReturn("Mimi");
when(property.type()).thenReturn(parameterType);
when(parameterType.defaultJavaTypeName("pack")).thenReturn(ClassName.get(Integer.class));
when(currentBuild.getMethodExtension(Annotations.ON_TYPE_METHOD_CREATION, type)).thenReturn(methodExtension);
when(currentBuild.getFieldExtension(Annotations.ON_TYPE_FIELD_CREATION, type)).thenReturn(fieldExtension);
mockMethodCreation(BuildPhase.IMPLEMENTATION);
when(type.properties()).thenReturn(Collections.singletonList(property));
when(property.name()).thenReturn("Mimi");
when(property.type()).thenReturn(parameterType);
when(parameterType.defaultJavaTypeName("pack")).thenReturn(ClassName.get(Integer.class));
when(currentBuild.getMethodExtension(Annotations.ON_TYPE_METHOD_CREATION, type)).thenReturn(methodExtension);
when(currentBuild.getFieldExtension(Annotations.ON_TYPE_FIELD_CREATION, type)).thenReturn(fieldExtension);
mockMethodCreation(BuildPhase.IMPLEMENTATION);

when(type.discriminator()).thenReturn("Mimi");
when(type.discriminatorValue()).thenReturn(Optional.<String>absent());
when(type.discriminator()).thenReturn("Mimi");
when(type.discriminatorValue()).thenReturn(Optional.<String>absent());

when(parentType.defaultJavaTypeName("pack")).thenReturn(ClassName.bestGuess("pack.Daddy"));
when(parentType.defaultJavaTypeName("pack")).thenReturn(ClassName.bestGuess("pack.Daddy"));

SimpleInheritanceExtension ext = new SimpleInheritanceExtension(type, registry, currentBuild);
TypeSpec.Builder builder = ext.onType(typeContext, null, type, BuildPhase.IMPLEMENTATION);
}
SimpleInheritanceExtension ext = new SimpleInheritanceExtension(type, registry, currentBuild);
TypeSpec.Builder builder = ext.onType(typeContext, null, type, BuildPhase.IMPLEMENTATION);
}

@Test
@Test
public void withProperty() throws Exception {

setupMocking(BuildPhase.INTERFACE);
Expand Down

0 comments on commit 3f74535

Please sign in to comment.