Skip to content

Commit a5d6d23

Browse files
fix: avro schema union types generate object type (#250)
* initial implementation; needs testing and clean up * added toString() call on objects in toString() method; created snapshot * fix linting issues
1 parent 5d9afdd commit a5d6d23

File tree

5 files changed

+125
-0
lines changed

5 files changed

+125
-0
lines changed

filters/all.js

+3
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,9 @@ function fixType([name, javaName, property]) {
336336
if (property.enum()) {
337337
debugType('It is an enum.');
338338
typeName = _.upperFirst(javaName);
339+
} else if (property.oneOf() || property.anyOf() || property.allOf()) {
340+
// Picking a type for the user may be difficult - especially since the first avro union value must be the default value which is normally of type null.
341+
typeName = 'Object';
339342
} else {
340343
// check to see if it's a ref to another schema.
341344
typeName = property.ext('x-parser-schema-id');

partials/java-class

+5
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,12 @@
7575
{%- set propModelClass = {schema: prop, schemaName: name} | getModelClass %}
7676
{%- set realClassName = propModelClass.getClassName() %}
7777
{%- set variableName = realClassName | identifierName %}
78+
{%- set typeInfo = [realClassName, realClassName, prop] | fixType -%}
79+
{%- set type = typeInfo[0] %}
7880
{{ indent3 }}+ " {{ variableName }}: " + {{ variableName }}
81+
{%- if type === 'Object' -%}
82+
.toString()
83+
{%- endif -%}
7984
{%- endfor %}
8085
{%- if modelClass.isSubClass() %}
8186
{{ indent3 }}+ " super: " + super.toString(){% endif %}

test/__snapshots__/integration.test.js.snap

+66
Original file line numberDiff line numberDiff line change
@@ -1801,3 +1801,69 @@ public class JobAcknowledge {
18011801
}
18021802
"
18031803
`;
1804+
1805+
exports[`template integration tests using the generator should return object when avro union type is used specifying many possible types 1`] = `
1806+
"package com.example.api.jobOrder;
1807+
1808+
1809+
import com.fasterxml.jackson.annotation.JsonInclude;
1810+
1811+
1812+
1813+
@JsonInclude(JsonInclude.Include.NON_NULL)
1814+
public class JobOrder {
1815+
1816+
public JobOrder () {
1817+
}
1818+
1819+
public JobOrder (
1820+
String jobOrderId,
1821+
Object jobOrderDescription,
1822+
Object jobOrderLongDescription) {
1823+
this.jobOrderId = jobOrderId;
1824+
this.jobOrderDescription = jobOrderDescription;
1825+
this.jobOrderLongDescription = jobOrderLongDescription;
1826+
}
1827+
1828+
private String jobOrderId;
1829+
private Object jobOrderDescription;
1830+
private Object jobOrderLongDescription;
1831+
public String getJobOrderId() {
1832+
return jobOrderId;
1833+
}
1834+
1835+
public JobOrder setJobOrderId(String jobOrderId) {
1836+
this.jobOrderId = jobOrderId;
1837+
return this;
1838+
}
1839+
1840+
1841+
public Object getJobOrderDescription() {
1842+
return jobOrderDescription;
1843+
}
1844+
1845+
public JobOrder setJobOrderDescription(Object jobOrderDescription) {
1846+
this.jobOrderDescription = jobOrderDescription;
1847+
return this;
1848+
}
1849+
1850+
1851+
public Object getJobOrderLongDescription() {
1852+
return jobOrderLongDescription;
1853+
}
1854+
1855+
public JobOrder setJobOrderLongDescription(Object jobOrderLongDescription) {
1856+
this.jobOrderLongDescription = jobOrderLongDescription;
1857+
return this;
1858+
}
1859+
1860+
public String toString() {
1861+
return \\"JobOrder [\\"
1862+
+ \\" jobOrderId: \\" + jobOrderId
1863+
+ \\" jobOrderDescription: \\" + jobOrderDescription.toString()
1864+
+ \\" jobOrderLongDescription: \\" + jobOrderLongDescription.toString()
1865+
+ \\" ]\\";
1866+
}
1867+
}
1868+
"
1869+
`;

test/integration.test.js

+9
Original file line numberDiff line numberDiff line change
@@ -183,4 +183,13 @@ describe('template integration tests using the generator', () => {
183183
];
184184
await assertExpectedFiles(validatedFiles);
185185
});
186+
187+
it('should return object when avro union type is used specifying many possible types', async () => {
188+
await generate('mocks/avro-union-object.yaml');
189+
190+
const validatedFiles = [
191+
'src/main/java/com/example/api/jobOrder/JobOrder.java'
192+
];
193+
await assertExpectedFiles(validatedFiles);
194+
});
186195
});

test/mocks/avro-union-object.yaml

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
---
2+
components:
3+
schemas: {}
4+
messages:
5+
JobOrder:
6+
payload:
7+
name: "JobOrder"
8+
namespace: "com.example.api.jobOrder"
9+
doc: "JobOrder"
10+
type: "record"
11+
fields:
12+
- name: "jobOrderId"
13+
doc: "JobOrderID"
14+
type: "string"
15+
- name: "jobOrderDescription"
16+
doc: "JobOrderDescription"
17+
type:
18+
- "null"
19+
- "string"
20+
- name: "jobOrderLongDescription"
21+
doc: "JobOrderLongDescription"
22+
type:
23+
- "null"
24+
- "string"
25+
schemaFormat: "application/vnd.apache.avro+json;version=1.9.0"
26+
contentType: "application/vnd.apache.avro+json"
27+
servers:
28+
production:
29+
protocol: "kafka"
30+
url: "xxxxx.us-east-2.aws.confluent.cloud:9092"
31+
channels:
32+
test.jobs.order:
33+
subscribe:
34+
message:
35+
$ref: "#/components/messages/JobOrder"
36+
asyncapi: "2.0.0"
37+
info:
38+
x-generated-time: "2022-02-24 01:18 UTC"
39+
description: ""
40+
title: "Union Types"
41+
x-view: "provider"
42+
version: "1"

0 commit comments

Comments
 (0)