3

TLDR:

OpenAPI offers 'oneOf' property. The resulting java classes created do not seem to allow one of the possible instances.

Details:

I am creating the spring/java server side rest api code using OpenAPI maven plugin.

The request class is not such that the passed object is not parsed correctly. The following error is printed in the console.

JSON parse error: Could not resolve subtype of [simple type, class com.model.Issuer]: missing type id property 'type' (for POJO property 'issuer'); nested exception is com.fasterxml.jackson.databind.exc.InvalidTypeIdException: Could not resolve subtype of [simple type, class com.Issuer]: missing type id property 'type' (for POJO property 'issuer')\n at [Source: (PushbackInputStream); line: 3, column: 15] (through reference chain: com.IssueCredentialRequest[\"credential\"]->com.Credential[\"issuer\"])

The reason seem to be that the generated class does not have subtypes list in the annotation:

package com.sphereon.vdp.vc.service.model;


import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;

@JsonTypeInfo(
  use = JsonTypeInfo.Id.NAME,
  include = JsonTypeInfo.As.PROPERTY,
  property = "type")
@JsonSubTypes({
})
public interface OneOfIssuer {

}

There is another class which is generated correctly. The reason that this one is generated correctly is probably that this one deals with non-primitive types.

package com.sphereon.vdp.vc.service.model;

import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;

@JsonTypeInfo(
  use = JsonTypeInfo.Id.NAME,
  include = JsonTypeInfo.As.PROPERTY,
  property = "type")
@JsonSubTypes({
  @JsonSubTypes.Type(value = VerifyPresentationRequest.class, name =     "VerifyPresentationRequest"),
  @JsonSubTypes.Type(value = ProoflessVerifyPresentationRequest.class, name = "ProoflessVerifyPresentationRequest")
})
public interface OneOfpresentationsVerifyBody {

}

Can someone point to how to fix the code generation for primitive types?

<plugin>
    <groupId>io.swagger.codegen.v3</groupId>
    <artifactId>swagger-codegen-maven-plugin</artifactId>
    <version>3.0.33</version>
    <dependencies>
      <dependency>
        <groupId>com.github.jknack</groupId>
        <artifactId>handlebars</artifactId>
        <version>4.3.0</version>
      </dependency>
    </dependencies>
    <executions>
      <execution>
        <id>vc-rest-api-issuer-source-generation</id>
        <goals>
          <goal>generate</goal>
        </goals>
        <phase>generate-sources</phase>
        <configuration>
          <inputSpec>${pom.basedir}/specifications/issuer.yml</inputSpec>

          <language>spring</language>

          <apiPackage>com.company.vdp.vc.service.api</apiPackage>
          <modelPackage>com.company.vdp.vc.service.model</modelPackage>
          <artifactVersion>${project.version}</artifactVersion>

          <generateModels>true</generateModels>
          <generateApis>true</generateApis>
          <generateModelDocumentation>true</generateModelDocumentation>
          <generateSupportingFiles>true</generateSupportingFiles>

          <verbose>${openapi-codegen-verbose}</verbose>
          <output>${project.basedir}/target/generated-sources/java/api</output>
          <ignoreFileOverride>${project.basedir}/target/generated-sources/java/api/.swagger-codegen-ignore</ignoreFileOverride>

          <configOptions>
            <delegatePattern>true</delegatePattern>
            <dateLibrary>java8</dateLibrary>
            <useTags>true</useTags>
          </configOptions>
        </configuration>
      </execution>
      <execution>
        <id>vc-rest-api-verifier-source-generation</id>
        <goals>
          <goal>generate</goal>
        </goals>
        <phase>generate-sources</phase>
        <configuration>
          <inputSpec>${pom.basedir}/specifications/verifier.yml</inputSpec>

          <language>spring</language>

          <apiPackage>com.company.vdp.vc.service.api</apiPackage>
          <modelPackage>com.company.vdp.vc.service.model</modelPackage>
          <artifactVersion>${project.version}</artifactVersion>

          <generateModels>true</generateModels>
          <generateApis>true</generateApis>
          <generateModelDocumentation>true</generateModelDocumentation>
          <generateSupportingFiles>true</generateSupportingFiles>

          <verbose>${openapi-codegen-verbose}</verbose>
          <output>${project.basedir}/target/generated-sources/java/api</output>
          <ignoreFileOverride>${project.basedir}/target/generated-sources/java/api/.swagger-codegen-ignore</ignoreFileOverride>

          <configOptions>
            <delegatePattern>true</delegatePattern>
            <dateLibrary>java8</dateLibrary>
            <useTags>true</useTags>
          </configOptions>
        </configuration>
      </execution>
    </executions>
  </plugin>

Following is the spec file that I am using without editing.

  1. https://github.com/w3c-ccg/vc-api/blob/main/components/Issuer.yml
  2. https://github.com/w3c-ccg/vc-api/blob/main/verifier.yml
Community
  • 1
  • 1
zur
  • 1,121
  • 1
  • 11
  • 21
  • I have the same issue. note that there is a new plugin openapi-generator-maven-plugin also. This plugin still has the same issue for primitive types. – Coen Damen Sep 15 '22 at 07:59

0 Answers0