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

Add OPEN API / DTO generation #52

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
4a6c987
Moving to springdoc dependency
alexandre-touret Sep 1, 2021
a7f524a
Remove old springfox swagger configuration and starting springdoc con…
alexandre-touret Sep 1, 2021
aebdc38
feat: enable model generation during buildtime using openapi generator.
alexandre-touret Sep 8, 2021
a4ca4b1
feat!: enable dto genertation using openapi generator. First tests do…
alexandre-touret Sep 14, 2021
923df72
refactor: drop json attributes in Pet entity
alexandre-touret Sep 14, 2021
7a8b4d1
feat: add Owner entity and trying to resolve cyclic dependency
alexandre-touret Oct 15, 2021
c667f55
feat: Add Owner and Pet contract to code generation
alexandre-touret Oct 28, 2021
c0b533d
feat: Finalizes Contract mode generation
alexandre-touret Oct 30, 2021
6a15d65
feat: Add api-docs.yml service contract
alexandre-touret Oct 30, 2021
f035c06
fix: Add mapper's javadoc
alexandre-touret Oct 30, 2021
08477e9
Moving to springdoc dependency
alexandre-touret Sep 1, 2021
59442e0
Remove old springfox swagger configuration and starting springdoc con…
alexandre-touret Sep 1, 2021
6e71cec
feat: enable model generation during buildtime using openapi generator.
alexandre-touret Sep 8, 2021
38c3ded
feat!: enable dto genertation using openapi generator. First tests do…
alexandre-touret Sep 14, 2021
deaac03
refactor: drop json attributes in Pet entity
alexandre-touret Sep 14, 2021
c873dd2
feat: add Owner entity and trying to resolve cyclic dependency
alexandre-touret Oct 15, 2021
1674f3f
feat: Add Owner and Pet contract to code generation
alexandre-touret Oct 28, 2021
c9e139f
feat: Finalizes Contract mode generation
alexandre-touret Oct 30, 2021
ae0e51f
feat: Add api-docs.yml service contract
alexandre-touret Oct 30, 2021
523ce61
fix: Add mapper's javadoc
alexandre-touret Oct 30, 2021
0b9ada0
Fix merge issue
alexandre-touret Nov 1, 2021
b993da6
fix: fix unit tests & @arey comments
alexandre-touret Nov 9, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 107 additions & 9 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,16 @@

<properties>
<spring-data-jdbc.version>1.2.1.RELEASE</spring-data-jdbc.version>
<springfox-swagger.version>2.9.2</springfox-swagger.version>
<springfox-swagger.version>3.0.0</springfox-swagger.version>
<jacoco.version>0.8.5</jacoco.version>

<docker.jib-maven-plugin.version>1.3.0</docker.jib-maven-plugin.version>
<docker.image.prefix>springcommunity</docker.image.prefix>
<jsr305.version>3.0.2</jsr305.version>
<jackson-databind-nullable.version>0.2.1</jackson-databind-nullable.version>
<mapstruct.version>1.4.1.Final</mapstruct.version>
alexandre-touret marked this conversation as resolved.
Show resolved Hide resolved
<jaxb-api.version>2.3.0</jaxb-api.version>
<openapi-generator-maven-plugin.version>5.2.1</openapi-generator-maven-plugin.version>
<build-helper-maven-plugin.version>3.2.0</build-helper-maven-plugin.version>
</properties>

<dependencies>
Expand Down Expand Up @@ -123,16 +128,26 @@
<artifactId>json-path</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<artifactId>springfox-boot-starter</artifactId>
<version>${springfox-swagger.version}</version>
</dependency>

<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${springfox-swagger.version}</version>
<groupId>org.openapitools</groupId>
<artifactId>jackson-databind-nullable</artifactId>
<version>${jackson-databind-nullable.version}</version>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>${mapstruct.version}</version>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${mapstruct.version}</version>
</dependency>

<dependency>
Expand All @@ -144,7 +159,7 @@
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
<version>${jaxb-api.version}</version>
</dependency>
</dependencies>

Expand Down Expand Up @@ -234,7 +249,90 @@
</to>
</configuration>
</plugin>


<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<!-- RELEASE_VERSION -->
<version>${openapi-generator-maven-plugin.version}</version>
<!-- /RELEASE_VERSION -->
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>${project.basedir}/src/main/resources/api-docs.yml</inputSpec>
<modelPackage>org.springframework.samples.petclinic.dto</modelPackage>
<generatorName>spring</generatorName>
<library>spring-boot</library>

<!-- Only enabling model generation -->
<modelNameSuffix>Dto</modelNameSuffix>
<generateApiTests>false</generateApiTests>
<generateModelDocumentation>false</generateModelDocumentation>
<generateModelTests>false</generateModelTests>
<generateApis>false</generateApis>
arey marked this conversation as resolved.
Show resolved Hide resolved
<generateApiDocumentation>false</generateApiDocumentation>
<generateModels>true</generateModels>
<generateSupportingFiles>false</generateSupportingFiles>
<!-- Activating JAVA8 features -->
<configOptions>
<performBeanValidation>true</performBeanValidation>
<dateLibrary>legacy</dateLibrary>
arey marked this conversation as resolved.
Show resolved Hide resolved
<java8>true</java8>
<openApiNullable>false</openApiNullable>
<serializationLibrary>jackson</serializationLibrary>
</configOptions>
<!-- override the default library to jersey2 -->
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>${build-helper-maven-plugin.version}</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>target/generated-sources/openapi/src/main/java</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${mapstruct.version}</version>
</path>
</annotationProcessorPaths>
<compilerArgs>
<compilerArg>
-Amapstruct.suppressGeneratorTimestamp=true
</compilerArg>
<compilerArg>
-Amapstruct.suppressGeneratorVersionInfoComment=true
</compilerArg>
<compilerArg>
-Amapstruct.defaultComponentModel=spring
</compilerArg>
</compilerArgs>
</configuration>
</plugin>
</plugins>
</build>

</project>
8 changes: 6 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,12 @@ There are actuator health check and info routes as well:
* [http://localhost:9966/petclinic/actuator/health](http://localhost:9966/petclinic/actuator/health)
* [http://localhost:9966/petclinic/actuator/info](http://localhost:9966/petclinic/actuator/info)

## Swagger REST API documentation presented here (after application start):
[http://localhost:9966/petclinic/swagger-ui.html](http://localhost:9966/petclinic/swagger-ui.html)
## OpenAPI REST API documentation presented here (after application start):

You can reach the swagger UI with this URL
[http://localhost:9966/petclinic/](http://localhost:9966/petclinic/swagger-ui.html).

You then can get the Open API description reaching this URL [localhost:9966/petclinic/v3/api-docs](localhost:9966/petclinic/v3/api-docs).

## Screenshot of the Angular client

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.springframework.samples.petclinic.mapper;

import org.mapstruct.Mapper;
import org.springframework.samples.petclinic.dto.OwnerDto;
import org.springframework.samples.petclinic.model.Owner;

import java.util.Collection;

/**
* Maps Owner & OwnerDto using Mapstruct
*/
@Mapper(uses = PetMapper.class)
public interface OwnerMapper {

OwnerDto toOwnerDto(Owner owner);

Owner toOwner(OwnerDto ownerDto);

Collection<OwnerDto> toOwnerDtoCollection(Collection<Owner> ownerCollection);

Collection<Owner> toOwners(Collection<OwnerDto> ownerDtos);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.springframework.samples.petclinic.mapper;

import org.mapstruct.Mapper;
import org.springframework.samples.petclinic.dto.PetDto;
import org.springframework.samples.petclinic.dto.PetTypeDto;
import org.springframework.samples.petclinic.model.Pet;
import org.springframework.samples.petclinic.model.PetType;

import java.util.Collection;

/**
* Map Pet & PetDto using mapstruct
*/
@Mapper
public interface PetMapper {
PetDto toPetDto(Pet pet);

Collection<PetDto> toPetsDto(Collection<Pet> pets);

Collection<Pet> toPets(Collection<PetDto> pets);

Pet toPet(PetDto petDto);

PetTypeDto toPetTypeDto(PetType petType);

PetType toPetType(PetTypeDto petTypeDto);

Collection<PetTypeDto> toPetTypeDtos(Collection<PetType> petTypes);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.springframework.samples.petclinic.mapper;

import org.mapstruct.Mapper;
import org.springframework.samples.petclinic.dto.PetTypeDto;
import org.springframework.samples.petclinic.model.PetType;

import java.util.Collection;

/**
* Map PetType & PetTypeDto using mapstruct
*/
@Mapper
public interface PetTypeMapper {

PetType toPetType(PetTypeDto petTypeDto);

PetTypeDto toPetTypeDto(PetType petType);

Collection<PetTypeDto> toPetTypeDtos(Collection<PetType> petTypes);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.springframework.samples.petclinic.mapper;

import org.mapstruct.Mapper;
import org.springframework.samples.petclinic.dto.SpecialtyDto;
import org.springframework.samples.petclinic.model.Specialty;

import java.util.Collection;

/**
* Map Specialty & SpecialtyDto using mapstruct
*/
@Mapper
public interface SpecialtyMapper {
Specialty toSpecialty(SpecialtyDto specialtyDto);

SpecialtyDto toSpecialtyDto(Specialty specialty);

Collection<SpecialtyDto> toSpecialtyDtos(Collection<Specialty> specialties);

Collection<Specialty> toSpecialtys(Collection<SpecialtyDto> specialties);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.springframework.samples.petclinic.mapper;

import org.mapstruct.Mapper;
import org.springframework.samples.petclinic.dto.RoleDto;
import org.springframework.samples.petclinic.dto.UserDto;
import org.springframework.samples.petclinic.model.Role;
import org.springframework.samples.petclinic.model.User;

import java.util.Collection;

/**
* Map User/Role & UserDto/RoleDto using mapstruct
*/
@Mapper
public interface UserMapper {
Role toRole(RoleDto roleDto);

RoleDto toRoleDto(Role role);

Collection<RoleDto> toRoleDtos(Collection<Role> roles);

User toUser(UserDto userDto);

UserDto toUserDto(User user);

Collection<Role> toRoles(Collection<RoleDto> roleDtos);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.springframework.samples.petclinic.mapper;

import org.mapstruct.Mapper;
import org.springframework.samples.petclinic.dto.VetDto;
import org.springframework.samples.petclinic.model.Vet;

import java.util.Collection;

/**
* Map Vet & VetoDto using mapstruct
*/
@Mapper(uses = SpecialtyMapper.class)
public interface VetMapper {
Vet toVet(VetDto vetDto);

VetDto toVetDto(Vet vet);

Collection<VetDto> toVetDtos(Collection<Vet> vets);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.springframework.samples.petclinic.mapper;

import org.mapstruct.Mapper;
import org.springframework.samples.petclinic.dto.VisitDto;
import org.springframework.samples.petclinic.model.Visit;

import java.util.Collection;

/**
* Map Visit & VisitDto using mapstruct
*/
@Mapper(uses = PetMapper.class)
public interface VisitMapper {
Visit toVisit(VisitDto visitDto);

VisitDto toVisitDto(Visit visit);

Collection<VisitDto> toVisitsDto(Collection<Visit> visits);

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,15 @@
*/
package org.springframework.samples.petclinic.model;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.validation.constraints.Digits;
import javax.validation.constraints.NotEmpty;

import org.springframework.beans.support.MutableSortDefinition;
import org.springframework.beans.support.PropertyComparator;
import org.springframework.core.style.ToStringCreator;
import org.springframework.samples.petclinic.rest.JacksonCustomOwnerDeserializer;
import org.springframework.samples.petclinic.rest.JacksonCustomOwnerSerializer;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import javax.persistence.*;
import javax.validation.constraints.Digits;
import javax.validation.constraints.NotEmpty;
import java.util.*;


/**
* Simple JavaBean domain object representing an owner.
Expand All @@ -50,8 +35,6 @@
*/
@Entity
@Table(name = "owners")
@JsonSerialize(using = JacksonCustomOwnerSerializer.class)
arey marked this conversation as resolved.
Show resolved Hide resolved
@JsonDeserialize(using = JacksonCustomOwnerDeserializer.class)
public class Owner extends Person {
@Column(name = "address")
@NotEmpty
Expand Down Expand Up @@ -93,7 +76,7 @@ public String getTelephone() {
public void setTelephone(String telephone) {
this.telephone = telephone;
}
@JsonIgnore

protected Set<Pet> getPetsInternal() {
if (this.pets == null) {
this.pets = new HashSet<>();
Expand All @@ -111,6 +94,10 @@ public List<Pet> getPets() {
return Collections.unmodifiableList(sortedPets);
}

public void setPets(List<Pet> pets) {
this.pets = new HashSet<>(pets);
}

public void addPet(Pet pet) {
getPetsInternal().add(pet);
pet.setOwner(this);
Expand Down
Loading