Skip to content

Commit c44f32d

Browse files
JapuDCretTenischev
andauthored
feat: add support for additionalProperties (#272) (#273)
* feat: Add support for additionalProperties (#272) * fix: Map imports (#272) * fix: Allow additionalProperties=false (#272) * test: Extend test case for boolean (#272) * test: Extend test case for oneOf (#272) * rework: Remove else-branch to check value (#272) * update from master, fix order of imports and tests * fix case when `additionalProperties = true` --------- Co-authored-by: Semen <tenischev.semen@gmail.com>
1 parent 550aed2 commit c44f32d

10 files changed

Lines changed: 632 additions & 9 deletions

File tree

filters/all.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,17 @@ const filter = module.exports;
22
const _ = require('lodash');
33

44
function defineType(prop, propName) {
5-
if (prop.type() === 'object') {
5+
if (prop.additionalProperties()) {
6+
if (prop.additionalProperties() === true) {
7+
return 'Map<String, Object>';
8+
} else if (prop.additionalProperties().type() === 'object') {
9+
return 'Map<String, ' + _.upperFirst(_.camelCase(prop.additionalProperties().uid())) + '>';
10+
} else if (prop.additionalProperties().format()) {
11+
return 'Map<String, ' + toClass(toJavaType(prop.additionalProperties().format())) + '>';
12+
} else if (prop.additionalProperties().type()) {
13+
return 'Map<String, ' + toClass(toJavaType(prop.additionalProperties().type())) + '>';
14+
}
15+
} else if (prop.type() === 'object') {
616
return _.upperFirst(_.camelCase(prop.uid()));
717
} else if (prop.type() === 'array') {
818
if (prop.items().type() === 'object') {

template/src/main/java/com/asyncapi/model/$$message$$.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
{% else %}
77
import jakarta.validation.Valid;
88
{%- endif %}
9-
import java.util.Objects;
109
import java.util.List;
10+
import java.util.Map;
11+
import java.util.Objects;
1112

1213
{% if message.description() or message.examples()%}/**{% for line in message.description() | splitByLines %}
1314
* {{ line | safe}}{% endfor %}{% if message.examples() %}

template/src/main/java/com/asyncapi/model/$$objectSchema$$.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import javax.annotation.processing.Generated;
1616
import java.util.List;
17+
import java.util.Map;
1718
import java.util.Objects;
1819

1920
{% if schema.description() or schema.examples() %}/**{% for line in schema.description() | splitByLines %}
@@ -24,7 +25,17 @@
2425
public class {{schemaName | camelCase | upperFirst}} {
2526
{% for propName, prop in schema.properties() %}
2627
{%- set isRequired = propName | isRequired(schema.required()) %}
27-
{%- if prop.type() === 'object' %}
28+
{%- if prop.additionalProperties() %}
29+
{%- if prop.additionalProperties() === true %}
30+
private @Valid Map<String, Object> {{propName | camelCase}};
31+
{%- elif prop.additionalProperties().type() === 'object' %}
32+
private @Valid Map<String, {{prop.additionalProperties().uid() | camelCase | upperFirst}}> {{propName | camelCase}};
33+
{%- elif prop.additionalProperties().format() %}
34+
private @Valid Map<String, {{prop.additionalProperties().format() | toJavaType | toClass}}> {{propName | camelCase}};
35+
{%- elif prop.additionalProperties().type() %}
36+
private @Valid Map<String, {{prop.additionalProperties().type() | toJavaType | toClass}}> {{propName | camelCase}};
37+
{%- endif %}
38+
{%- elif prop.type() === 'object' %}
2839
private @Valid {{prop.uid() | camelCase | upperFirst}} {{propName | camelCase}};
2940
{%- elif prop.type() === 'array' %}
3041
{%- if prop.items().type() === 'object' %}

tests/__snapshots__/additional-formats.test.js.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import com.fasterxml.jackson.annotation.JsonValue;
1313
1414
import javax.annotation.processing.Generated;
1515
import java.util.List;
16+
import java.util.Map;
1617
import java.util.Objects;
1718
1819

tests/__snapshots__/kafka.test.js.snap

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ import com.fasterxml.jackson.annotation.JsonValue;
180180
181181
import javax.annotation.processing.Generated;
182182
import java.util.List;
183+
import java.util.Map;
183184
import java.util.Objects;
184185
185186
@@ -264,8 +265,9 @@ exports[`template integration tests for generated files using the generator and
264265
import javax.annotation.processing.Generated;
265266
266267
import jakarta.validation.Valid;
267-
import java.util.Objects;
268268
import java.util.List;
269+
import java.util.Map;
270+
import java.util.Objects;
269271
270272
271273
@Generated(value="com.asyncapi.generator.template.spring", date="AnyDate")

0 commit comments

Comments
 (0)