spring-bootmavenspringdoc-openapi-uiopenapi-generator-maven-plugin

OpenApi maven-plugin-generator attributes error


I'm using SpringBoot 3.4.1, Java 21 and I'm trying to generate Spring server classes (Api plus Model).

I have this maven config on the generator project module:

 <dependency>
     <groupId>org.springdoc</groupId>
     <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
     <version>2.7.0</version>
 </dependency>
 
 <build>
    <plugins>
        <plugin>
            <groupId>org.openapitools</groupId>
            <artifactId>openapi-generator-maven-plugin</artifactId>
            <version>7.10.0</version>
            <executions>
                <execution>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                    <configuration>
                        <inputSpec>${project.basedir}/src/main/resources/api-docs.yaml</inputSpec>
                        <output>${project.build.directory}/generated-sources</output>
                        <generatorName>spring</generatorName>
                        <apiPackage>com.api.rest.web.resources</apiPackage>
                        <modelPackage>com.api.rest.model</modelPackage>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

I have also written this simple .yaml file to test the generator:

openapi: "3.0.2"
info:
title: Example Rest Archetype
version: 0.0.1

components:
  schemas:
    User:
      type: object
      properties:
        id:
          type: integer
          format: int64
        name:
          type: string
          minLength: 3
          maxLength: 20
        surname:
          type: string
          minLength: 3
          maxLength: 20
        username:
          type: string
          minLength: 3
          maxLength: 30
        email:
          type: string
          minLength: 3
          maxLength: 30
        address:
          type: array
          items:
            $ref: "#/components/schemas/Address"
      required:
       - name
       - surname
       - username
    Address:
      type: object
      properties:
        id:
          type: integer
          format: int64
        description:
          type: string
          minLength: 3
          maxLength: 20
        user:
          type: object
          $ref: "#/components/schemas/User"
    Error:
      type: object
      properties:
        code:
          type: integer
          format: int32
        message:
          type: string
      required:
       - code
       - message

That's it. I know that the attribute paths is missing but when I compile the module I receive other two errors and I don't understand why. This is the result:

Errors: 
 -attribute paths is missing
 -attribute components.schemas.Error.properties is not of type `object`
 -attribute components.schemas.User.properties is not of type `object`

What's wrong?


I corrected my .yaml file because I used IntelliJ editor and probably the indentation error was due to adding space to formatting it as a code here. Anyway I missed the jackson dependency on my pom.xml and surely this caused the error.

Thanks a lot.


Solution

  • There are some indentation errors in your yaml. I added the missing paths: {} in order to enable build.

    Corrected file

    openapi: "3.0.2"
    info:
      title: Example Rest Archetype
      version: 0.0.1
    
    paths: {}
    
    components:
      schemas:
        User:
          type: object
          properties:
            id:
              type: integer
              format: int64
            name:
              type: string
              minLength: 3
              maxLength: 20
            surname:
              type: string
              minLength: 3
              maxLength: 20
            username:
              type: string
              minLength: 3
              maxLength: 30
            email:
              type: string
              minLength: 3
              maxLength: 30
            address:
              type: array
              items:
                $ref: "#/components/schemas/Address"
          required:
            - name
            - surname
            - username
        Address:
          type: object
          properties:
            id:
              type: integer
              format: int64
            description:
              type: string
              minLength: 3
              maxLength: 20
            user:
              $ref: "#/components/schemas/User"
        Error:
          type: object
          properties:
            code:
              type: integer
              format: int32
            message:
              type: string
          required:
            - code
            - message
    

    In order to generate Jakarta-annotations, I added <useJakartaEe>true</useJakartaEe>.

    Full POM

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>3.4.1</version>
            <relativePath/>
        </parent>
        <groupId>com.api.rest</groupId>
        <artifactId>demo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>demo</name>
        <description>Demo project for Spring Boot</description>
        <properties>
            <java.version>21</java.version>
        </properties>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springdoc</groupId>
                <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
                <version>2.7.0</version>
            </dependency>
            <dependency>
                <groupId>org.openapitools</groupId>
                <artifactId>jackson-databind-nullable</artifactId>
                <version>0.2.6</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.openapitools</groupId>
                    <artifactId>openapi-generator-maven-plugin</artifactId>
                    <version>7.10.0</version>
                    <executions>
                        <execution>
                            <goals>
                                <goal>generate</goal>
                            </goals>
                            <configuration>
                                <inputSpec>${project.basedir}/src/main/resources/api-docs.yaml</inputSpec>
                                <output>${project.build.directory}/generated-sources</output>
                                <generatorName>spring</generatorName>
                                <apiPackage>com.api.rest.web.resources</apiPackage>
                                <modelPackage>com.api.rest.model</modelPackage>
                                <configOptions>
                                    <useJakartaEe>true</useJakartaEe>
                                </configOptions>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <configuration>
                        <mainClass>com.api.rest.DemoApplication</mainClass>
                    </configuration>
                    <executions>
                        <execution>
                            <goals>
                                <goal>build-info</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </project>