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

Fix for #39 (Nil union values not handled properly by jackson) #40

Merged
merged 2 commits into from
Dec 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ public TypeSpec.Builder classCreated(UnionPluginContext unionPluginContext, Unio
return unionTypeHandlerPlugin.classCreated(unionPluginContext, ramlType, incoming, eventType);
}

@Override
public FieldSpec.Builder fieldBuilt(UnionPluginContext unionPluginContext, TypeDeclaration ramlType, FieldSpec.Builder fieldSpec, EventType eventType) {
return unionTypeHandlerPlugin.fieldBuilt(unionPluginContext, ramlType, fieldSpec, eventType);
}

@Override
public FieldSpec.Builder anyFieldCreated(UnionPluginContext context, UnionTypeDeclaration union, TypeSpec.Builder typeSpec, FieldSpec.Builder anyType, EventType eventType) {
return unionTypeHandlerPlugin.anyFieldCreated(context, union, typeSpec, anyType, eventType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
import org.raml.ramltopojo.CreationResult;
import org.raml.v2.api.model.v10.datamodel.TypeDeclaration;

import com.squareup.javapoet.TypeName;
/**
* Created. There, you have it.
*/
public interface UnionPluginContext {

CreationResult creationResult();
CreationResult unionClass(TypeDeclaration ramlType);
TypeName findType(String typeName, TypeDeclaration type);
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package org.raml.ramltopojo.extensions;

import org.raml.ramltopojo.CreationResult;
import org.raml.ramltopojo.EventType;
import org.raml.ramltopojo.GenerationContext;
import org.raml.ramltopojo.TypeDeclarationType;
import org.raml.v2.api.model.v10.datamodel.TypeDeclaration;

import com.squareup.javapoet.TypeName;

/**
* Created. There, you have it.
Expand All @@ -23,6 +26,12 @@ public CreationResult creationResult() {
return result;
}

@Override
public TypeName findType(String typeName, TypeDeclaration type) {

return TypeDeclarationType.calculateTypeName(typeName, type, generationContext, EventType.INTERFACE);
}

@Override
public CreationResult unionClass(TypeDeclaration ramlType) {
return generationContext.findCreatedType(ramlType.name(), ramlType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.squareup.javapoet.FieldSpec;
import com.squareup.javapoet.TypeSpec;
import org.raml.ramltopojo.EventType;
import org.raml.v2.api.model.v10.datamodel.TypeDeclaration;
import org.raml.v2.api.model.v10.datamodel.UnionTypeDeclaration;

import java.util.ArrayList;
Expand Down Expand Up @@ -32,11 +33,17 @@ public TypeSpec.Builder classCreated(UnionPluginContext unionPluginContext, Unio
public FieldSpec.Builder anyFieldCreated(UnionPluginContext context, UnionTypeDeclaration union, TypeSpec.Builder typeSpec, FieldSpec.Builder anyType, EventType eventType) {
return anyType;
}

@Override
public FieldSpec.Builder fieldBuilt(UnionPluginContext context, TypeDeclaration ramlType, FieldSpec.Builder fieldSpec, EventType eventType) {
return fieldSpec;
}
}

ClassName className(UnionPluginContext unionPluginContext, UnionTypeDeclaration ramlType, ClassName currentSuggestion, EventType eventType);
TypeSpec.Builder classCreated(UnionPluginContext unionPluginContext, UnionTypeDeclaration ramlType, TypeSpec.Builder incoming, EventType eventType);
FieldSpec.Builder anyFieldCreated(UnionPluginContext context, UnionTypeDeclaration union, TypeSpec.Builder typeSpec, FieldSpec.Builder anyType, EventType eventType);
FieldSpec.Builder fieldBuilt(UnionPluginContext unionPluginContext, TypeDeclaration ramlType, FieldSpec.Builder fieldSpec, EventType eventType);

class Composite implements UnionTypeHandlerPlugin {

Expand Down Expand Up @@ -80,5 +87,16 @@ public FieldSpec.Builder anyFieldCreated(UnionPluginContext context, UnionTypeDe

return anyType;
}

@Override
public FieldSpec.Builder fieldBuilt(UnionPluginContext context, TypeDeclaration ramlType, FieldSpec.Builder fieldSpec, EventType eventType) {
for (UnionTypeHandlerPlugin plugin : plugins) {
if (fieldSpec == null) {
break;
}
fieldSpec = plugin.fieldBuilt(context, ramlType, fieldSpec, eventType);
}
return fieldSpec;
}
}
}

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,9 @@ public FieldSpec.Builder anyFieldCreated(UnionPluginContext context, UnionTypeDe

return anyType;
}

@Override
public FieldSpec.Builder fieldBuilt(UnionPluginContext context, TypeDeclaration property, FieldSpec.Builder fieldSpec, EventType eventType) {
return fieldSpec;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,50 +28,72 @@

import static org.raml.ramltopojo.extensions.jsr303.FacetValidation.addAnnotations;

import java.util.Objects;

import javax.validation.constraints.NotNull;

/**
* Created by Jean-Philippe Belanger on 12/12/16. Just potential zeroes and ones
*/
public class Jsr303Extension extends AllTypesPluginHelper {

@Override
public FieldSpec.Builder fieldBuilt(ObjectPluginContext objectPluginContext, TypeDeclaration typeDeclaration, FieldSpec.Builder fieldSpec, EventType eventType) {
AnnotationAdder adder = new AnnotationAdder() {

@Override
public TypeName typeName() {
return fieldSpec.build().type;
}

@Override
public void addAnnotation(AnnotationSpec spec) {
fieldSpec.addAnnotation(spec);
}
};


addAnnotations(typeDeclaration, adder);
return fieldSpec;
}


@Override
public FieldSpec.Builder anyFieldCreated(UnionPluginContext context, UnionTypeDeclaration union, TypeSpec.Builder typeSpec, FieldSpec.Builder anyType, EventType eventType) {

FacetValidation.addFacetsForBuilt(new AnnotationAdder() {

@Override
public TypeName typeName() {
return anyType.build().type;
}

@Override
public void addAnnotation(AnnotationSpec spec) {

anyType.addAnnotation(spec);
}
});

return anyType;
}

@Override
public FieldSpec.Builder fieldBuilt(ObjectPluginContext objectPluginContext, TypeDeclaration typeDeclaration, FieldSpec.Builder fieldSpec, EventType eventType) {
AnnotationAdder adder = new AnnotationAdder() {

@Override
public TypeName typeName() {
return fieldSpec.build().type;
}

@Override
public void addAnnotation(AnnotationSpec spec) {
fieldSpec.addAnnotation(spec);
}
};

addAnnotations(typeDeclaration, adder);
return fieldSpec;
}

@Override
public FieldSpec.Builder fieldBuilt(UnionPluginContext unionPluginContext, TypeDeclaration ramlType, FieldSpec.Builder fieldSpec, EventType eventType) {
AnnotationAdder adder = new AnnotationAdder() {

@Override
public TypeName typeName() {
return fieldSpec.build().type;
}

@Override
public void addAnnotation(AnnotationSpec spec) {
// ignore not null (we are in a union, of course they can be null)
if (!Objects.equals(spec.type, TypeName.get(NotNull.class))) {
fieldSpec.addAnnotation(spec);
}
}
};

addAnnotations(ramlType, adder);
return fieldSpec;
}

@Override
public FieldSpec.Builder anyFieldCreated( UnionPluginContext context, UnionTypeDeclaration union, TypeSpec.Builder typeSpec, FieldSpec.Builder anyType, EventType eventType) {
FacetValidation.addFacetsForBuilt(new AnnotationAdder() {

@Override
public TypeName typeName() {
return anyType.build().type;
}

@Override
public void addAnnotation(AnnotationSpec spec) {

anyType.addAnnotation(spec);
}
});

return anyType;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.raml.ramltopojo.extensions.ObjectTypeHandlerPlugin;
import org.raml.v2.api.model.v10.datamodel.ObjectTypeDeclaration;
import org.raml.v2.api.model.v10.datamodel.TypeDeclaration;
import org.raml.v2.api.model.v10.datamodel.UnionTypeDeclaration;

import javax.annotation.Nullable;
import javax.lang.model.element.Modifier;
Expand Down Expand Up @@ -95,6 +96,14 @@ private TypeSpec createImplementation(ObjectPluginContext objectPluginContext, C
if ( TypeDeclarationType.isNewInlineType(propertyDeclaration) ){

CreationResult cr = result.internalType(propertyDeclaration.name());
if (cr.getImplementation().isPresent()) {

// we need a special handling for property unions, they need to be added as inline types
if (propertyDeclaration instanceof UnionTypeDeclaration) {
TypeSpec.Builder innerTypeSpecImpl = cr.getImplementation().get().toBuilder();
typeSpec.addType(innerTypeSpecImpl.addModifiers(Modifier.PUBLIC, Modifier.STATIC).build());
}
}
tn = cr.getJavaName(EventType.INTERFACE);

} else {
Expand Down Expand Up @@ -190,12 +199,27 @@ private TypeSpec createInterface(ObjectPluginContext objectPluginContext, Creati
}

TypeName tn = null;
if ( TypeDeclarationType.isNewInlineType(propertyDeclaration) ){
if ( TypeDeclarationType.isNewInlineType(propertyDeclaration) ) {

// we need a special handling for property unions, they need to be added as inline types
if (propertyDeclaration instanceof UnionTypeDeclaration) {

// Inline union naming: string | nil => StringNilUnion
CreationResult cr = TypeDeclarationType.createInlineType(interf, result.getJavaName(EventType.IMPLEMENTATION),
Names.typeName(propertyDeclaration.type(), "union"), propertyDeclaration, generationContext).get();
result.withInternalType(propertyDeclaration.name(), cr);
tn = cr.getJavaName(EventType.INTERFACE);

typeSpec.addType(cr.getInterface().toBuilder().addModifiers(Modifier.PUBLIC, Modifier.STATIC).build());

} else {

Optional<CreationResult> cr = TypeDeclarationType.createInlineType(interf, result.getJavaName(EventType.IMPLEMENTATION), Names.typeName(propertyDeclaration.name(), "type"), propertyDeclaration, generationContext);
if ( cr.isPresent() ) {
result.withInternalType(propertyDeclaration.name(), cr.get());
tn = cr.get().getJavaName(EventType.INTERFACE);
Optional<CreationResult> cr = TypeDeclarationType.createInlineType(interf, result.getJavaName(EventType.IMPLEMENTATION),
Names.typeName(propertyDeclaration.name(), "type"), propertyDeclaration, generationContext);
if (cr.isPresent()) {
result.withInternalType(propertyDeclaration.name(), cr.get());
tn = cr.get().getJavaName(EventType.INTERFACE);
}
}
} else {

Expand Down
Loading