I've defined an API to register a User. I want to return SaveUserResponse after registeration of user is successful. However, the interface generated after gradle build has return type as Object.
Here is the userapi.yaml
paths:
/signup:
post:
description: User registration API
operationId: signup
requestBody:
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/SignupRequest'
responses:
'201':
description: CREATED
content:
application/json:
schema:
$ref: '#components/schemas/SaveUserResponse'
components:
schemas:
SignupRequest:
type: object
description: User model for GetARoom
required:
- firstName
- lastName
- email
- password
properties:
firstName:
type: string
description: First name of the user
minLength: 2
maxLength: 15
lastName:
type: string
description: Last name of the user
minLength: 1
maxLength: 15
email:
type: string
description: User's email address.
password:
type: string
format: password
description: Password of the user
pattern: /^(?=.*[0-9])(?=.*[a-z]).{8,12}$/
minLength: 8
maxLength: 12
SaveUserResponse:
type: object
properties:
accountNumber:
type: integer
userId:
type: string
The following interface got generated after the build. If you notice (Line 55), the return type of signup is ResponseEntity<Object>
but I want to return ResponseEntity <SaveUserResponse>
. What am I missing?
My build.gradle :
plugins {
id 'java'
id 'org.springframework.boot' version '3.2.0'
id 'io.spring.dependency-management' version '1.0.15.RELEASE'
id 'org.openapi.generator' version '6.6.0'
}
group = 'com.ums'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'
repositories {
mavenCentral()
}
sourceSets {
main {
java {
srcDirs("$buildDir/generated/openapi/src/main/java")
}
}
}
openApiGenerate {
generatorName = "spring"
inputSpec.set("$projectDir/src/main/resources/api/userapi.yaml")
outputDir.set("$buildDir/generated/openapi")
apiPackage.set("com.ums.userservice.api")
modelPackage.set("com.ums.userservice.model")
configOptions = [
library : "spring-boot",
useSpringBoot3: "true"
]
additionalProperties = [
dateLibrary: "java8",
generateModels: true,
generateApis: true,
interfaceOnly: true,
skipDefaultInterface: true,
useBeanValidation: true,
serializableModel: true
]
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation group: 'org.openapitools', name: 'jackson-databind-nullable', version: '0.2.6'
implementation group: 'io.swagger.core.v3', name: 'swagger-annotations', version: '2.2.21'
implementation group: 'io.swagger', name: 'swagger-annotations', version: '1.6.14'
compileOnly group: 'org.projectlombok', name: 'lombok', version: '1.18.24'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
annotationProcessor 'org.projectlombok:lombok:1.18.24'
testCompileOnly 'org.projectlombok:lombok:1.18.24'
testAnnotationProcessor 'org.projectlombok:lombok:1.18.24'
}
tasks.named('test') {
useJUnitPlatform()
}
tasks.withType(JavaCompile) {
dependsOn(tasks.openApiGenerate)
}
Found the issue: It was with the following line
$ref: '#components/schemas/SaveUserResponse'
.
It should be '#/components/schemas/SaveUserResponse'