Skip to content

Commit

Permalink
fix: avro schema union types generate object type (#250)
Browse files Browse the repository at this point in the history
* initial implementation; needs testing and clean up

* added toString() call on objects in toString() method; created snapshot

* fix linting issues
  • Loading branch information
CameronRushton authored Mar 1, 2022
1 parent 5d9afdd commit a5d6d23
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 0 deletions.
3 changes: 3 additions & 0 deletions filters/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,9 @@ function fixType([name, javaName, property]) {
if (property.enum()) {
debugType('It is an enum.');
typeName = _.upperFirst(javaName);
} else if (property.oneOf() || property.anyOf() || property.allOf()) {
// 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.
typeName = 'Object';
} else {
// check to see if it's a ref to another schema.
typeName = property.ext('x-parser-schema-id');
Expand Down
5 changes: 5 additions & 0 deletions partials/java-class
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,12 @@
{%- set propModelClass = {schema: prop, schemaName: name} | getModelClass %}
{%- set realClassName = propModelClass.getClassName() %}
{%- set variableName = realClassName | identifierName %}
{%- set typeInfo = [realClassName, realClassName, prop] | fixType -%}
{%- set type = typeInfo[0] %}
{{ indent3 }}+ " {{ variableName }}: " + {{ variableName }}
{%- if type === 'Object' -%}
.toString()
{%- endif -%}
{%- endfor %}
{%- if modelClass.isSubClass() %}
{{ indent3 }}+ " super: " + super.toString(){% endif %}
Expand Down
66 changes: 66 additions & 0 deletions test/__snapshots__/integration.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1801,3 +1801,69 @@ public class JobAcknowledge {
}
"
`;
exports[`template integration tests using the generator should return object when avro union type is used specifying many possible types 1`] = `
"package com.example.api.jobOrder;
import com.fasterxml.jackson.annotation.JsonInclude;
@JsonInclude(JsonInclude.Include.NON_NULL)
public class JobOrder {
public JobOrder () {
}
public JobOrder (
String jobOrderId,
Object jobOrderDescription,
Object jobOrderLongDescription) {
this.jobOrderId = jobOrderId;
this.jobOrderDescription = jobOrderDescription;
this.jobOrderLongDescription = jobOrderLongDescription;
}
private String jobOrderId;
private Object jobOrderDescription;
private Object jobOrderLongDescription;
public String getJobOrderId() {
return jobOrderId;
}
public JobOrder setJobOrderId(String jobOrderId) {
this.jobOrderId = jobOrderId;
return this;
}
public Object getJobOrderDescription() {
return jobOrderDescription;
}
public JobOrder setJobOrderDescription(Object jobOrderDescription) {
this.jobOrderDescription = jobOrderDescription;
return this;
}
public Object getJobOrderLongDescription() {
return jobOrderLongDescription;
}
public JobOrder setJobOrderLongDescription(Object jobOrderLongDescription) {
this.jobOrderLongDescription = jobOrderLongDescription;
return this;
}
public String toString() {
return \\"JobOrder [\\"
+ \\" jobOrderId: \\" + jobOrderId
+ \\" jobOrderDescription: \\" + jobOrderDescription.toString()
+ \\" jobOrderLongDescription: \\" + jobOrderLongDescription.toString()
+ \\" ]\\";
}
}
"
`;
9 changes: 9 additions & 0 deletions test/integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,13 @@ describe('template integration tests using the generator', () => {
];
await assertExpectedFiles(validatedFiles);
});

it('should return object when avro union type is used specifying many possible types', async () => {
await generate('mocks/avro-union-object.yaml');

const validatedFiles = [
'src/main/java/com/example/api/jobOrder/JobOrder.java'
];
await assertExpectedFiles(validatedFiles);
});
});
42 changes: 42 additions & 0 deletions test/mocks/avro-union-object.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
components:
schemas: {}
messages:
JobOrder:
payload:
name: "JobOrder"
namespace: "com.example.api.jobOrder"
doc: "JobOrder"
type: "record"
fields:
- name: "jobOrderId"
doc: "JobOrderID"
type: "string"
- name: "jobOrderDescription"
doc: "JobOrderDescription"
type:
- "null"
- "string"
- name: "jobOrderLongDescription"
doc: "JobOrderLongDescription"
type:
- "null"
- "string"
schemaFormat: "application/vnd.apache.avro+json;version=1.9.0"
contentType: "application/vnd.apache.avro+json"
servers:
production:
protocol: "kafka"
url: "xxxxx.us-east-2.aws.confluent.cloud:9092"
channels:
test.jobs.order:
subscribe:
message:
$ref: "#/components/messages/JobOrder"
asyncapi: "2.0.0"
info:
x-generated-time: "2022-02-24 01:18 UTC"
description: ""
title: "Union Types"
x-view: "provider"
version: "1"

0 comments on commit a5d6d23

Please sign in to comment.