@openapi-generator-plus/java-retrofit-client-generator
Version:
An OpenAPI Generator Plus template for a Java API client using Retrofit
138 lines (134 loc) • 6.57 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.api = api;
const idx = __importStar(require("@openapi-generator-plus/indexed-type"));
const template_utils_1 = require("@openapi-generator-plus/template-utils");
const java_jaxrs_generator_common_1 = require("@openapi-generator-plus/java-jaxrs-generator-common");
/** One query parameter's Retrofit annotation and declaration, or `''` if `parameter` isn't a query parameter. */
function queryParamDeclaration(parameter, ctx) {
if (!parameter.isQueryParam) {
return '';
}
return `@retrofit2.http.Query("${parameter.name}") ${parameter.nativeType} ${(0, template_utils_1.identifier)(ctx.generatorContext.generator(), parameter.name)}`;
}
/**
* One path parameter's Retrofit annotation and declaration, or `''` if `parameter` isn't a path
* parameter. Uses the parameter's raw name for the Java variable, not an escaped identifier —
* matching the original template exactly.
*/
function pathParamDeclaration(parameter) {
if (!parameter.isPathParam) {
return '';
}
return `@retrofit2.http.Path("${parameter.name}") ${parameter.nativeType} ${parameter.name}`;
}
/** One header parameter's Retrofit annotation and declaration, or `''` if `parameter` isn't a header parameter. */
function headerParamDeclaration(parameter, ctx) {
if (!parameter.isHeaderParam) {
return '';
}
return `@retrofit2.http.Header("${parameter.name}") ${parameter.nativeType} ${(0, template_utils_1.identifier)(ctx.generatorContext.generator(), parameter.name)}`;
}
/**
* One form parameter's Retrofit annotation and declaration, or `''` if `parameter` isn't a form
* parameter. `@FormUrlEncoded` isn't Retrofit's usual per-field form annotation (`@Field` is) —
* preserved as-is, matching the original (untested) template.
*/
function formParamDeclaration(parameter, ctx) {
if (!parameter.isFormParam) {
return '';
}
return `@retrofit2.http.FormUrlEncoded("${parameter.name}") ${parameter.nativeType} ${(0, template_utils_1.identifier)(ctx.generatorContext.generator(), parameter.name)}`;
}
/**
* One parameter's declaration within an operation's method signature. Exactly one of the
* query/path/header/form annotations applies to any given parameter — a cookie parameter
* matches none of them (Retrofit has no cookie-parameter annotation) and renders nothing,
* so it is omitted from the signature entirely.
*/
function parameterDeclaration(parameter, ctx) {
return queryParamDeclaration(parameter, ctx)
|| pathParamDeclaration(parameter)
|| headerParamDeclaration(parameter, ctx)
|| formParamDeclaration(parameter, ctx);
}
/** The operation's full parameter list, as it appears within its method signature's parentheses. */
function operationParameters(operation, ctx) {
var _a;
const parameters = operation.parameters ? idx.allValues(operation.parameters) : [];
const declarations = parameters.map(parameter => parameterDeclaration(parameter, ctx)).filter(declaration => declaration !== '');
if ((_a = operation.requestBody) === null || _a === void 0 ? void 0 : _a.nativeType) {
declarations.push(`@retrofit2.http.Body ${operation.requestBody.nativeType} ${operation.requestBody.name}`);
}
return declarations.join(', ');
}
/**
* One operation's Retrofit method declaration.
*
* The original template also rendered any nested models declared directly on the operation
* (`{{#ifdef schemas}}{{>nestedModels}}{{/ifdef}}`) before the method — but `CodegenOperation`
* has no `schemas` field, so that guard was always false and nothing ever rendered; omitted
* here entirely rather than ported as permanently-dead code.
*/
function operationMethod(operation, ctx) {
const generator = ctx.generatorContext.generator();
const methodAnnotation = operation.path
? `@retrofit2.http.${operation.httpMethod}("${operation.path}")`
: `@retrofit2.http.${operation.httpMethod}`;
const returnType = operation.returnNativeType ? `retrofit2.Call<${operation.returnNativeType}>` : 'retrofit2.Call<Void>';
return (0, template_utils_1.ts) `
${methodAnnotation}
${(0, template_utils_1.when)(operation.deprecated, '@java.lang.Deprecated')}
${returnType} ${(0, template_utils_1.identifier)(generator, operation.name)}(${operationParameters(operation, ctx)});
`;
}
/**
* This client's whole `Api.java` interface for one operation group: one Retrofit-annotated
* method per operation, using Retrofit's own `@retrofit2.http.*` request/parameter annotations
* directly (rather than the family's shared JAX-RS-oriented operation rendering).
*/
function api(group, ctx) {
const generator = ctx.generatorContext.generator();
const name = (0, template_utils_1.className)(generator, group.name);
const operationsText = (0, template_utils_1.each)(group.operations, operation => operationMethod(operation, ctx), '');
return (0, template_utils_1.ts) `
package ${ctx.root.apiPackage};
${(0, java_jaxrs_generator_common_1.imports)(ctx.root)}
${(0, java_jaxrs_generator_common_1.generatedAnnotation)(ctx.root)}
public interface ${name}Api {
${operationsText}}
`;
}