Skip to content

Commit 5aaf77e

Browse files
macisamuelecortinico
authored andcommitted
Add support "unsafe to use" endpoints (#18)
* Ensure propagation of unsafe to use operations (in case the operations is marked as unsafe a @Deprecation warning is set) * Bump pre-commit hooks * Trigger git hooks install only if not previously installed * PR feedbacks + documentation enhancement
1 parent b4515d7 commit 5aaf77e

File tree

5 files changed

+56
-11
lines changed

5 files changed

+56
-11
lines changed

.pre-commit-config.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
repos:
22
- repo: https://github.com/pre-commit/pre-commit-hooks
3-
rev: v2.0.0
3+
rev: v2.1.0
44
hooks:
55
- id: check-merge-conflict
66
- id: trailing-whitespace
@@ -12,7 +12,7 @@ repos:
1212
- id: check-yaml
1313
- id: check-executables-have-shebangs
1414
- repo: https://github.com/macisamuele/language-formatters-pre-commit-hooks
15-
rev: v1.2.2
15+
rev: v1.2.3
1616
hooks:
1717
- id: pretty-format-java
1818
args: [--autofix]
@@ -21,7 +21,7 @@ repos:
2121
- id: pretty-format-yaml
2222
args: [--autofix, --indent, '2']
2323
- repo: https://github.com/Yelp/detect-secrets
24-
rev: v0.11.3
24+
rev: v0.12.0
2525
hooks:
2626
- id: detect-secrets
2727
args: [--baseline, .secrets.baseline]

.secrets.baseline

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
{
2-
"exclude_regex": null,
3-
"generated_at": "2019-01-28T12:41:36Z",
2+
"exclude": {
3+
"files": null,
4+
"lines": null
5+
},
6+
"generated_at": "2019-02-14T20:34:51Z",
47
"plugins_used": [
58
{
69
"name": "AWSKeyDetector"
@@ -32,5 +35,5 @@
3235
}
3336
]
3437
},
35-
"version": "0.11.3"
38+
"version": "0.12.0"
3639
}

Makefile

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
.PHONY: install-hooks
22

3+
.git/hooks/pre-commit: venv
4+
${CURDIR}/venv/bin/pre-commit install --install-hooks
5+
6+
install-hooks: .git/hooks/pre-commit
7+
@true
8+
39
venv:
410
virtualenv venv
511
./venv/bin/pip install pre-commit
6-
7-
install-hooks: venv
8-
./venv/bin/pre-commit install --install-hooks

plugin/src/main/java/com/yelp/codegen/SharedCodegen.kt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import io.swagger.codegen.SupportingFile
1212
import io.swagger.models.ArrayModel
1313
import io.swagger.models.Model
1414
import io.swagger.models.ModelImpl
15+
import io.swagger.models.Operation
1516
import io.swagger.models.Swagger
1617
import io.swagger.models.properties.ArrayProperty
1718
import io.swagger.models.properties.MapProperty
@@ -30,12 +31,14 @@ const val HEADERS_TO_IGNORE = "headers_to_ignore"
3031
internal const val X_NULLABLE = "x-nullable"
3132
internal const val X_MODEL = "x-model"
3233
internal const val X_OPERATION_ID = "x-operation-id"
34+
internal const val X_UNSAFE_OPERATION = "x-unsafe-operation"
3335

3436
abstract class SharedCodegen : DefaultCodegen(), CodegenConfig {
3537

3638
// Reference to the Swagger Specs
3739
protected var swagger: Swagger? = null
3840
private val xModelMatches = mutableMapOf<String, String>()
41+
private val unsafeOperations: MutableList<String> = mutableListOf()
3942

4043
override fun getTag() = CodegenType.CLIENT
4144

@@ -81,6 +84,11 @@ abstract class SharedCodegen : DefaultCodegen(), CodegenConfig {
8184
override fun preprocessSwagger(swagger: Swagger) {
8285
super.preprocessSwagger(swagger)
8386

87+
unsafeOperations.addAll(when (val it = swagger.info.vendorExtensions["x-operation-ids-unsafe-to-use"]) {
88+
is List<*> -> it.filterIsInstance()
89+
else -> listOf()
90+
})
91+
8492
mapXModel(swagger)
8593
this.swagger = swagger
8694
}
@@ -358,6 +366,32 @@ abstract class SharedCodegen : DefaultCodegen(), CodegenConfig {
358366
return codegenModel.dataType
359367
}
360368

369+
/**
370+
* Convert Swagger Operation object to Codegen Operation object
371+
*
372+
* The function takes care of adding additional vendor extensions on the Codegen Operation
373+
* to better support the swagger-gradle-codegen use-case
374+
* 1) X_OPERATION_ID : added as we want to render operation ids on the final API artifacts
375+
* 2) X_UNSAFE_OPERATION : added as we want to mark as deprecated APIs for which we're not sure
376+
* that will work exactly as expected in the generated code
377+
*
378+
* @return the converted codegen operation
379+
*/
380+
override fun fromOperation(
381+
path: String?,
382+
httpMethod: String?,
383+
operation: Operation?,
384+
definitions: MutableMap<String, Model>?,
385+
swagger: Swagger?
386+
): CodegenOperation {
387+
val codegenOperation = super.fromOperation(path, httpMethod, operation, definitions, swagger)
388+
codegenOperation.vendorExtensions[X_OPERATION_ID] = operation?.operationId
389+
if (unsafeOperations.contains(operation?.operationId)) {
390+
codegenOperation.vendorExtensions[X_UNSAFE_OPERATION] = true
391+
}
392+
return codegenOperation
393+
}
394+
361395
/**
362396
* Abstract function to create a type for a JSON Array.
363397
* @param listType A List Type (e.g. `List`, `ArrayList`, etc)

plugin/src/main/resources/kotlin/retrofit2/api.mustache

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,13 @@ interface {{classname}} {
3737
@Headers({{#vendorExtensions.x-operation-id}}{{{newline}}} "X-Operation-ID: {{vendorExtensions.x-operation-id}}"{{/vendorExtensions.x-operation-id}}{{^formParams}}{{#prioritizedContentTypes}}{{#-first}},
3838
"Content-Type:{{{mediaType}}}"{{/-first}}{{/prioritizedContentTypes}}{{/formParams}}
3939
)
40-
@{{httpMethod}}("{{{path}}}"){{#isDeprecated}}
41-
@Deprecated(message = "Deprecated"){{/isDeprecated}}
40+
41+
@{{httpMethod}}("{{{path}}}"){{#vendorExtensions.x-unsafe-operation}}{{#isDeprecated}}
42+
@Deprecated(message = "Deprecated and unsafe to use"){{/isDeprecated}}{{^isDeprecated}}
43+
@Deprecated(message = "Unsafe to use"){{/isDeprecated}}
44+
{{/vendorExtensions.x-unsafe-operation}}{{^vendorExtensions.x-unsafe-operation}}{{#isDeprecated}}
45+
@Deprecated(message = "Deprecated"){{/isDeprecated}}
46+
{{/vendorExtensions.x-unsafe-operation}}
4247
fun {{operationId}}({{^allParams}}){{/allParams}}
4348
{{#allParams}}{{>retrofit2/queryParams}}{{>retrofit2/pathParams}}{{>retrofit2/headerParams}}{{>retrofit2/bodyParams}}{{>retrofit2/formParams}}{{#hasMore}},
4449
{{/hasMore}}{{^hasMore}}

0 commit comments

Comments
 (0)