Skip to content

Commit

Permalink
299 async api string with format date or datetime (#305)
Browse files Browse the repository at this point in the history
* 299-adding data and date-time with format

* 299-update versions

* 299 - adding format properties and format annotation at arrays

* 299 - fix intend

* 299 - format. remove leading space

* 299 - reduce parameter

* recheck spaces

* 299 - change documentation

* 299 - simplify functions

* remove unused imports

* 299 - date type configurable, change version

---------

Co-authored-by: nbis Klaus Bertram <dev@n-bis-de>
  • Loading branch information
devNbis and nbis Klaus Bertram committed Dec 11, 2023
1 parent 1005e2c commit 1546f3b
Show file tree
Hide file tree
Showing 13 changed files with 208 additions and 76 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,14 @@ can be configured in the plugin.
created by the user. As the previous parameter, this is also optional.
Check [how is the modelPackage set](#how-is-modelpackage-set) for more
information about how his parameter works, and the values it could have.
- **dateFormat**: This parameter changes the format annotation for `LocalDate` fields.
The syntax follow the [Java SimpleDateFormat](https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html).
The default value are `yyyy-MM-dd`.
- **dateTimeFormat**: This parameter changes the format annotation for `LocalDateTime`
fields. The syntax follow the [Java SimpleDateFormat](https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html).
The default value are `yyyy-MM-dd'T'HH:mm:ss`.
- **useTimeType**: Enum TimeType value. Controls the types used when generating dates. Can be `LOCAL` or `ZOINED`.
The default value is `TimeType.LOCAL`.

The configuration of `consumer`, `supplier` and `streamBridge` are independent.
If only one of them is configured in the pom file, only that one will be
Expand Down
2 changes: 1 addition & 1 deletion multiapi-engine/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>com.sngular</groupId>
<artifactId>multiapi-engine</artifactId>
<version>5.1.1</version>
<version>5.2.1</version>
<packaging>jar</packaging>

<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import com.sngular.api.generator.plugin.common.files.ClasspathFileLocation;
import com.sngular.api.generator.plugin.common.files.DirectoryFileLocation;
import com.sngular.api.generator.plugin.common.files.FileLocation;
import com.sngular.api.generator.plugin.common.model.TimeType;
import com.sngular.api.generator.plugin.common.tools.ApiTool;
import com.sngular.api.generator.plugin.exception.InvalidAPIException;
import freemarker.template.TemplateException;
Expand Down Expand Up @@ -475,7 +476,8 @@ private void fillTemplateFactory(
final JsonNode schemaToBuild = processedMethod.getPayload();
if (shouldBuild(schemaToBuild)) {
final var schemaObjectIt =
MapperContentUtil.mapComponentToSchemaObject(totalSchemas, className, schemaToBuild, null, operationObject.getModelNameSuffix(), parentPackage).iterator();
MapperContentUtil.mapComponentToSchemaObject(totalSchemas, className, schemaToBuild, null, operationObject.getModelNameSuffix(), parentPackage,
operationObject.getFormats(), operationObject.getUseTimeType()).iterator();

if (schemaObjectIt.hasNext()) {
final var filePath = writeSchemaObject(operationObject.isUseLombokModelAnnotation(), operationObject.getModelPackage(), keyClassName, schemaObjectIt.next());
Expand Down Expand Up @@ -526,7 +528,7 @@ private ProcessMethodResult processMethod(
} else if (message.has(PAYLOAD)) {
payloadInfo = processPayload(operationObject, ApiTool.getName(message), ApiTool.getNode(message, PAYLOAD), ymlParent);
if (ApiTool.hasNode(message, BINDINGS)) {
processBindings(processBindingsResultBuilder, operationObject.getClassNamePostfix(), operationObject.getModelNameSuffix(), message);
processBindings(processBindingsResultBuilder, operationObject.getClassNamePostfix(), operationObject.getModelNameSuffix(), message, operationObject.getUseTimeType());
}
} else {
throw new InvalidAsyncAPIException(operationId);
Expand Down Expand Up @@ -559,7 +561,7 @@ private Pair<String, JsonNode> processMethodRef(

final var message = totalSchemas.get(MapperUtil.buildKey(MapperUtil.splitName(messageRef)));
if (ApiTool.hasNode(message, BINDINGS)) {
processBindings(bindingsResult, operationObject.getClassNamePostfix(), operationObject.getModelNameSuffix(), message);
processBindings(bindingsResult, operationObject.getClassNamePostfix(), operationObject.getModelNameSuffix(), message, operationObject.getUseTimeType());
}
return processPayload(operationObject, MapperUtil.getRefClass(method), ApiTool.getNode(message, PAYLOAD), ymlParent);
}
Expand Down Expand Up @@ -613,20 +615,22 @@ private String processExternalRef(final String modelPackage, final FileLocation
}
}

private void processBindings(final ProcessBindingsResultBuilder bindingsResult, final String prefix, final String suffix, final JsonNode message) {
private void processBindings(final ProcessBindingsResultBuilder bindingsResult, final String prefix, final String suffix, final JsonNode message,
final TimeType useTimeType) {
if (message.has(BINDINGS)) {
final var bindingsNode = message.get(BINDINGS);
if (bindingsNode.has(KAFKA)) {
processKafkaBindings(bindingsResult, prefix, suffix, bindingsNode.get(KAFKA));
processKafkaBindings(bindingsResult, prefix, suffix, bindingsNode.get(KAFKA), useTimeType);
} else {
bindingsResult.bindingType(BindingTypeEnum.NONBINDING.getValue());
}
}
}

private void processKafkaBindings(final ProcessBindingsResultBuilder bindingsResult, final String prefix, final String suffix, final JsonNode kafkaBindings) {
private void processKafkaBindings(final ProcessBindingsResultBuilder bindingsResult, final String prefix, final String suffix, final JsonNode kafkaBindings,
final TimeType useTimeType) {
if (kafkaBindings.has(KEY)) {
bindingsResult.bindings(MapperUtil.getSimpleType(ApiTool.getNode(kafkaBindings, "key"), prefix, suffix))
bindingsResult.bindings(MapperUtil.getSimpleType(ApiTool.getNode(kafkaBindings, "key"), prefix, suffix, useTimeType))
.bindingType(BindingTypeEnum.KAFKA.getValue());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public class SchemaFieldObjectProperties {

private String multipleOf = null;

private String format = null;

private Set<String> properties;

public SchemaFieldObjectProperties() {
Expand Down Expand Up @@ -98,4 +100,11 @@ public void setMinimum(final String mininum) {
}
}

public void setFormat(final String format) {
this.format = format;
if (Objects.nonNull(format)) {
properties.add("Format");
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@

package com.sngular.api.generator.plugin.asyncapi.parameter;


import java.util.List;
import java.util.Map;

import com.sngular.api.generator.plugin.common.model.TimeType;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Builder.Default;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;
Expand All @@ -38,10 +42,24 @@ public final class OperationParameterObject {

private boolean useLombokModelAnnotation;

@Builder.Default
private String dateTimeFormat = "yyyy-MM-dd'T'HH:mm:ss";

@Builder.Default
private String dateFormat = "yyyy-MM-dd";

@Default
private TimeType useTimeType = TimeType.LOCAL;

@SuppressWarnings("unused")
private List<String> operationIds;

public List<String> getOperationIds() {
return StringUtils.isEmpty(ids) ? List.of() : List.of(ids.replace(" ", "").split(","));
}

public Map<String, String> getFormats() {
return Map.of("DATE_TIME", dateTimeFormat, "DATE", dateFormat);
}

}
Loading

0 comments on commit 1546f3b

Please sign in to comment.