openapi-processor / openapi-processor-spring

an OpenAPI 3.0 & 3.1 interface & model java code generator for Spring Boot
https://docs.openapiprocessor.io
Apache License 2.0
40 stars 9 forks source link

@NotNull annotation is parsed in models even if it's not required (in schema) #123

Closed rombru closed 3 years ago

rombru commented 3 years ago

Hi,

Here is an example

openapi-processor-mapping: v2

options:
  package-name: be.unamur.nuts.web.api
  javadoc: true
  bean-validation: true
openapi: 3.0.2
info:
  title: nuts
  version: '1.0'
  contact: {}
  description: 'Test bug'
servers:
  - url: 'http://localhost:8080'
tags:
  - name: user
    description: Everything about users
paths:
  '/users/{userId}':
    parameters:
      - schema:
          type: integer
        name: userId
        in: path
        required: true
        description: Id of an existing user.
    get:
      summary: Get User Info by User ID
      tags:
        - user
      responses:
        '200':
          description: User Found
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/User'
              examples:
                Get User Alice Smith:
                  value:
                    id: 142
                    firstName: Alice
                    lastName: Smith
                    email: alice.smith@gmail.com
                    dateOfBirth: '1997-10-31'
                    emailVerified: true
                    signUpDate: '2019-08-24'
        '404':
          description: User Not Found
      operationId: get-users-userId
      description: Retrieve the information of the user with the matching user ID.

components:
  schemas:
    User:
      title: User
      type: object
      description: ''
      x-examples:
        Alice Smith:
          id: 142
          firstName: Alice
          lastName: Smith
      properties:
        id:
          type: integer
          description: Unique identifier for the given user.
        firstName:
          type: string
          description: The firstname of the user
        lastName:
          type: string
          description: The lastname of the user
      required:
        - lastName
  examples: {}

The result is the following Model:

public class User {
    @JsonProperty("id")
    @NotNull
    private Integer id;
    @JsonProperty("firstName")
    @NotNull
    private String firstName;
    @JsonProperty("lastName")
    @NotNull
    private String lastName;

    public User() {
    }
    ....
}

spring processor version: 1.0.0.M20 maven plugin version: 1.0.0.M2

The id and the firstName should not be @NotNull, only lastName is required

hauner commented 3 years ago

Yes, that was a bug in M20, fixed in https://github.com/openapi-processor/openapi-processor-core/issues/44

It will work with oap-spring 2021.2.

rombru commented 3 years ago

Thanks ! Works like a charm :partying_face: