Jackson deserialize json: The field `form_object_guid` in the JSON string is not defined in the `DigitalFormGetResponse` properties

26 views Asked by At

I query a GET and get a response JSON string but desalinization throws The field form_object_guid in the JSON string is not defined in the DigitalFormGetResponse properties, which means the field form_object_guid does not exist in the class DigitalFormGetResponse. The class DigitalFormGetResponse is automatically generated from maven artifact openapi-generator-maven-plugin version 6.6.0 without including jersey2 as library. After I added an annotation to the plugin <additionalModelTypeAnnotations>@com.fasterxml.jackson.annotation.JsonIgnoreProperties(ignoreUnknown = true)</additionalModelTypeAnnotations>, the generated code showed the class DigitalFormGetResponse has IgnoreProperties annotation and I assume it will ignore unknown properties in retrieved JSON string.

@com.fasterxml.jackson.annotation.JsonIgnoreProperties(ignoreUnknown = true)
public class DigitalFormGetResponse {

However, I got the exception from the auto-generated function:

enter image description here

My previous version used jersey2 in the plugin and worked fine to ignore unknown properties. However, I have to move jersey2 out of the plugin (because it does not support PATH requests for Java16+) and then this ignoring feature becomes an issue.

POM file

    <plugin>
        <groupId>org.openapitools</groupId>
        <artifactId>openapi-generator-maven-plugin</artifactId>
        <version>6.6.0</version>
        <executions>
            <execution>
                <id>spring-boot-api</id>
                <goals>
                    <goal>generate</goal>
                </goals>
                <configuration>
                    <inputSpec>${project.basedir}/digitalformsords.yaml</inputSpec>
                    <generatorName>java</generatorName>
                    <configOptions>
                        <dateLibrary>joda</dateLibrary>
                        <validateSpec>false</validateSpec>
                        <useSpringBoot3>true</useSpringBoot3>
                        <useJakartaEe>true</useJakartaEe>
                        <additionalModelTypeAnnotations>@com.fasterxml.jackson.annotation.JsonIgnoreProperties(ignoreUnknown = true)</additionalModelTypeAnnotations>
                    </configOptions>
                    <library>jersey2</library>
                    <apiPackage>${default-package}.api</apiPackage>
                    <modelPackage>${default-package}.api.model</modelPackage>
                    <invokerPackage>${default-package}.api.handler</invokerPackage>
                    <generateModelTests>false</generateModelTests>
                    <generateApiTests>false</generateApiTests>
                </configuration>
            </execution>
        </executions>
    </plugin>
1

There are 1 answers

0
jianmin tu On

I added maven-replacer-plugin to POM file to remove the invoking to validateJsonObject(jsonObj); so the exception will not happen. I do not think it is the best way but I have to keep the existing logic to not force a JSON validation.

    <plugin>
        <groupId>com.google.code.maven-replacer-plugin</groupId>
        <artifactId>replacer</artifactId>
        <version>1.5.3</version>
        <executions>
            <execution>
                <phase>generate-sources</phase>
                <goals>
                    <goal>replace</goal>
                </goals>
            </execution>
        </executions>
        <configuration>
            <includes>
                <include>${project.build.directory}/generated-sources/openapi/src/main/java/open/jag/ordsvipsclient/api/model/ErrorMessage.java</include>
                <include>${project.build.directory}/generated-sources/openapi/src/main/java/open/jag/ordsvipsclient/api/model/HealthOrdsResponse.java</include>
                <include>${project.build.directory}/generated-sources/openapi/src/main/java/open/jag/ordsvipsclient/api/model/VipsDisclosureSentOrdsRequest.java</include>
                <include>${project.build.directory}/generated-sources/openapi/src/main/java/open/jag/ordsvipsclient/api/model/VipsDisclosureSentOrdsResponse.java</include>
                <include>${project.build.directory}/generated-sources/openapi/src/main/java/open/jag/ordsvipsclient/api/model/VipsDocumentOrdsResponse.java</include>
                <include>${project.build.directory}/generated-sources/openapi/src/main/java/open/jag/ordsvipsclient/api/model/VipsProhibitionStatusOrdsResponse.java</include>
                <include>${project.build.directory}/generated-sources/openapi/src/main/java/open/jag/ordsvipsclient/api/model/VipsProhibitionStatusOrdsResponseDisclosureInner.java</include>
                <include>${project.build.directory}/generated-sources/openapi/src/main/java/open/jag/ordsvipsclient/api/model/VipsProhibitionStatusOrdsResponseReviewsInner.java</include>
            </includes>
            <replacements>
                <replacement>
                    <token>validateJsonObject\(jsonObj\);</token>
                    <value></value>
                </replacement>
            </replacements>
        </configuration>
    </plugin>
</plugins>