Skip to content

Commit cf7d462

Browse files
RLPNC-5319: support unfielded addresses
1 parent a99ff5a commit cf7d462

File tree

11 files changed

+368
-7
lines changed

11 files changed

+368
-7
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2020 Basis Technology Corp.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.basistech.rosette.apimodel.jackson;
18+
19+
import com.basistech.rosette.apimodel.FieldedAddress;
20+
import com.basistech.rosette.apimodel.IAddress;
21+
import com.basistech.rosette.apimodel.UnfieldedAddress;
22+
23+
import com.fasterxml.jackson.core.JsonParser;
24+
import com.fasterxml.jackson.core.TreeNode;
25+
import com.fasterxml.jackson.databind.DeserializationContext;
26+
import com.fasterxml.jackson.databind.JsonNode;
27+
import com.fasterxml.jackson.databind.ObjectMapper;
28+
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
29+
import com.fasterxml.jackson.databind.node.ObjectNode;
30+
import com.fasterxml.jackson.databind.node.TextNode;
31+
32+
import java.io.IOException;
33+
import java.util.Iterator;
34+
import java.util.Map;
35+
36+
public class AddressDeserializer extends StdDeserializer<IAddress> {
37+
38+
private static final String UNFIELDED_KEY = "address";
39+
40+
AddressDeserializer() {
41+
super(IAddress.class);
42+
}
43+
44+
@Override
45+
public IAddress deserialize(JsonParser jp, DeserializationContext ctxt)
46+
throws IOException {
47+
ObjectMapper mapper = (ObjectMapper) jp.getCodec();
48+
TreeNode root = mapper.readTree(jp);
49+
Class<? extends IAddress> addressClass = FieldedAddress.class;
50+
if (root instanceof TextNode) {
51+
// We only have a JsonProperty-based constructor, so we can't
52+
// have jackson create the object for us.
53+
return new UnfieldedAddress(((TextNode) root).textValue());
54+
} else {
55+
Iterator<Map.Entry<String, JsonNode>> iterator = ((ObjectNode)root).fields();
56+
if (iterator.hasNext()) {
57+
Map.Entry<String, JsonNode> element = iterator.next();
58+
if (UNFIELDED_KEY.equals(element.getKey())) {
59+
addressClass = UnfieldedAddress.class;
60+
}
61+
}
62+
}
63+
return mapper.treeToValue(root, addressClass);
64+
}
65+
}

json/src/main/java/com/basistech/rosette/apimodel/jackson/AddressSimilarityRequestMixin.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import com.fasterxml.jackson.annotation.JsonTypeName;
2323
import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
2424

25-
import com.basistech.rosette.apimodel.Address;
25+
import com.basistech.rosette.apimodel.IAddress;
2626

2727
@JsonTypeName("AddressSimilarityRequest")
2828
@JsonInclude(JsonInclude.Include.NON_NULL)
@@ -31,8 +31,8 @@ public class AddressSimilarityRequestMixin {
3131
@JsonCreator
3232
protected AddressSimilarityRequestMixin(
3333
@JsonProperty("profileId") String profileId,
34-
@JsonProperty("address1") Address address1,
35-
@JsonProperty("address2") Address address2
34+
@JsonProperty("address1") IAddress address1,
35+
@JsonProperty("address2") IAddress address2
3636
) {
3737
//
3838
}

json/src/main/java/com/basistech/rosette/apimodel/jackson/ApiModelMixinModule.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,18 @@
2121
import com.basistech.rosette.apimodel.AdmRequest;
2222
import com.basistech.rosette.apimodel.ConfigurationRequest;
2323
import com.basistech.rosette.apimodel.DocumentRequest;
24+
import com.basistech.rosette.apimodel.FieldedAddress;
25+
import com.basistech.rosette.apimodel.IAddress;
2426
import com.basistech.rosette.apimodel.Name;
2527
import com.basistech.rosette.apimodel.NameDeduplicationRequest;
2628
import com.basistech.rosette.apimodel.NameSimilarityRequest;
2729
import com.basistech.rosette.apimodel.NameTranslationRequest;
30+
import com.basistech.rosette.apimodel.UnfieldedAddress;
2831
import com.basistech.rosette.dm.jackson.AnnotatedDataModelModule;
2932
import com.fasterxml.jackson.databind.MapperFeature;
3033
import com.fasterxml.jackson.databind.Module;
3134
import com.fasterxml.jackson.databind.ObjectMapper;
35+
import com.fasterxml.jackson.databind.module.SimpleDeserializers;
3236

3337
/**
3438
* Jackson module to configure Json serialization and deserialization for the
@@ -62,11 +66,20 @@ public void setupModule(Module.SetupContext context) {
6266
context.setMixInAnnotations(NameDeduplicationRequest.NameDeduplicationRequestBuilder.class, NameDeduplicationRequestMixin.NameDeduplicationRequestBuilderMixin.class);
6367
context.setMixInAnnotations(Address.class, AddressMixin.class);
6468
context.setMixInAnnotations(Address.AddressBuilder.class, AddressMixin.AddressBuilderMixin.class);
69+
context.setMixInAnnotations(FieldedAddress.class, FieldedAddressMixin.class);
70+
context.setMixInAnnotations(FieldedAddress.FieldedAddressBuilder.class, FieldedAddressMixin.FieldedAddressBuilderMixin.class);
71+
context.setMixInAnnotations(UnfieldedAddress.class, UnfieldedAddressMixin.class);
72+
context.setMixInAnnotations(UnfieldedAddress.UnfieldedAddressBuilder.class, UnfieldedAddressMixin.UnfieldedAddressBuilderMixin.class);
6573
context.setMixInAnnotations(AddressSimilarityRequest.class, AddressSimilarityRequestMixin.class);
6674
context.setMixInAnnotations(AddressSimilarityRequest.AddressSimilarityRequestBuilder.class, AddressSimilarityRequestMixin.AddressSimilarityRequestBuilderMixin.class);
6775
context.setMixInAnnotations(ConfigurationRequest.class, ConfigurationRequestMixin.class);
6876
context.setMixInAnnotations(ConfigurationRequest.ConfigurationRequestBuilder.class,
6977
ConfigurationRequestMixin.ConfigurationRequestBuilderMixin.class);
78+
79+
// IAddresses require a custom deserializer
80+
SimpleDeserializers deserializers = new SimpleDeserializers();
81+
deserializers.addDeserializer(IAddress.class, new AddressDeserializer());
82+
context.addDeserializers(deserializers);
7083
}
7184

7285
/**
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright 2020 Basis Technology Corp.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.basistech.rosette.apimodel.jackson;
18+
19+
import com.fasterxml.jackson.annotation.JsonCreator;
20+
import com.fasterxml.jackson.annotation.JsonInclude;
21+
import com.fasterxml.jackson.annotation.JsonProperty;
22+
import com.fasterxml.jackson.annotation.JsonTypeName;
23+
import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
24+
25+
@JsonTypeName("FieldedAddress")
26+
@JsonInclude(JsonInclude.Include.NON_NULL)
27+
public abstract class FieldedAddressMixin {
28+
29+
@JsonCreator
30+
protected FieldedAddressMixin(@JsonProperty("house") String house,
31+
@JsonProperty("houseNumber") String houseNumber,
32+
@JsonProperty("road") String road,
33+
@JsonProperty("unit") String unit,
34+
@JsonProperty("level") String level,
35+
@JsonProperty("staircase") String staircase,
36+
@JsonProperty("entrance") String entrance,
37+
@JsonProperty("suburb") String suburb,
38+
@JsonProperty("cityDistrict") String cityDistrict,
39+
@JsonProperty("city") String city,
40+
@JsonProperty("island") String island,
41+
@JsonProperty("stateDistrict") String stateDistrict,
42+
@JsonProperty("state") String state,
43+
@JsonProperty("countryRegion") String countryRegion,
44+
@JsonProperty("country") String country,
45+
@JsonProperty("worldRegion") String worldRegion,
46+
@JsonProperty("postCode") String postCode,
47+
@JsonProperty("poBox") String poBox) {
48+
//
49+
}
50+
51+
@JsonPOJOBuilder(withPrefix = "")
52+
abstract class FieldedAddressBuilderMixin {
53+
}
54+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2020 Basis Technology Corp.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.basistech.rosette.apimodel.jackson;
18+
19+
import com.fasterxml.jackson.annotation.JsonCreator;
20+
import com.fasterxml.jackson.annotation.JsonInclude;
21+
import com.fasterxml.jackson.annotation.JsonProperty;
22+
import com.fasterxml.jackson.annotation.JsonTypeName;
23+
import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
24+
25+
@JsonTypeName("UnfieldedAddress")
26+
@JsonInclude(JsonInclude.Include.NON_NULL)
27+
public abstract class UnfieldedAddressMixin {
28+
29+
30+
@JsonCreator
31+
protected UnfieldedAddressMixin(@JsonProperty("address") String address) {
32+
//
33+
}
34+
35+
@JsonPOJOBuilder(withPrefix = "")
36+
abstract class UnfieldedAddressBuilderMixin {
37+
}
38+
}

json/src/test/java/com/basistech/rosette/apimodel/ModelTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,10 @@ private Object createObjectForType(Class<?> type, Type genericParameterType) thr
293293
}
294294
break;
295295
}
296+
case "IAddress": {
297+
o = new UnfieldedAddress("foo");
298+
break;
299+
}
296300
default:
297301
if (parameterArgClass != null) {
298302
Constructor[] ctors = parameterArgClass.getDeclaredConstructors();

model/src/main/java/com/basistech/rosette/apimodel/Address.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
*/
2525
@Value
2626
@Builder
27-
public class Address {
27+
@Deprecated
28+
public class Address implements IAddress {
2829

2930
/**
3031
* @return the address house
@@ -115,4 +116,10 @@ public class Address {
115116
* @return the address P.O. Box
116117
*/
117118
private final String poBox;
119+
120+
@Override
121+
public boolean fielded() {
122+
return true;
123+
}
124+
118125
}

model/src/main/java/com/basistech/rosette/apimodel/AddressSimilarityRequest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,16 @@ public final class AddressSimilarityRequest extends Request {
3333
* @return first address
3434
*/
3535
@NotNull
36-
private Address address1;
36+
private IAddress address1;
3737

3838
/**
3939
* @return second address
4040
*/
4141
@NotNull
42-
private Address address2;
42+
private IAddress address2;
4343

4444
@Builder
45-
public AddressSimilarityRequest(String profileId, Address address1, Address address2) {
45+
public AddressSimilarityRequest(String profileId, IAddress address1, IAddress address2) {
4646
super(profileId);
4747
this.address1 = address1;
4848
this.address2 = address2;
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/*
2+
* Copyright 2020 Basis Technology Corp.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.basistech.rosette.apimodel;
18+
19+
import lombok.Builder;
20+
import lombok.Value;
21+
22+
@Value
23+
@Builder
24+
public class FieldedAddress implements IAddress {
25+
/**
26+
* @return the address house
27+
*/
28+
private final String house;
29+
30+
/**
31+
* @return the address house number
32+
*/
33+
private final String houseNumber;
34+
35+
/**
36+
* @return the address road
37+
*/
38+
private final String road;
39+
40+
/**
41+
* @return the address unit
42+
*/
43+
private final String unit;
44+
45+
/**
46+
* @return the address level
47+
*/
48+
private final String level;
49+
50+
/**
51+
* @return the address staircase
52+
*/
53+
private final String staircase;
54+
55+
/**
56+
* @return the address entrance
57+
*/
58+
private final String entrance;
59+
60+
/**
61+
* @return the address suburb
62+
*/
63+
private final String suburb;
64+
65+
/**
66+
* @return the address city district
67+
*/
68+
private final String cityDistrict;
69+
70+
/**
71+
* @return the address city
72+
*/
73+
private final String city;
74+
75+
/**
76+
* @return the address island
77+
*/
78+
private final String island;
79+
80+
/**
81+
* @return the address state district
82+
*/
83+
private final String stateDistrict;
84+
85+
/**
86+
* @return the address state
87+
*/
88+
private final String state;
89+
90+
/**
91+
* @return the address country region
92+
*/
93+
private final String countryRegion;
94+
95+
/**
96+
* @return the address country
97+
*/
98+
private final String country;
99+
100+
/**
101+
* @return the address world region
102+
*/
103+
private final String worldRegion;
104+
105+
/**
106+
* @return the address postal code
107+
*/
108+
private final String postCode;
109+
110+
/**
111+
* @return the address P.O. Box
112+
*/
113+
private final String poBox;
114+
115+
@Override
116+
public boolean fielded() {
117+
return true;
118+
}
119+
}

0 commit comments

Comments
 (0)