Skip to content

[Fix #634] Generated classes to not depend on jackson #635

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jul 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 51 additions & 60 deletions api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,28 @@

<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<groupId>io.serverlessworkflow</groupId>
<artifactId>serverlessworkflow-types</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<groupId>io.serverlessworkflow</groupId>
<artifactId>serverlessworkflow-serialization</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.networknt</groupId>
<artifactId>json-schema-validator</artifactId>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<groupId>com.networknt</groupId>
<artifactId>json-schema-validator</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
</dependency>
<dependency>
<groupId>jakarta.validation</groupId>
<artifactId>jakarta.validation-api</artifactId>
</dependency>

<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
Expand Down Expand Up @@ -79,54 +78,46 @@
<scope>test</scope>
</dependency>
</dependencies>


<build>
<plugins>
<plugin>
<groupId>org.jsonschema2pojo</groupId>
<artifactId>jsonschema2pojo-maven-plugin</artifactId>
<plugins>

<plugin>
<groupId>io.serverlessworkflow</groupId>
<artifactId>jackson-generator</artifactId>
<version>${project.version}</version>
<dependencies/>
<configuration>
<targetPackage>io.serverlessworkflow.api.types</targetPackage>
</configuration>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<phase>generate-sources</phase>
</execution>
</executions>
</plugin>
<plugin>
<!-- a hint for IDE's to add the java sources to the classpath -->
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.3.0</version>
<executions>
<execution>
<id>add-mixin</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sourceDirectory>${basedir}/src/main/resources/schema</sourceDirectory>
<!--The comment below is left intentionally in case jsonschema2pojo one day accepts https urls. That day we can remove the file from schema dir and use directly the real schema-->
<!-- <sourcePaths>
<sourcePath>https://raw.githubusercontent.com/serverlessworkflow/specification/main/schema/workflow.yaml</sourcePath>
</sourcePaths> -->
<sourceType>yamlschema</sourceType>
<targetPackage>io.serverlessworkflow.api.types</targetPackage>
<outputDirectory>${project.build.directory}/generated-sources/src/main/java</outputDirectory>
<includeJsr303Annotations>true</includeJsr303Annotations>
<generateBuilders>true</generateBuilders>
<initializeCollections>true</initializeCollections>
<includeAdditionalProperties>true</includeAdditionalProperties>
<includeToString>false</includeToString>
<includeHashcodeAndEquals>false</includeHashcodeAndEquals>
<includeConstructors>true</includeConstructors>
<constructorsRequiredPropertiesOnly>true</constructorsRequiredPropertiesOnly>
<useTitleAsClassname>true</useTitleAsClassname>
<serializable>true</serializable>
<targetVersion>${java.version}</targetVersion>
<usePrimitives>true</usePrimitives>
<useJakartaValidation>true</useJakartaValidation>
<customRuleFactory>io.serverlessworkflow.generator.UnreferencedFactory</customRuleFactory>
<customAnnotator>io.serverlessworkflow.generator.ConstAnnotator</customAnnotator>
<sources>
<source>${project.build.directory}/generated-sources/jacksonmixinpojo</source>
</sources>
</configuration>
<dependencies>
<dependency>
<groupId>io.serverlessworkflow</groupId>
<artifactId>serverless-workflow-custom-generator</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<phase>generate-sources</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
*/
package io.serverlessworkflow.api;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator.Feature;
import io.serverlessworkflow.api.types.JacksonMixInModule;
import io.serverlessworkflow.serialization.BeanDeserializerModifierWithValidation;
import io.serverlessworkflow.serialization.URIDeserializer;
import io.serverlessworkflow.serialization.URISerializer;
Expand Down Expand Up @@ -47,10 +49,12 @@ private static ObjectMapper configure(ObjectMapper mapper) {
validationModule.setDeserializerModifier(new BeanDeserializerModifierWithValidation());

return mapper
.setSerializationInclusion(Include.NON_NULL)
.configure(SerializationFeature.INDENT_OUTPUT, true)
.configure(SerializationFeature.WRITE_EMPTY_JSON_ARRAYS, false)
.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false)
.registerModule(validationModule);
.registerModule(validationModule)
.registerModule(new JacksonMixInModule());
}

private ObjectMapperFactory() {}
Expand Down
6 changes: 6 additions & 0 deletions custom-generator/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@
<groupId>org.jsonschema2pojo</groupId>
<artifactId>jsonschema2pojo-core</artifactId>
</dependency>
<dependency>
<groupId>io.serverlessworkflow</groupId>
<artifactId>serverlessworkflow-annotations</artifactId>
<version>${project.version}</version>
</dependency>

</dependencies>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@
package io.serverlessworkflow.generator;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.sun.codemodel.JAnnotationArrayMember;
import com.sun.codemodel.JBlock;
import com.sun.codemodel.JClass;
import com.sun.codemodel.JClassAlreadyExistsException;
Expand All @@ -33,6 +32,9 @@
import com.sun.codemodel.JPackage;
import com.sun.codemodel.JType;
import com.sun.codemodel.JVar;
import io.serverlessworkflow.annotations.OneOfSetter;
import io.serverlessworkflow.annotations.OneOfValueProvider;
import io.serverlessworkflow.annotations.Union;
import jakarta.validation.ConstraintViolationException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
Expand Down Expand Up @@ -316,28 +318,11 @@ private JDefinedClass populateOneOf(
commonType.orElse(definedClass.owner().ref(Object.class)),
ruleFactory.getNameHelper().getPropertyName("value", null),
null);

definedClass._implements(
definedClass
.owner()
.ref(GeneratorUtils.ONE_OF_VALUE_PROVIDER_INTERFACE_NAME)
.narrow(valueField.type()));
definedClass.owner().ref(OneOfValueProvider.class).narrow(valueField.type()));
GeneratorUtils.implementInterface(definedClass, valueField);
try {
JDefinedClass serializer = generateSerializer(definedClass);
definedClass.annotate(JsonSerialize.class).param("using", serializer);
} catch (JClassAlreadyExistsException ex) {
// already serialized aware
}

try {
JDefinedClass deserializer =
generateDeserializer(definedClass, oneOfTypes, "deserializeOneOf");
definedClass.annotate(JsonDeserialize.class).param("using", deserializer);
} catch (JClassAlreadyExistsException ex) {
// already deserialized aware
}

JAnnotationArrayMember unionAnnotation = definedClass.annotate(Union.class).paramArray("value");
oneOfTypes.forEach(t -> unionAnnotation.param(t.getType()));
return wrapAll(parentSchema, definedClass, commonType, oneOfTypes, Optional.of(valueField));
}

Expand Down Expand Up @@ -388,51 +373,6 @@ private static boolean isStringType(JType type) {
return type.name().equals("String");
}

private JDefinedClass generateSerializer(JDefinedClass relatedClass)
throws JClassAlreadyExistsException {
JDefinedClass definedClass = GeneratorUtils.serializerClass(relatedClass);
GeneratorUtils.fillSerializer(
definedClass,
relatedClass,
(method, valueParam, genParam) ->
method
.body()
.staticInvoke(
definedClass.owner().ref(GeneratorUtils.SERIALIZE_HELPER_NAME),
"serializeOneOf")
.arg(genParam)
.arg(valueParam));
return definedClass;
}

private JDefinedClass generateDeserializer(
JDefinedClass relatedClass, Collection<JTypeWrapper> oneOfTypes, String methodName)
throws JClassAlreadyExistsException {
JDefinedClass definedClass = GeneratorUtils.deserializerClass(relatedClass);
GeneratorUtils.fillDeserializer(
definedClass,
relatedClass,
(method, parserParam) -> {
JBlock body = method.body();

body._return(
definedClass
.owner()
.ref(GeneratorUtils.DESERIALIZE_HELPER_NAME)
.staticInvoke(methodName)
.arg(parserParam)
.arg(relatedClass.dotclass())
.arg(list(definedClass, oneOfTypes)));
});
return definedClass;
}

private JInvocation list(JDefinedClass definedClass, Collection<JTypeWrapper> list) {
JInvocation result = definedClass.owner().ref(List.class).staticInvoke("of");
list.forEach(c -> result.arg(((JClass) c.getType()).dotclass()));
return result;
}

private void wrapIt(
Schema parentSchema,
JDefinedClass definedClass,
Expand Down Expand Up @@ -460,7 +400,7 @@ private JVar setupMethod(
v -> {
method.body().assign(JExpr._this().ref(v), methodParam);
method
.annotate(definedClass.owner().ref(GeneratorUtils.SETTER_ANNOTATION_NAME))
.annotate(definedClass.owner().ref(OneOfSetter.class))
.param("value", instanceField.type());
});
return methodParam;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,24 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.sun.codemodel.JDefinedClass;
import com.sun.codemodel.JFieldVar;
import io.serverlessworkflow.annotations.AdditionalProperties;
import jakarta.validation.constraints.Pattern;
import org.jsonschema2pojo.AbstractAnnotator;
import org.jsonschema2pojo.GenerationConfig;

public class ConstAnnotator extends AbstractAnnotator {
public class CustomAnnotator extends AbstractAnnotator {

private static final String CONST = "const";

public ConstAnnotator(GenerationConfig generationConfig) {
public CustomAnnotator(GenerationConfig generationConfig) {
super(generationConfig);
}

@Override
public void additionalPropertiesField(JFieldVar field, JDefinedClass clazz, String propertyName) {
clazz.annotate(AdditionalProperties.class);
}

@Override
public void propertyField(
JFieldVar field, JDefinedClass clazz, String propertyName, JsonNode propertyNode) {
Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy