Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avivlevizky config specify folder for api and model files #16704

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ public interface CodegenConfig {

Map<String, Object> vendorExtensions();

Map<String, String> templateOutputDirs();

String testPackage();

String apiPackage();
Expand Down Expand Up @@ -219,8 +221,12 @@ public interface CodegenConfig {

String modelFilename(String templateName, String modelName);

String modelFilename(String templateName, String modelName, String outputDir);

String apiFilename(String templateName, String tag);

String apiFilename(String templateName, String tag, String outputDir);

String apiTestFilename(String templateName, String tag);

String apiDocFilename(String templateName, String tag);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ apiTemplateFiles are for API outputs only (controllers/handlers).
protected Map<String, Object> additionalProperties = new HashMap<>();
protected Map<String, String> serverVariables = new HashMap<>();
protected Map<String, Object> vendorExtensions = new HashMap<>();
protected Map<String, String> templateOutputDirs = new HashMap<>();
/*
Supporting files are those which aren't models, APIs, or docs.
These get a different map of data bound to the templates. Supporting files are written once.
Expand Down Expand Up @@ -730,9 +731,9 @@ public void setCircularReferences(Map<String, CodegenModel> models) {

// for oneOf
final Map<String, List<CodegenProperty>> oneOfDependencyMap = models.entrySet().stream()
.collect(Collectors.toMap(Entry::getKey, entry -> getModelDependencies(
(entry.getValue().getComposedSchemas() != null && entry.getValue().getComposedSchemas().getOneOf() != null)
? entry.getValue().getComposedSchemas().getOneOf() : new ArrayList<CodegenProperty>())));
.collect(Collectors.toMap(Entry::getKey, entry -> getModelDependencies(
(entry.getValue().getComposedSchemas() != null && entry.getValue().getComposedSchemas().getOneOf() != null)
? entry.getValue().getComposedSchemas().getOneOf() : new ArrayList<CodegenProperty>())));

models.keySet().forEach(name -> setCircularReferencesOnProperties(name, oneOfDependencyMap));
}
Expand Down Expand Up @@ -1351,6 +1352,11 @@ public Map<String, Object> vendorExtensions() {
return vendorExtensions;
}

@Override
public Map<String, String> templateOutputDirs() {
return templateOutputDirs;
}

@Override
public List<SupportingFile> supportingFiles() {
return supportingFiles;
Expand Down Expand Up @@ -4889,7 +4895,7 @@ public CodegenResponse fromResponse(String responseCode, ApiResponse response) {
if (ModelUtils.isEmailSchema(responseSchema)) {
r.isEmail = true;
} else if (ModelUtils.isPasswordSchema(responseSchema)) {
r.isPassword = true;
r.isPassword = true;
} else if (ModelUtils.isUUIDSchema(responseSchema)) {
r.isUuid = true;
} else if (ModelUtils.isByteArraySchema(responseSchema)) {
Expand Down Expand Up @@ -6029,12 +6035,24 @@ public String apiFilename(String templateName, String tag) {
return apiFileFolder() + File.separator + toApiFilename(tag) + suffix;
}

@Override
public String apiFilename(String templateName, String tag, String outputDir) {
String suffix = apiTemplateFiles().get(templateName);
return outputDir + File.separator + toApiFilename(tag) + suffix;
}

@Override
public String modelFilename(String templateName, String modelName) {
String suffix = modelTemplateFiles().get(templateName);
return modelFileFolder() + File.separator + toModelFilename(modelName) + suffix;
}

@Override
public String modelFilename(String templateName, String modelName, String outputDir) {
String suffix = modelTemplateFiles().get(templateName);
return outputDir + File.separator + toModelFilename(modelName) + suffix;
}

/**
* Return the full path and API documentation file
*
Expand Down Expand Up @@ -7348,7 +7366,7 @@ protected void updateRequestBodyForArray(CodegenParameter codegenParameter, Sche
Schema inner = getSchemaItems(arraySchema);
CodegenProperty codegenProperty = fromProperty("property", arraySchema, false);
if (codegenProperty == null) {
throw new RuntimeException("CodegenProperty cannot be null. arraySchema for debugging: " + arraySchema);
throw new RuntimeException("CodegenProperty cannot be null. arraySchema for debugging: " + arraySchema);
}

imports.add(codegenProperty.baseType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -419,8 +419,15 @@ private void generateModelDocumentation(List<File> files, Map<String, Object> mo

private void generateModel(List<File> files, Map<String, Object> models, String modelName) throws IOException {
for (String templateName : config.modelTemplateFiles().keySet()) {
String filename = config.modelFilename(templateName, modelName);
File written = processTemplateToFile(models, templateName, filename, generateModels, CodegenConstants.MODELS);
File written;
if (config.templateOutputDirs().containsKey(templateName)) {
String outputDir = config.getOutputDir() + File.separator + config.templateOutputDirs().get(templateName);
String filename = config.modelFilename(templateName, modelName, outputDir);
written = processTemplateToFile(models, templateName, filename, generateModels, CodegenConstants.MODELS, outputDir);
} else {
String filename = config.modelFilename(templateName, modelName);
written = processTemplateToFile(models, templateName, filename, generateModels, CodegenConstants.MODELS);
}
if (written != null) {
files.add(written);
if (config.isEnablePostProcessFile() && !dryRun) {
Expand Down Expand Up @@ -687,8 +694,15 @@ void generateApis(List<File> files, List<OperationsMap> allOperations, List<Mode
addAuthenticationSwitches(operation);

for (String templateName : config.apiTemplateFiles().keySet()) {
String filename = config.apiFilename(templateName, tag);
File written = processTemplateToFile(operation, templateName, filename, generateApis, CodegenConstants.APIS);
File written;
if (config.templateOutputDirs().containsKey(templateName)) {
String outputDir = config.getOutputDir() + File.separator + config.templateOutputDirs().get(templateName);
String filename = config.apiFilename(templateName, tag, outputDir);
written = processTemplateToFile(operation, templateName, filename, generateApis, CodegenConstants.APIS, outputDir);
} else {
String filename = config.apiFilename(templateName, tag);
written = processTemplateToFile(operation, templateName, filename, generateApis, CodegenConstants.APIS);
}
if (written != null) {
files.add(written);
if (config.isEnablePostProcessFile() && !dryRun) {
Expand Down Expand Up @@ -1061,7 +1075,10 @@ private void processUserDefinedTemplates() {
} else {
templateExt = StringUtils.prependIfMissing(templateExt, ".");
}

String templateOutputFolder = userDefinedTemplate.getFolder();
if (!templateOutputFolder.isEmpty()) {
config.templateOutputDirs().put(templateFile, templateOutputFolder);
}
switch (userDefinedTemplate.getTemplateType()) {
case API:
config.apiTemplateFiles().put(templateFile, templateExt);
Expand Down