Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: @RestResource annotation and source generation #97

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from

Commits on May 22, 2024

  1. feat: @RestResource annotation and source generation

    This PR adds the @RestResource annotation and a visitor. the idea is to generate to combine the annotation with a Micronaut Data CrudRepository to generate a REST controller.
    
    For example, for a Book entity:
    
    ```java
    import io.micronaut.core.annotation.Nullable;
    import io.micronaut.data.annotation.GeneratedValue;
    import io.micronaut.data.annotation.Id;
    import io.micronaut.data.annotation.MappedEntity;
    import io.micronaut.serde.annotation.Serdeable;
    
    @Serdeable
    @MappedEntity
    public record Book(@GeneratedValue @id @nullable Long id, String title) {
    }
    ```
    
    and a JDBC repository:
    
    ```java
    import io.micronaut.data.jdbc.annotation.JdbcRepository;
    import io.micronaut.data.model.query.builder.sql.Dialect;
    import io.micronaut.data.repository.CrudRepository;
    
    @JdbcRepository(dialect = Dialect.H2)
    public interface BookRepository extends CrudRepository<Book, Long> {
    }
    ```
    
    And a class annotated with `@RestResource`:
    
    ```java
    import io.micronaut.sourcegen.annotations.RestResource;
    
    @RestResource(repository = BookRepository.class, rolesAllowed = { "isAnonymous()" })
    class BookRestResource {
    }
    ```
    
    Generates the following controller:
    
    ```java
    @controller("/books")
    @RolesAllowed("isAnonymous()")
    class BookController {
      @Inject
      BookRepository repository;
    
      @get
      Iterable<Book> findAll() {
        return this.repository.findAll();
      }
    
      @get("/{id}")
      Optional<Book> findById(Long id) {
        return this.repository.findById(id);
      }
    
      @delete("/{id}")
      @Status(HttpStatus.NO_CONTENT)
      void deleteById(Long id) {
        this.repository.deleteById(id);
      }
    
      @Status(HttpStatus.CREATED)
      @post
      void save(@Body Book entity) {
        this.repository.save(entity);
      }
    
      @put
      @Status(HttpStatus.NO_CONTENT)
      void update(@Body Book entity) {
        this.repository.update(entity);
      }
    }
    ```
    sdelamo committed May 22, 2024
    Configuration menu
    Copy the full SHA
    8ba1a75 View commit details
    Browse the repository at this point in the history
  2. remove println statements

    sdelamo committed May 22, 2024
    Configuration menu
    Copy the full SHA
    1927a5a View commit details
    Browse the repository at this point in the history
  3. refactor visitor

    sdelamo committed May 22, 2024
    Configuration menu
    Copy the full SHA
    6666cff View commit details
    Browse the repository at this point in the history
  4. checkstyle private

    sdelamo committed May 22, 2024
    Configuration menu
    Copy the full SHA
    5331f18 View commit details
    Browse the repository at this point in the history