micronaut-openapi icon indicating copy to clipboard operation
micronaut-openapi copied to clipboard

javadoc for record components not read

Open graemerocher opened this issue 2 years ago • 2 comments

Expected Behavior

Should read javadoc comments from @param tags of records:

/**
 *
 * @param name The name of the pet
 * @author gkrocher
 */
@Serdeable
public record Pet(
        @NotBlank
    @Size(max = 200)
    String name) {}

Actual Behaviour

Doesn't read the javadoc

Steps To Reproduce

No response

Environment Information

No response

Example Application

No response

Version

3.7.1

graemerocher avatar Oct 04 '22 10:10 graemerocher

@graemerocher Need full example for it. I already wrote test for records:

    void "test read javadoc from class level for records"() {
        given:
        when:
        buildBeanDefinition('test.MyBean', '''
package test;

import io.micronaut.core.annotation.Nullable;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;

@Controller
class PersonController {

    @Get("/person")
    Person getPerson() {
        return new Person("John", 42);
    }
}

/**
 * The Person class description
 *
 * @param name this is persons name
 * @param age this is persons age
 *
 * @return new instance of Person class
*/
record Person(@Nullable String name, Integer age) {}

@jakarta.inject.Singleton
class MyBean {}
''')
        then:
        Utils.testReference != null

        when:
        OpenAPI openAPI = Utils.testReference

        then:
        openAPI.components.schemas
        openAPI.components.schemas.size() == 1

        Schema personSchema = openAPI.components.schemas['Person']
        personSchema.name == 'Person'
        personSchema.type == 'object'
        personSchema.description == 'The Person class description'
        personSchema.properties.name.type == 'string'
        personSchema.properties.name.description == 'this is persons name'
        personSchema.properties.age.type == 'integer'
        personSchema.properties.age.description == 'this is persons age'
        personSchema.required
        personSchema.required.size() == 1
        personSchema.required.contains('age')
    }

And now I wrote another test for your case - it works too:

    void "test read javadoc from class level for records - 2"() {
        given:
        when:
        buildBeanDefinition('test.MyBean', '''
package test;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;

import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
import io.micronaut.serde.annotation.Serdeable;

@Controller
class PetController {

    @Get("/pet")
    Pet getPet() {
        return new Pet("John");
    }
}

/**
 *
 * @param name The name of the pet
 * @author gkrocher
 */
@Serdeable
record Pet(
        @NotBlank
    @Size(max = 200)
    String name) {}

@jakarta.inject.Singleton
class MyBean {}
''')
        then:
        Utils.testReference != null

        when:
        OpenAPI openAPI = Utils.testReference

        then:
        openAPI.components.schemas
        openAPI.components.schemas.size() == 1

        Schema petchema = openAPI.components.schemas['Pet']
        petchema.name == 'Pet'
        petchema.type == 'object'
        petchema.description == null
        petchema.properties.name.type == 'string'
        petchema.properties.name.description == 'The name of the pet'
        petchema.required
        petchema.required.size() == 1
        petchema.required.contains('name')
    }

altro3 avatar Oct 04 '22 14:10 altro3

@graemerocher Checked this on sample project - all works fine:

Controller:

@Controller
public class TestController {

    @Get
    public Pet test() {
        return null;
    }
}

DTO:

/**
 * @param name The name of the pet
 * @author gkrocher
 */
@Serdeable
public record Pet(
        @NotBlank
        @Size(max = 200)
        String name) {
}

Generated yml:

openapi: 3.0.1
info:
  title: mn-schema-override-exemplar
  version: "0.0"
paths:
  /:
    get:
      operationId: test
      responses:
        "200":
          description: test 200 response
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Pet'
components:
  schemas:
    Pet:
      required:
      - name
      type: object
      properties:
        name:
          maxLength: 200
          minLength: 1
          type: string
          description: The name of the pet

Maybe something got cached, try clearing the build and restarting it with org.gradle.caching = false

If the problem persists, then you need a full-fledged example to reproduce, because I can't reproduce the problem locally

altro3 avatar Oct 08 '22 14:10 altro3

@graemerocher ping you about this issue. I still can't reproduce your problem. Could you give me sample app ?

altro3 avatar Oct 28 '22 11:10 altro3

let me try reproduce

graemerocher avatar Oct 28 '22 11:10 graemerocher

ok seems to work, closing. Thanks!

graemerocher avatar Oct 28 '22 11:10 graemerocher