Skip to content

Commit

Permalink
Update the generated code according to the current status of the repo
Browse files Browse the repository at this point in the history
Scripted via: `make regenerate-samples`

Co-authored-by: Filipe Pereira <fpereira@yelp.com>
  • Loading branch information
macisamuele and filipemp committed Jan 25, 2020
1 parent e835456 commit dcf4e3d
Show file tree
Hide file tree
Showing 41 changed files with 536 additions and 1,447 deletions.
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
.PHONY: install-hooks
.PHONY: install-hooks regenerate-samples

.git/hooks/pre-commit: venv
${CURDIR}/venv/bin/pre-commit install --install-hooks

install-hooks: .git/hooks/pre-commit
@true

regenerate-samples:
${CURDIR}/gradlew plugin:build
${CURDIR}/gradlew plugin:publishToMavenLocal
${CURDIR}/gradlew generateSwagger

venv:
virtualenv venv
./venv/bin/pip install pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,27 @@

package com.yelp.codegen.generatecodesamples.apis

import okhttp3.RequestBody

import com.yelp.codegen.generatecodesamples.models.Pet
import io.reactivex.Single
import retrofit2.http.GET
import retrofit2.http.Headers

@JvmSuppressWildcards
interface DefaultApi {
/**
* Find pet by ID
* Returns a single pet
* The endpoint is owned by generatecodesamples service owner
* @param petId ID of pet to return (required)
*/
@Headers(
"X-Operation-ID: getPetById"
)
@GET("/pet/{petId}")
fun getPetById(
@retrofit2.http.Path("petId") petId: Long
): Single<Pet>
/**
* Find pet by ID
* Returns a single pet
* The endpoint is owned by generatecodesamples service owner
* @param petId ID of pet to return (required)
*/
@Headers(
"X-Operation-ID: getPetById"
)
@GET("/pet/{petId}")
fun getPetById(
@retrofit2.http.Path("petId") petId: Long
): Single<Pet>

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@
package com.yelp.codegen.generatecodesamples.models

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

/**
* A Category used to group pets
* @property id Unique ID of the Category
* @property name Name of this category
*/
data class Category(
@Json(name = "id") @field:Json(name = "id") var id: Long? = null,
@Json(name = "name") @field:Json(name = "name") var name: String? = null
@JsonClass(generateAdapter = true)
data class Category (
@Json(name = "id") @field:Json(name = "id") var id: Long? = null,
@Json(name = "name") @field:Json(name = "name") var name: String? = null
)

Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package com.yelp.codegen.generatecodesamples.models

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

/**
* Represents a specific Pet in the store
Expand All @@ -16,10 +17,12 @@ import com.squareup.moshi.Json
* @property photoUrls Photo URls for this Pet on the bucket
* @property tags Pet status in the store
*/
data class Pet(
@Json(name = "name") @field:Json(name = "name") var name: String,
@Json(name = "photoUrls") @field:Json(name = "photoUrls") var photoUrls: List<String>,
@Json(name = "category") @field:Json(name = "category") var category: Category? = null,
@Json(name = "id") @field:Json(name = "id") var id: Long? = null,
@Json(name = "tags") @field:Json(name = "tags") var tags: List<Tag>? = null
@JsonClass(generateAdapter = true)
data class Pet (
@Json(name = "name") @field:Json(name = "name") var name: String,
@Json(name = "photoUrls") @field:Json(name = "photoUrls") var photoUrls: List<String>,
@Json(name = "category") @field:Json(name = "category") var category: Category? = null,
@Json(name = "id") @field:Json(name = "id") var id: Long? = null,
@Json(name = "tags") @field:Json(name = "tags") var tags: List<Tag>? = null
)

Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@
package com.yelp.codegen.generatecodesamples.models

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

/**
* A Tag used to group entities
* @property id Unique ID of the Tag
* @property name Name of this Tag
*/
data class Tag(
@Json(name = "id") @field:Json(name = "id") var id: Long? = null,
@Json(name = "name") @field:Json(name = "name") var name: String? = null
@JsonClass(generateAdapter = true)
data class Tag (
@Json(name = "id") @field:Json(name = "id") var id: Long? = null,
@Json(name = "name") @field:Json(name = "name") var name: String? = null
)

Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@ package com.yelp.codegen.generatecodesamples.tools
@Retention(AnnotationRetention.RUNTIME)
annotation class CSV


@Target(AnnotationTarget.FIELD, AnnotationTarget.VALUE_PARAMETER)
@Retention(AnnotationRetention.RUNTIME)
annotation class SSV


@Target(AnnotationTarget.FIELD, AnnotationTarget.VALUE_PARAMETER)
@Retention(AnnotationRetention.RUNTIME)
annotation class TSV


@Target(AnnotationTarget.FIELD, AnnotationTarget.VALUE_PARAMETER)
@Retention(AnnotationRetention.RUNTIME)
annotation class PIPES
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ internal class EnumToValueConverterFactory : Converter.Factory() {
internal class EnumToValueConverter : Converter<Any, String> {
override fun convert(enum: Any): String? {
val enumName = (enum as Enum<*>).name
val jsonAnnotation: Json? = enum.javaClass.getField(enumName).getAnnotation(Json::class.java)
val jsonAnnotation : Json? = enum.javaClass.getField(enumName).getAnnotation(Json::class.java)

// Checking if the Enum is annotated with @Json to get the name.
// If not, fallback to enum default (.toString())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
import retrofit2.Converter
import retrofit2.converter.moshi.MoshiConverterFactory


object GeneratedCodeConverters {
private val moshi = Moshi.Builder()
.add(XNullableAdapterFactory())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class TypesAdapterFactory : JsonAdapter.Factory {
*/
internal abstract class XNullableJsonAdapter<T> : JsonAdapter<T>() {

abstract fun fromNonNullString(nextString: String): T
abstract fun fromNonNullString(nextString: String) : T

override fun fromJson(reader: JsonReader): T? {
return if (reader.peek() != JsonReader.Token.NULL) {
Expand All @@ -57,7 +57,7 @@ internal abstract class XNullableJsonAdapter<T> : JsonAdapter<T>() {
internal class LocalDateAdapter : XNullableJsonAdapter<LocalDate>() {
private val formatter = DateTimeFormatter.ISO_LOCAL_DATE

override fun fromNonNullString(nextString: String): LocalDate = LocalDate.parse(nextString, formatter)
override fun fromNonNullString(nextString: String) : LocalDate = LocalDate.parse(nextString, formatter)

override fun toJson(writer: JsonWriter, value: LocalDate?) {
value?.let { writer.value(it.format(formatter)) }
Expand All @@ -70,7 +70,7 @@ internal class ZonedDateTimeAdapter : XNullableJsonAdapter<ZonedDateTime>() {
override fun fromNonNullString(nextString: String): ZonedDateTime {
return try {
ZonedDateTime.parse(nextString, formatter)
} catch (parseException: DateTimeException) {
} catch (parseException : DateTimeException) {
val localDateTime = LocalDateTime.parse(nextString, formatter)
localDateTime.atZone(ZoneId.of("Z"))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,26 @@ import java.lang.reflect.Type

internal class WrapperConverterFactory(private vararg val factories: Converter.Factory) : Converter.Factory() {

override fun responseBodyConverter(
type: Type,
annotations: Array<Annotation>,
retrofit: Retrofit
): Converter<ResponseBody, *>? {
override fun responseBodyConverter(type: Type,
annotations: Array<Annotation>,
retrofit: Retrofit): Converter<ResponseBody, *>? {
return factories.mapFirstNonNull {
it.responseBodyConverter(type, annotations, retrofit)
}
}

override fun requestBodyConverter(
type: Type,
parameterAnnotations: Array<Annotation>,
methodAnnotations: Array<Annotation>,
retrofit: Retrofit
): Converter<*, RequestBody>? {
override fun requestBodyConverter(type: Type,
parameterAnnotations: Array<Annotation>,
methodAnnotations: Array<Annotation>,
retrofit: Retrofit): Converter<*, RequestBody>? {
return factories.mapFirstNonNull {
it.requestBodyConverter(type, parameterAnnotations, methodAnnotations, retrofit)
}
}

override fun stringConverter(
type: Type,
annotations: Array<Annotation>,
retrofit: Retrofit
): Converter<*, String>? {
override fun stringConverter(type: Type,
annotations: Array<Annotation>,
retrofit: Retrofit): Converter<*, String>? {
return factories.mapFirstNonNull {
it.stringConverter(type, annotations, retrofit)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,13 @@ class XNullableAdapterFactory : JsonAdapter.Factory {
nextAdapter?.toJson(writer, value)
}
}

}
}
return null
}

private fun Set<Annotation>.removeXNullableAnnotation(): MutableSet<out Annotation> {
private fun Set<Annotation>.removeXNullableAnnotation():MutableSet<out Annotation>{
return this.filter { it !is XNullable }.toMutableSet()
}
}
105 changes: 1 addition & 104 deletions samples/generated-code/src/main/java/swagger.json
Original file line number Diff line number Diff line change
@@ -1,104 +1 @@
{
"definitions": {
"Category": {
"description": "A Category used to group pets",
"properties": {
"id": {
"description": "Unique ID of the Category",
"format": "int64",
"type": "integer"
},
"name": {
"description": "Name of this category",
"type": "string"
}
},
"type": "object"
},
"Pet": {
"description": "Represents a specific Pet in the store",
"properties": {
"category": {
"$ref": "#/definitions/Category",
"description": "Optional category of the pet"
},
"id": {
"description": "Unique ID of this Pet",
"format": "int64",
"type": "integer"
},
"name": {
"description": "Name of this specific pet",
"type": "string"
},
"photoUrls": {
"description": "Photo URls for this Pet on the bucket",
"items": {
"type": "string"
},
"type": "array"
},
"tags": {
"description": "Pet status in the store",
"items": {
"$ref": "#/definitions/Tag"
},
"type": "array"
}
},
"required": [
"name",
"photoUrls"
],
"type": "object"
},
"Tag": {
"description": "A Tag used to group entities",
"properties": {
"id": {
"description": "Unique ID of the Tag",
"format": "int64",
"type": "integer"
},
"name": {
"description": "Name of this Tag",
"type": "string"
}
},
"type": "object"
}
},
"info": {
"description": "This is a simplified version of the sample server Petstore server.",
"title": "Swagger Petstore",
"version": "1.0.0"
},
"paths": {
"/pet/{petId}": {
"get": {
"description": "Returns a single pet",
"operationId": "getPetById",
"parameters": [
{
"description": "ID of pet to return",
"format": "int64",
"in": "path",
"name": "petId",
"required": true,
"type": "integer"
}
],
"responses": {
"200": {
"description": "successful operation",
"schema": {
"$ref": "#/definitions/Pet"
}
}
},
"summary": "Find pet by ID"
}
}
},
"swagger": "2.0"
}
{"swagger":"2.0","info":{"description":"This is a simplified version of the sample server Petstore server.","version":"1.0.0","title":"Swagger Petstore"},"paths":{"/pet/{petId}":{"get":{"summary":"Find pet by ID","description":"Returns a single pet","operationId":"getPetById","parameters":[{"name":"petId","in":"path","description":"ID of pet to return","required":true,"type":"integer","format":"int64"}],"responses":{"200":{"description":"successful operation","schema":{"$ref":"#/definitions/Pet"}}}}}},"definitions":{"Category":{"type":"object","properties":{"id":{"type":"integer","format":"int64","description":"Unique ID of the Category"},"name":{"type":"string","description":"Name of this category"}},"description":"A Category used to group pets"},"Pet":{"type":"object","required":["name","photoUrls"],"properties":{"category":{"description":"Optional category of the pet","$ref":"#/definitions/Category"},"id":{"type":"integer","format":"int64","description":"Unique ID of this Pet"},"name":{"type":"string","description":"Name of this specific pet"},"photoUrls":{"type":"array","description":"Photo URls for this Pet on the bucket","items":{"type":"string"}},"tags":{"type":"array","description":"Pet status in the store","items":{"$ref":"#/definitions/Tag"}}},"description":"Represents a specific Pet in the store"},"Tag":{"type":"object","properties":{"id":{"type":"integer","format":"int64","description":"Unique ID of the Tag"},"name":{"type":"string","description":"Name of this Tag"}},"description":"A Tag used to group entities"}}}
Loading

0 comments on commit dcf4e3d

Please sign in to comment.