-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
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
[kotlin-server][jax-rs] Added support for JAX-RS library into Kotlin Server generator #10830
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
5e159be
Added templates for Kotlin JAX-RS server
anttileppa ecfb5a2
Fixed Kotlin Server JAX-RS template directory
anttileppa c2767bf
Added support for Kotlin Server JAX-RS library
anttileppa 7083116
Added support using coroutines with Kotlin server JAX-RS library
anttileppa 643cc0c
Added sample for Kotlin server JAX-RS library
anttileppa 7aefbfd
Added support for returnResponse option into Kotlin server JAX-RS lib…
anttileppa 75fe65d
Fixed issue with optional parameters in Kotlin JAX-RS spec
anttileppa 996cfae
Merge remote-tracking branch 'upstream/master' into kotlin-jaxrs
anttileppa 8956747
Fixed oneOf issue in Kotlin jaxrs-spec generator
anttileppa 6406c6e
Added better documentation to Kotlin Server JAX-RS options
anttileppa 8bb9a56
Updated kotlin-server.md
anttileppa 643019f
Updated kotlin-server jaxrs-spec samples
anttileppa 05c7365
Fixed issue with Kotlin JAX-RS spec and reserved names
anttileppa 083e4e6
Regenerated samples
anttileppa bf7d028
Merge remote-tracking branch 'upstream/master' into kotlin-jaxrs
anttileppa 46117c1
Merge remote-tracking branch 'upstream/master' into kotlin-jaxrs
anttileppa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
generatorName: kotlin-server | ||
outputDir: samples/server/petstore/kotlin-server/jaxrs-spec | ||
library: jaxrs-spec | ||
inputSpec: modules/openapi-generator/src/test/resources/2_0/petstore.yaml | ||
templateDir: modules/openapi-generator/src/main/resources/kotlin-server | ||
additionalProperties: | ||
useCoroutines: "true" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
...main/resources/kotlin-server/libraries/jaxrs-spec/additionalModelTypeAnnotations.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
{{#additionalModelTypeAnnotations}}{{{.}}} | ||
{{/additionalModelTypeAnnotations}} |
31 changes: 31 additions & 0 deletions
31
modules/openapi-generator/src/main/resources/kotlin-server/libraries/jaxrs-spec/api.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package {{package}}; | ||
|
||
{{#imports}}import {{import}} | ||
{{/imports}} | ||
|
||
import javax.ws.rs.* | ||
import javax.ws.rs.core.Response | ||
|
||
{{#useSwaggerAnnotations}} | ||
import io.swagger.annotations.* | ||
{{/useSwaggerAnnotations}} | ||
|
||
import java.io.InputStream | ||
import java.util.Map | ||
import java.util.List | ||
{{#useBeanValidation}}import javax.validation.constraints.* | ||
import javax.validation.Valid{{/useBeanValidation}} | ||
|
||
{{#useSwaggerAnnotations}} | ||
@Api(description = "the {{{baseName}}} API"){{/useSwaggerAnnotations}}{{#hasConsumes}} | ||
@Consumes({ {{#consumes}}"{{{mediaType}}}"{{^-last}}, {{/-last}}{{/consumes}} }){{/hasConsumes}}{{#hasProduces}} | ||
@Produces({ {{#produces}}"{{{mediaType}}}"{{^-last}}, {{/-last}}{{/produces}} }){{/hasProduces}} | ||
@Path("/") | ||
{{>generatedAnnotation}}{{#interfaceOnly}}interface{{/interfaceOnly}}{{^interfaceOnly}}class{{/interfaceOnly}} {{classname}} { | ||
{{#operations}} | ||
{{#operation}} | ||
|
||
{{#interfaceOnly}}{{>apiInterface}}{{/interfaceOnly}}{{^interfaceOnly}}{{>apiMethod}}{{/interfaceOnly}} | ||
{{/operation}} | ||
} | ||
{{/operations}} |
13 changes: 13 additions & 0 deletions
13
...api-generator/src/main/resources/kotlin-server/libraries/jaxrs-spec/apiInterface.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
@{{httpMethod}} | ||
@Path("{{{path}}}"){{#hasConsumes}} | ||
@Consumes({{#consumes}}"{{{mediaType}}}"{{^-last}}, {{/-last}}{{/consumes}}){{/hasConsumes}}{{#hasProduces}} | ||
@Produces({{#produces}}"{{{mediaType}}}"{{^-last}}, {{/-last}}{{/produces}}){{/hasProduces}}{{#useSwaggerAnnotations}} | ||
@ApiOperation(value = "{{{summary}}}", notes = "{{{notes}}}"{{#hasAuthMethods}}, authorizations = { | ||
{{#authMethods}}{{#isOAuth}}@Authorization(value = "{{name}}", scopes = { | ||
{{#scopes}}@AuthorizationScope(scope = "{{scope}}", description = "{{description}}"){{^-last}}, | ||
{{/-last}}{{/scopes}} }){{^-last}},{{/-last}}{{/isOAuth}} | ||
{{^isOAuth}}@Authorization(value = "{{name}}"){{^-last}},{{/-last}} | ||
{{/isOAuth}}{{/authMethods}} }{{/hasAuthMethods}}, tags={ {{#vendorExtensions.x-tags}}"{{tag}}"{{^-last}}, {{/-last}}{{/vendorExtensions.x-tags}} }) | ||
@ApiResponses(value = { {{#responses}} | ||
@ApiResponse(code = {{{code}}}, message = "{{{message}}}", response = {{{baseType}}}.class{{#returnContainer}}, responseContainer = "{{{.}}}"{{/returnContainer}}){{^-last}},{{/-last}}{{/responses}} }){{/useSwaggerAnnotations}} | ||
{{#useCoroutines}}suspend {{/useCoroutines}}fun {{nickname}}({{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{^-last}},{{/-last}}{{/allParams}}){{#returnResponse}}: Response{{/returnResponse}}{{^returnResponse}}{{#returnType}}: {{{returnType}}}{{/returnType}}{{/returnResponse}} |
16 changes: 16 additions & 0 deletions
16
...penapi-generator/src/main/resources/kotlin-server/libraries/jaxrs-spec/apiMethod.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
@{{httpMethod}}{{#subresourceOperation}} | ||
@Path("{{{path}}}"){{/subresourceOperation}}{{#hasConsumes}} | ||
@Consumes({{#consumes}}"{{{mediaType}}}"{{^-last}}, {{/-last}}{{/consumes}}){{/hasConsumes}}{{#hasProduces}} | ||
@Produces({{#produces}}"{{{mediaType}}}"{{^-last}}, {{/-last}}{{/produces}}){{/hasProduces}}{{#useSwaggerAnnotations}} | ||
@ApiOperation(value = "{{{summary}}}", notes = "{{{notes}}}", response = {{{returnBaseType}}}.class{{#returnContainer}}, responseContainer = "{{{.}}}"{{/returnContainer}}{{#hasAuthMethods}}, authorizations = { | ||
{{#authMethods}}{{#isOAuth}}@Authorization(value = "{{name}}", scopes = { | ||
{{#scopes}}@AuthorizationScope(scope = "{{scope}}", description = "{{description}}"){{^-last}}, | ||
{{/-last}}{{/scopes}} }){{^-last}},{{/-last}}{{/isOAuth}} | ||
{{^isOAuth}}@Authorization(value = "{{name}}"){{^-last}},{{/-last}} | ||
{{/isOAuth}}{{/authMethods}} }{{/hasAuthMethods}}, tags={ {{#vendorExtensions.x-tags}}"{{tag}}"{{^-last}}, {{/-last}}{{/vendorExtensions.x-tags}} }) | ||
@ApiResponses(value = { {{#responses}} | ||
@ApiResponse(code = {{{code}}}, message = "{{{message}}}", response = {{{baseType}}}.class{{#containerType}}, responseContainer = "{{{.}}}"{{/containerType}}){{^-last}},{{/-last}}{{/responses}} | ||
}){{/useSwaggerAnnotations}} | ||
{{#useCoroutines}}suspend {{/useCoroutines}}fun {{nickname}}({{#allParams}}{{>queryParams}}{{>pathParams}}{{>cookieParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{^-last}},{{/-last}}{{/allParams}}): Response { | ||
return Response.ok().entity("magic!").build(); | ||
} |
5 changes: 5 additions & 0 deletions
5
...i-generator/src/main/resources/kotlin-server/libraries/jaxrs-spec/beanValidation.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
@Valid | ||
{{#required}} | ||
@NotNull | ||
{{/required}} | ||
{{>beanValidationCore}} |
20 changes: 20 additions & 0 deletions
20
...nerator/src/main/resources/kotlin-server/libraries/jaxrs-spec/beanValidationCore.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{{#pattern}} @Pattern(regexp="{{{.}}}"){{/pattern}}{{! | ||
minLength && maxLength set | ||
}}{{#minLength}}{{#maxLength}} @Size(min={{minLength}},max={{maxLength}}){{/maxLength}}{{/minLength}}{{! | ||
minLength set, maxLength not | ||
}}{{#minLength}}{{^maxLength}} @Size(min={{minLength}}){{/maxLength}}{{/minLength}}{{! | ||
minLength not set, maxLength set | ||
}}{{^minLength}}{{#maxLength}} @Size(max={{.}}){{/maxLength}}{{/minLength}}{{! | ||
@Size: minItems && maxItems set | ||
}}{{#minItems}}{{#maxItems}} @Size(min={{minItems}},max={{maxItems}}){{/maxItems}}{{/minItems}}{{! | ||
@Size: minItems set, maxItems not | ||
}}{{#minItems}}{{^maxItems}} @Size(min={{minItems}}){{/maxItems}}{{/minItems}}{{! | ||
@Size: minItems not set && maxItems set | ||
}}{{^minItems}}{{#maxItems}} @Size(max={{.}}){{/maxItems}}{{/minItems}}{{! | ||
check for integer or long / all others=decimal type with @Decimal* | ||
isInteger set | ||
}}{{#isInteger}}{{#minimum}} @Min({{.}}){{/minimum}}{{#maximum}} @Max({{.}}){{/maximum}}{{/isInteger}}{{! | ||
isLong set | ||
}}{{#isLong}}{{#minimum}} @Min({{.}}L){{/minimum}}{{#maximum}} @Max({{.}}L){{/maximum}}{{/isLong}}{{! | ||
Not Integer, not Long => we have a decimal value! | ||
}}{{^isInteger}}{{^isLong}}{{#minimum}} @DecimalMin({{#exclusiveMinimum}}value={{/exclusiveMinimum}}"{{minimum}}"{{#exclusiveMinimum}},inclusive=false{{/exclusiveMinimum}}){{/minimum}}{{#maximum}} @DecimalMax({{#exclusiveMaximum}}value={{/exclusiveMaximum}}"{{maximum}}"{{#exclusiveMaximum}},inclusive=false{{/exclusiveMaximum}}){{/maximum}}{{/isLong}}{{/isInteger}} |
1 change: 1 addition & 0 deletions
1
...src/main/resources/kotlin-server/libraries/jaxrs-spec/beanValidationHeaderParams.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{{#required}} @NotNull{{/required}}{{>beanValidationCore}} |
1 change: 1 addition & 0 deletions
1
...r/src/main/resources/kotlin-server/libraries/jaxrs-spec/beanValidationPathParams.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{{! PathParam is always required, no @NotNull necessary }}{{>beanValidationCore}} |
1 change: 1 addition & 0 deletions
1
.../src/main/resources/kotlin-server/libraries/jaxrs-spec/beanValidationQueryParams.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{{#required}} @NotNull{{/required}}{{>beanValidationCore}} |
1 change: 1 addition & 0 deletions
1
...enapi-generator/src/main/resources/kotlin-server/libraries/jaxrs-spec/bodyParams.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{{#isBodyParam}}{{#useBeanValidation}}@Valid {{#required}}{{^isNullable}}@NotNull {{/isNullable}}{{/required}}{{/useBeanValidation}} {{paramName}}: {{{dataType}}}{{^required}}?{{/required}}{{/isBodyParam}} |
57 changes: 57 additions & 0 deletions
57
...api-generator/src/main/resources/kotlin-server/libraries/jaxrs-spec/build.gradle.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
group "{{groupId}}" | ||
version "{{artifactVersion}}" | ||
|
||
wrapper { | ||
gradleVersion = "6.9" | ||
distributionUrl = "https://services.gradle.org/distributions/gradle-$gradleVersion-all.zip" | ||
} | ||
|
||
buildscript { | ||
ext.kotlin_version = "1.4.32" | ||
ext.jakarta_ws_rs_version = "2.1.6" | ||
ext.swagger_annotations_version = "1.5.3" | ||
ext.jakarta_annotations_version = "1.3.5" | ||
ext.jackson_version = "2.9.9" | ||
{{#useBeanValidation}} | ||
ext.beanvalidation_version = "2.0.2" | ||
{{/useBeanValidation}} | ||
repositories { | ||
maven { url "https://repo1.maven.org/maven2" } | ||
maven { url "https://plugins.gradle.org/m2/" } | ||
} | ||
dependencies { | ||
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version") | ||
} | ||
} | ||
|
||
apply plugin: "java" | ||
apply plugin: "kotlin" | ||
apply plugin: "application" | ||
|
||
sourceCompatibility = 1.8 | ||
|
||
compileKotlin { | ||
kotlinOptions.jvmTarget = "1.8" | ||
} | ||
|
||
compileTestKotlin { | ||
kotlinOptions.jvmTarget = "1.8" | ||
} | ||
|
||
repositories { | ||
maven { setUrl("https://repo1.maven.org/maven2") } | ||
} | ||
|
||
dependencies { | ||
implementation("org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version") | ||
implementation("ch.qos.logback:logback-classic:1.2.1") | ||
implementation("jakarta.ws.rs:jakarta.ws.rs-api:$jakarta_ws_rs_version") | ||
implementation("jakarta.annotation:jakarta.annotation-api:$jakarta_annotations_version") | ||
implementation("io.swagger:swagger-annotations:$swagger_annotations_version") | ||
implementation("com.fasterxml.jackson.jaxrs:jackson-jaxrs-json-provider:$jackson_version") | ||
implementation("com.fasterxml.jackson.datatype:jackson-datatype-jsr310:$jackson_version") | ||
{{#useBeanValidation}} | ||
implementation("jakarta.validation:jakarta.validation-api:$beanvalidation_version") | ||
{{/useBeanValidation}} | ||
testImplementation("junit:junit:4.13.2") | ||
} |
1 change: 1 addition & 0 deletions
1
...api-generator/src/main/resources/kotlin-server/libraries/jaxrs-spec/cookieParams.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{{#isCookieParam}}@CookieParam("{{baseName}}"){{#useBeanValidation}}{{>beanValidationQueryParams}}{{/useBeanValidation}}{{^isContainer}}{{#defaultValue}} @DefaultValue("{{{.}}}"){{/defaultValue}}{{/isContainer}} {{#useSwaggerAnnotations}}{{#description}} @ApiParam("{{.}}"){{/description}}{{/useSwaggerAnnotations}} {{paramName}}: {{{dataType}}}{{^required}}?{{/required}}{{/isCookieParam}} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey,
First of all, big thanks @anttileppa and @wing328 , this PR came very handy for me this week, I appreciate your work on it 👏
Secondly, I realise that this PR is closed and merged, but I thought I'd ask here before I report the issue, because perhaps it's my understanding that's the issue here, not the code :-)
I'm trying to generate the code for the API that's hosted under
api
. For this reason I defined:in my definition and based on my experience with other generators I'd expect it to work as a "prefix" for all REST resources. Unfortunately what's generated by the
jaxrs-spec
library is:(not surprising given what's in this line of the template 😉 )
meaning that the e.g. resource
xyz
would exist under/xyz
rather than/api/xyz
.Conversely, a Kotlin client generated from the same spec prefixes all the URLs with
/api
- such a "mismatch" between the generated client and the server code logic makes me think that perhaps something is not right.My question is: was "/" intentionally hardcoded here or is this perhaps a mistake? Alternatively, if that top-level "prefix" is not desired for some reason - should it perhaps be used as a prefix for each API method
@Path
?(disclaimer: it's my first contact with JAX-RS, which could be where the problem is 😉 )
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@regispl sounds like a mistake to me