Skip to content

Commit

Permalink
Merge pull request #91 from macisamuele/maci-bump-swagger-codegen-2
Browse files Browse the repository at this point in the history
Bump io.swagger:swagger-codegen from 2.3.1 to 2.4.12
  • Loading branch information
macisamuele committed Feb 4, 2020
2 parents 9de9741 + 26cfafe commit 5ab43f0
Show file tree
Hide file tree
Showing 11 changed files with 465 additions and 499 deletions.
2 changes: 1 addition & 1 deletion plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ dependencies {

implementation("commons-cli:commons-cli:1.4")
implementation("com.google.guava:guava:27.0-jre")
implementation("io.swagger:swagger-codegen:2.3.1")
implementation("io.swagger:swagger-codegen:2.4.12")

testImplementation("junit:junit:4.12")
}
Expand Down
19 changes: 16 additions & 3 deletions plugin/src/main/java/com/yelp/codegen/SharedCodegen.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.yelp.codegen

import com.yelp.codegen.utils.InlineModelResolver
import com.yelp.codegen.utils.safeSuffix
import io.swagger.codegen.CodegenConfig
import io.swagger.codegen.CodegenModel
import io.swagger.codegen.CodegenOperation
import io.swagger.codegen.CodegenProperty
import io.swagger.codegen.CodegenType
import io.swagger.codegen.DefaultCodegen
import io.swagger.codegen.InlineModelResolver
import io.swagger.codegen.SupportingFile
import io.swagger.models.ArrayModel
import io.swagger.models.ComposedModel
Expand Down Expand Up @@ -93,12 +93,26 @@ abstract class SharedCodegen : DefaultCodegen(), CodegenConfig {
override fun preprocessSwagger(swagger: Swagger) {
super.preprocessSwagger(swagger)

// Swagger-Codegen does invoke InlineModelResolver.flatten before starting the API and Models generation
// It is a bit too late to ensure that inline models (with x-model) have the model name honored
// according to the following ordering preference: x-model, title, <whatever codegen generates>
// So we're triggering the process early on as the process is not super slow and more importantly is idempotent
InlineModelResolver().flatten(swagger)

unsafeOperations.addAll(when (val it = swagger.info.vendorExtensions["x-operation-ids-unsafe-to-use"]) {
is List<*> -> it.filterIsInstance<String>()
else -> listOf()
})

mapXModel(swagger)

swagger.definitions?.forEach { (name, model) ->
// Ensure that all the models have a title
// The title should give priority to x-model, then title and finally
// to the name that codegen thought to use
model.title = xModelMatches[name] ?: name
}

this.swagger = swagger
}

Expand All @@ -119,8 +133,7 @@ abstract class SharedCodegen : DefaultCodegen(), CodegenConfig {
* computing models names.
*/
private fun mapXModel(swagger: Swagger) {
InlineModelResolver().flatten(swagger)
swagger.definitions.forEach { name, model ->
swagger.definitions?.forEach { (name, model) ->
(model.vendorExtensions?.get(X_MODEL) as? String?)?.let { x_model ->
xModelMatches[name] = x_model
}
Expand Down
495 changes: 0 additions & 495 deletions plugin/src/main/java/com/yelp/codegen/utils/InlineModelResolver.java

This file was deleted.

104 changes: 104 additions & 0 deletions samples/junit-tests/junit_tests_specs.json
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,110 @@
]
}
},
"/model_name/no_x-model_or_title": {
"get": {
"operationId": "getInlinedModelWithNoNames",
"responses": {
"200": {
"description": "The expected model name is InlineResponse200 (generated by codegen)",
"schema": {
"properties": {
"no_xmodel_no_title": {
"enum": [
"val1",
"val2"
],
"type": "string"
}
},
"type": "object"
}
}
},
"tags": [
"model_name_check"
]
}
},
"/model_name/title_only": {
"get": {
"operationId": "getInlinedModelWithTitleOnly",
"responses": {
"200": {
"description": "The expected model name is ModelWithOnlyTitle (title attribute)",
"schema": {
"properties": {
"title_only": {
"enum": [
"val1",
"val2"
],
"type": "string"
}
},
"title": "ModelWithOnlyTitle",
"type": "object"
}
}
},
"tags": [
"model_name_check"
]
}
},
"/model_name/x-model_and_title": {
"get": {
"operationId": "getInlinedModelWithXModelAndTitle",
"responses": {
"200": {
"description": "The expected model name is ModelWithXModelAndTitle (x-model attribute)",
"schema": {
"properties": {
"xmodel_and_title": {
"enum": [
"val1",
"val2"
],
"type": "string"
}
},
"title": "ThisShouldBeIgnored",
"type": "object",
"x-model": "ModelWithXModelAndTitle"
}
}
},
"tags": [
"model_name_check"
]
}
},
"/model_name/x-model_only": {
"get": {
"operationId": "getInlinedModelWithXModelOnly",
"responses": {
"200": {
"description": "The expected model name is ModelWithOnlyXModel (x-model attribute)",
"schema": {
"properties": {
"xmodel_only": {
"enum": [
"val1",
"val2"
],
"type": "string"
}
},
"type": "object",
"x-model": "ModelWithOnlyXModel"
}
}
},
"tags": [
"model_name_check"
]
}
},
"/nested_additional_properties": {
"get": {
"operationId": "get_nested_additional_properties",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* NOTE: This class is auto generated by the Swagger Gradle Codegen for the following API: JUnit Tests
*
* More info on this tool is available on https://github.com/Yelp/swagger-gradle-codegen
*/

package com.yelp.codegen.generatecodesamples.apis

import com.yelp.codegen.generatecodesamples.models.InlineResponse200
import com.yelp.codegen.generatecodesamples.models.ModelWithOnlyTitle
import com.yelp.codegen.generatecodesamples.models.ModelWithOnlyXModel
import com.yelp.codegen.generatecodesamples.models.ModelWithXModelAndTitle
import io.reactivex.Single
import retrofit2.http.GET
import retrofit2.http.Headers

@JvmSuppressWildcards
interface ModelNameCheckApi {
/**
* The endpoint is owned by junittests service owner
*/
@Headers(
"X-Operation-ID: getInlinedModelWithNoNames"
)
@GET("/model_name/no_x-model_or_title")
fun getInlinedModelWithNoNames(): Single<InlineResponse200>

/**
* The endpoint is owned by junittests service owner
*/
@Headers(
"X-Operation-ID: getInlinedModelWithTitleOnly"
)
@GET("/model_name/title_only")
fun getInlinedModelWithTitleOnly(): Single<ModelWithOnlyTitle>

/**
* The endpoint is owned by junittests service owner
*/
@Headers(
"X-Operation-ID: getInlinedModelWithXModelAndTitle"
)
@GET("/model_name/x-model_and_title")
fun getInlinedModelWithXModelAndTitle(): Single<ModelWithXModelAndTitle>

/**
* The endpoint is owned by junittests service owner
*/
@Headers(
"X-Operation-ID: getInlinedModelWithXModelOnly"
)
@GET("/model_name/x-model_only")
fun getInlinedModelWithXModelOnly(): Single<ModelWithOnlyXModel>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* NOTE: This class is auto generated by the Swagger Gradle Codegen for the following API: JUnit Tests
*
* More info on this tool is available on https://github.com/Yelp/swagger-gradle-codegen
*/

package com.yelp.codegen.generatecodesamples.models

import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass

/**
*
* @property noXmodelNoTitle
*/
@JsonClass(generateAdapter = true)
data class InlineResponse200(
@Json(name = "no_xmodel_no_title") @field:Json(name = "no_xmodel_no_title") var noXmodelNoTitle: InlineResponse200.NoXmodelNoTitleEnum? = null
) {
/**
*
* Values: VAL1, VAL2
*/
@JsonClass(generateAdapter = false)
enum class NoXmodelNoTitleEnum(val value: String) {
@Json(name = "val1") VAL1("val1"),
@Json(name = "val2") VAL2("val2")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* NOTE: This class is auto generated by the Swagger Gradle Codegen for the following API: JUnit Tests
*
* More info on this tool is available on https://github.com/Yelp/swagger-gradle-codegen
*/

package com.yelp.codegen.generatecodesamples.models

import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass

/**
*
* @property titleOnly
*/
@JsonClass(generateAdapter = true)
data class ModelWithOnlyTitle(
@Json(name = "title_only") @field:Json(name = "title_only") var titleOnly: ModelWithOnlyTitle.TitleOnlyEnum? = null
) {
/**
*
* Values: VAL1, VAL2
*/
@JsonClass(generateAdapter = false)
enum class TitleOnlyEnum(val value: String) {
@Json(name = "val1") VAL1("val1"),
@Json(name = "val2") VAL2("val2")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* NOTE: This class is auto generated by the Swagger Gradle Codegen for the following API: JUnit Tests
*
* More info on this tool is available on https://github.com/Yelp/swagger-gradle-codegen
*/

package com.yelp.codegen.generatecodesamples.models

import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass

/**
*
* @property xmodelOnly
*/
@JsonClass(generateAdapter = true)
data class ModelWithOnlyXModel(
@Json(name = "xmodel_only") @field:Json(name = "xmodel_only") var xmodelOnly: ModelWithOnlyXModel.XmodelOnlyEnum? = null
) {
/**
*
* Values: VAL1, VAL2
*/
@JsonClass(generateAdapter = false)
enum class XmodelOnlyEnum(val value: String) {
@Json(name = "val1") VAL1("val1"),
@Json(name = "val2") VAL2("val2")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* NOTE: This class is auto generated by the Swagger Gradle Codegen for the following API: JUnit Tests
*
* More info on this tool is available on https://github.com/Yelp/swagger-gradle-codegen
*/

package com.yelp.codegen.generatecodesamples.models

import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass

/**
*
* @property xmodelAndTitle
*/
@JsonClass(generateAdapter = true)
data class ModelWithXModelAndTitle(
@Json(name = "xmodel_and_title") @field:Json(name = "xmodel_and_title") var xmodelAndTitle: ModelWithXModelAndTitle.XmodelAndTitleEnum? = null
) {
/**
*
* Values: VAL1, VAL2
*/
@JsonClass(generateAdapter = false)
enum class XmodelAndTitleEnum(val value: String) {
@Json(name = "val1") VAL1("val1"),
@Json(name = "val2") VAL2("val2")
}
}
Loading

0 comments on commit 5ab43f0

Please sign in to comment.