build
ディレクトリ)は.gitignore対象とするため、リポジトリの管理対象外となるgen-src/main/java
とする
docker run --rm \
-v "${PWD}/build/openapi:/out" \
openapitools/openapi-generator-cli:v6.0.1 generate \
--generator-name spring \
--input-spec https://raw.githubusercontent.com/openapitools/openapi-generator/master/modules/openapi-generator/src/test/resources/3_0/petstore.yaml \
--output /out \
--additional-properties hideGenerationTimestamp=true \
--additional-properties sourceFolder=gen-src/main/java \
--additional-properties basePackage=com.sliber_kugel.openapi \
--additional-properties apiPackage=com.sliber_kugel.openapi.adapter.controller \
--additional-properties modelPackage=com.sliber_kugel.openapi.domain.model \
--additional-properties skipDefaultInterface=true \
--additional-properties oas3=true \
--additional-properties dateLibrary=java8 \
--additional-properties delegatePattern=false \
--additional-properties interfaceOnly=true \
--additional-properties openApiNullable=false \
--additional-properties useTags=true \
--additional-properties disallowAdditionalPropertiesIfNotPresent=false \
--additional-properties useBeanValidation=true \
--additional-properties serializableModel=true
jakarta.validation-api
については次のIssueが報告されており、最新のv3系の場合はコンパイルが出来ないためv2系の最新として2.0.2としている。// OpenAPI Generatorによって自動生成されるディレクトリをコンパイル対象として追加
sourceSets {
main {
java {
srcDirs = ['src/main/java', "${buildDir}/openapi/gen-src/main/java"]
}
}
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
// 以下2つを追加
implementation 'io.swagger.core.v3:swagger-annotations:2.2.2'
implementation 'jakarta.validation:jakarta.validation-api:2.0.2'
}
implements
する
@RestController
アノテーションを付与する
package com.sliber_kugel.openapi.adapter.controller;
import com.sliber_kugel.openapi.domain.model.ModelApiResponse;
import com.sliber_kugel.openapi.domain.model.Pet;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
@RestController
public class PetController implements PetApi {
@Override
public ResponseEntity<Pet> addPet(Pet pet) {
// some implementation
throw new UnsupportedOperationException();
}
@Override
public ResponseEntity<Void> deletePet(Long petId, String apiKey) {
// some implementation
throw new UnsupportedOperationException();
}
@Override
public ResponseEntity<List<Pet>> findPetsByStatus(List<String> status) {
// some implementation
throw new UnsupportedOperationException();
}
@Override
public ResponseEntity<List<Pet>> findPetsByTags(List<String> tags) {
// some implementation
throw new UnsupportedOperationException();
}
@Override
public ResponseEntity<Pet> getPetById(Long petId) {
// some implementation
throw new UnsupportedOperationException();
}
@Override
public ResponseEntity<Pet> updatePet(Pet pet) {
// some implementation
throw new UnsupportedOperationException();
}
@Override
public ResponseEntity<Void> updatePetWithForm(Long petId, String name, String status) {
// some implementation
throw new UnsupportedOperationException();
}
@Override
public ResponseEntity<ModelApiResponse> uploadFile(Long petId, String additionalMetadata, MultipartFile file) {
// some implementation
throw new UnsupportedOperationException();
}
}