Skip to content

Commit

Permalink
feat: add support for additionalProperties (#272) (#273)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
JapuDCret and Tenischev committed Oct 11, 2023
1 parent 550aed2 commit c44f32d
Show file tree
Hide file tree
Showing 10 changed files with 632 additions and 9 deletions.
12 changes: 11 additions & 1 deletion filters/all.js
Expand Up @@ -2,7 +2,17 @@ const filter = module.exports;
const _ = require('lodash');

function defineType(prop, propName) {
if (prop.type() === 'object') {
if (prop.additionalProperties()) {
if (prop.additionalProperties() === true) {
return 'Map<String, Object>';
} else if (prop.additionalProperties().type() === 'object') {
return 'Map<String, ' + _.upperFirst(_.camelCase(prop.additionalProperties().uid())) + '>';
} else if (prop.additionalProperties().format()) {
return 'Map<String, ' + toClass(toJavaType(prop.additionalProperties().format())) + '>';
} else if (prop.additionalProperties().type()) {
return 'Map<String, ' + toClass(toJavaType(prop.additionalProperties().type())) + '>';
}
} else if (prop.type() === 'object') {
return _.upperFirst(_.camelCase(prop.uid()));
} else if (prop.type() === 'array') {
if (prop.items().type() === 'object') {
Expand Down
3 changes: 2 additions & 1 deletion template/src/main/java/com/asyncapi/model/$$message$$.java
Expand Up @@ -6,8 +6,9 @@
{% else %}
import jakarta.validation.Valid;
{%- endif %}
import java.util.Objects;
import java.util.List;
import java.util.Map;
import java.util.Objects;

{% if message.description() or message.examples()%}/**{% for line in message.description() | splitByLines %}
* {{ line | safe}}{% endfor %}{% if message.examples() %}
Expand Down
13 changes: 12 additions & 1 deletion template/src/main/java/com/asyncapi/model/$$objectSchema$$.java
Expand Up @@ -14,6 +14,7 @@

import javax.annotation.processing.Generated;
import java.util.List;
import java.util.Map;
import java.util.Objects;

{% if schema.description() or schema.examples() %}/**{% for line in schema.description() | splitByLines %}
Expand All @@ -24,7 +25,17 @@
public class {{schemaName | camelCase | upperFirst}} {
{% for propName, prop in schema.properties() %}
{%- set isRequired = propName | isRequired(schema.required()) %}
{%- if prop.type() === 'object' %}
{%- if prop.additionalProperties() %}
{%- if prop.additionalProperties() === true %}
private @Valid Map<String, Object> {{propName | camelCase}};
{%- elif prop.additionalProperties().type() === 'object' %}
private @Valid Map<String, {{prop.additionalProperties().uid() | camelCase | upperFirst}}> {{propName | camelCase}};
{%- elif prop.additionalProperties().format() %}
private @Valid Map<String, {{prop.additionalProperties().format() | toJavaType | toClass}}> {{propName | camelCase}};
{%- elif prop.additionalProperties().type() %}
private @Valid Map<String, {{prop.additionalProperties().type() | toJavaType | toClass}}> {{propName | camelCase}};
{%- endif %}
{%- elif prop.type() === 'object' %}
private @Valid {{prop.uid() | camelCase | upperFirst}} {{propName | camelCase}};
{%- elif prop.type() === 'array' %}
{%- if prop.items().type() === 'object' %}
Expand Down
1 change: 1 addition & 0 deletions tests/__snapshots__/additional-formats.test.js.snap
Expand Up @@ -13,6 +13,7 @@ import com.fasterxml.jackson.annotation.JsonValue;
import javax.annotation.processing.Generated;
import java.util.List;
import java.util.Map;
import java.util.Objects;
Expand Down
4 changes: 3 additions & 1 deletion tests/__snapshots__/kafka.test.js.snap
Expand Up @@ -180,6 +180,7 @@ import com.fasterxml.jackson.annotation.JsonValue;
import javax.annotation.processing.Generated;
import java.util.List;
import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -264,8 +265,9 @@ exports[`template integration tests for generated files using the generator and
import javax.annotation.processing.Generated;
import jakarta.validation.Valid;
import java.util.Objects;
import java.util.List;
import java.util.Map;
import java.util.Objects;
@Generated(value="com.asyncapi.generator.template.spring", date="AnyDate")
Expand Down

0 comments on commit c44f32d

Please sign in to comment.