Skip to content

Commit

Permalink
[Kotlin][Server] Skip new post processing routine for kotlin server j…
Browse files Browse the repository at this point in the history
…axrs (OpenAPITools#18180)

* Skip post processing when generating JAXRS spec with Kotlin
This feature was previously added to work with the javalin5 library

* Add sample that proves what we expect after fixing this

* Update sample

* Added new samples to samples workflow

* Revert from jdk17 pipeline
  • Loading branch information
zapodot committed Mar 21, 2024
1 parent 3a6849b commit dd29cc0
Show file tree
Hide file tree
Showing 14 changed files with 330 additions and 7 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/samples-kotlin-server.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ on:
branches:
- samples/server/others/kotlin-server/jaxrs-spec/**
- 'samples/server/petstore/kotlin*/**'
- 'samples/server/others/kotlin-server/jaxrs-spec-array-response/**'
# comment out due to gradle build failure
#- samples/server/petstore/kotlin-spring-default/**
pull_request:
paths:
- samples/server/others/kotlin-server/jaxrs-spec/**
- 'samples/server/petstore/kotlin*/**'
- 'samples/server/others/kotlin-server/jaxrs-spec-array-response/**'
# comment out due to gradle build failure
# - samples/server/petstore/kotlin-spring-default/**

Expand Down Expand Up @@ -40,6 +42,7 @@ jobs:
- samples/server/petstore/kotlin-server-modelMutable
- samples/server/petstore/kotlin-server/javalin
- samples/server/others/kotlin-server/jaxrs-spec
- samples/server/others/kotlin-server/jaxrs-spec-array-response
# comment out due to gradle build failure
#- samples/server/petstore/kotlin-spring-default
# no build.gradle file
Expand Down
8 changes: 8 additions & 0 deletions bin/configs/kotlin-server-jaxrs-spec-array-response.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
generatorName: kotlin-server
outputDir: samples/server/others/kotlin-server/jaxrs-spec-array-response
library: jaxrs-spec
inputSpec: modules/openapi-generator/src/test/resources/3_0/kotlin/issue18177-array.yaml
templateDir: modules/openapi-generator/src/main/resources/kotlin-server
additionalProperties:
interfaceOnly: "true"
useJakartaEe: true
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.List;
import java.util.Map;
import java.util.*;

public class KotlinServerCodegen extends AbstractKotlinCodegen implements BeanValidationFeatures {

Expand Down Expand Up @@ -412,7 +409,8 @@ public void postProcess() {
@Override
public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<ModelMap> allModels) {
OperationMap operations = objs.getOperations();
if (operations != null) {
// The following processing breaks the JAX-RS spec, so we only do this for the other libs.
if (operations != null && !Objects.equals(library, Constants.JAXRS_SPEC)) {
List<CodegenOperation> ops = operations.getOperation();
ops.forEach(operation -> {
List<CodegenResponse> responses = operation.responses;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
import java.nio.file.Paths;

import static org.openapitools.codegen.CodegenConstants.LIBRARY;
import static org.openapitools.codegen.languages.AbstractKotlinCodegen.USE_JAKARTA_EE;
import static org.openapitools.codegen.languages.KotlinServerCodegen.Constants.JAXRS_SPEC;
import static org.openapitools.codegen.TestUtils.assertFileContains;
import static org.openapitools.codegen.TestUtils.assertFileNotContains;
import static org.openapitools.codegen.languages.AbstractKotlinCodegen.USE_JAKARTA_EE;
import static org.openapitools.codegen.languages.KotlinServerCodegen.Constants.JAXRS_SPEC;
import static org.openapitools.codegen.languages.KotlinServerCodegen.Constants.INTERFACE_ONLY;
import static org.openapitools.codegen.languages.features.BeanValidationFeatures.USE_BEANVALIDATION;

public class KotlinServerCodegenTest {
Expand Down Expand Up @@ -166,6 +167,38 @@ public void beanValidationJakartaEeImports() throws IOException {
);
}

@Test
public void issue18177Arrays() throws IOException {
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
output.deleteOnExit();

KotlinServerCodegen codegen = new KotlinServerCodegen();
codegen.setOutputDir(output.getAbsolutePath());
codegen.setUseBeanValidation(true);
codegen.additionalProperties().put(INTERFACE_ONLY, true);
codegen.additionalProperties().put(USE_JAKARTA_EE, true);
codegen.additionalProperties().put(LIBRARY, JAXRS_SPEC);
new DefaultGenerator().opts(new ClientOptInput()
.openAPI(TestUtils.parseSpec("src/test/resources/3_0/kotlin/issue18177-array.yaml"))
.config(codegen))
.generate();

String outputPath = output.getAbsolutePath() + "/src/main/kotlin/org/openapitools/server";
Path stuffApi = Paths.get(outputPath + "/apis/StuffApi.kt");
assertFileContains(
stuffApi,
"fun findStuff(): kotlin.collections.List<Stuff>"
);
assertFileNotContains(
stuffApi,
"fun findStuff(): Stuff"
);
assertFileContains(
stuffApi,
"fun findUniqueStuff(): kotlin.collections.Set<Stuff>"
);
}

@Test
public void beanValidationWithEmail() throws IOException {
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
openapi: 3.0.0
servers:
- url: 'https://example.org/v1'
info:
description: >-
Example created
version: 1.0.0
title: OpenAPI Stuff API created to reproduce issue
license:
name: Apache-2.0
url: 'https://www.apache.org/licenses/LICENSE-2.0.html'
tags:
- name: stuff
description: All about the stuff
security:
- bearerAuth: []
paths:
/stuff:
get:
tags:
- stuff
summary: Finds stuff
description: Finds stuff
operationId: findStuff
responses:
'200':
description: successful operation
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/Stuff'
'400':
description: Invalid status value
/uniquestuff:
get:
tags:
- stuff
summary: Finds unique stuff
description: Finds unique stuff
operationId: find unique Stuff
responses:
'200':
description: successful operation
content:
application/json:
schema:
type: array
uniqueItems: true
items:
$ref: '#/components/schemas/Stuff'
'400':
description: Invalid status value
externalDocs:
description: Find out more about Swagger
url: 'http://swagger.io'
components:
securitySchemes:
bearerAuth:
type: http
scheme: bearer
bearerFormat: JWT
schemas:
Stuff:
type: object
properties:
id:
type: integer
format: int64
name:
type: string
tag:
type: string
required:
- name

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# OpenAPI Generator Ignore
# Generated by openapi-generator https://github.com/openapitools/openapi-generator

# Use this file to prevent files from being overwritten by the generator.
# The patterns follow closely to .gitignore or .dockerignore.

# As an example, the C# client generator defines ApiClient.cs.
# You can make changes and tell OpenAPI Generator to ignore just this file by uncommenting the following line:
#ApiClient.cs

# You can match any string of characters against a directory, file or extension with a single asterisk (*):
#foo/*/qux
# The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux

# You can recursively match patterns against a directory, file or extension with a double asterisk (**):
#foo/**/qux
# This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux

# You can also negate patterns with an exclamation (!).
# For example, you can ignore all files in a docs folder with the file extension .md:
#docs/*.md
# Then explicitly reverse the ignore rule for a single file:
#!docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
README.md
build.gradle
gradle.properties
settings.gradle
src/main/kotlin/org/openapitools/server/apis/StuffApi.kt
src/main/kotlin/org/openapitools/server/models/Stuff.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
7.5.0-SNAPSHOT
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# org.openapitools.server - Kotlin Server library for OpenAPI Stuff API created to reproduce issue

## Requires

* Kotlin 1.4.31
* Gradle 6.8.2

## Build

First, create the gradle wrapper script:

```
gradle wrapper
```

Then, run:

```
./gradlew check assemble
```

This runs all tests and packages the library.

## Features/Implementation Notes

* Supports JSON inputs/outputs, File inputs, and Form inputs.
* Supports collection formats for query parameters: csv, tsv, ssv, pipes.
* Some Kotlin and Java types are fully qualified to avoid conflicts with types defined in OpenAPI definitions.

<a id="documentation-for-api-endpoints"></a>
## Documentation for API Endpoints

All URIs are relative to *https://example.org/v1*

Class | Method | HTTP request | Description
------------ | ------------- | ------------- | -------------
*StuffApi* | [**findStuff**](docs/StuffApi.md#findstuff) | **GET** /stuff | Finds stuff
*StuffApi* | [**findUniqueStuff**](docs/StuffApi.md#finduniquestuff) | **GET** /uniquestuff | Finds unique stuff


<a id="documentation-for-models"></a>
## Documentation for Models

- [org.openapitools.server.models.Stuff](docs/Stuff.md)


<a id="documentation-for-authorization"></a>
## Documentation for Authorization


Authentication schemes defined for the API:
<a id="bearerAuth"></a>
### bearerAuth

- **Type**: HTTP Bearer Token authentication (JWT)

Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
group "org.openapitools"
version "1.0.0"

wrapper {
gradleVersion = '6.9'
distributionUrl = "https://services.gradle.org/distributions/gradle-$gradleVersion-all.zip"
}

buildscript {
ext.kotlin_version = "1.7.20"
ext.swagger_annotations_version = "1.5.3"
ext.jakarta_annotations_version = "2.1.1"
ext.jakarta_ws_rs_version = "3.1.0"
ext.jackson_version = "2.9.9"
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")
testImplementation("junit:junit:4.13.2")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.gradle.caching=true
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rootProject.name = 'kotlin-server'
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.openapitools.server.apis;

import org.openapitools.server.models.Stuff

import jakarta.ws.rs.*
import jakarta.ws.rs.core.Response


import java.io.InputStream



@Path("/")
@jakarta.annotation.Generated(value = arrayOf("org.openapitools.codegen.languages.KotlinServerCodegen"), comments = "Generator version: 7.5.0-SNAPSHOT")
interface StuffApi {

@GET
@Path("/stuff")
@Produces("application/json")
fun findStuff(): kotlin.collections.List<Stuff>

@GET
@Path("/uniquestuff")
@Produces("application/json")
fun findUniqueStuff(): kotlin.collections.Set<Stuff>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* OpenAPI Stuff API created to reproduce issue
* Example created
*
* The version of the OpenAPI document: 1.0.0
*
*
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
* Do not edit the class manually.
*/
package org.openapitools.server.models

import com.fasterxml.jackson.annotation.JsonProperty
/**
*
*
* @param name
* @param id
* @param tag
*/


data class Stuff (


@JsonProperty("name")
val name: kotlin.String,


@JsonProperty("id")
val id: kotlin.Long? = null,


@JsonProperty("tag")
val tag: kotlin.String? = null

)

0 comments on commit dd29cc0

Please sign in to comment.