From ef1ca9f6df15c9ce1843ca457d394bdfdeee6c2d Mon Sep 17 00:00:00 2001 From: wing328 Date: Thu, 31 Mar 2016 15:46:46 +0800 Subject: [PATCH] update objc codegen to generate api doc --- .../codegen/languages/ObjcClientCodegen.java | 95 +++++++++++++++++++ 1 file changed, 95 insertions(+) diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/ObjcClientCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/ObjcClientCodegen.java index 4ec5fa0693..8c68072d55 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/ObjcClientCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/ObjcClientCodegen.java @@ -36,6 +36,8 @@ public class ObjcClientCodegen extends DefaultCodegen implements CodegenConfig { protected String license = "MIT"; protected String gitRepoURL = "https://github.com/swagger-api/swagger-codegen"; protected String[] specialWords = {"new", "copy"}; + protected String apiDocPath = "docs/"; + protected String modelDocPath = "docs/"; public ObjcClientCodegen() { super(); @@ -46,6 +48,8 @@ public class ObjcClientCodegen extends DefaultCodegen implements CodegenConfig { apiTemplateFiles.put("api-header.mustache", ".h"); apiTemplateFiles.put("api-body.mustache", ".m"); embeddedTemplateDir = templateDir = "objc"; + modelDocTemplateFiles.put("model_doc.mustache", ".md"); + apiDocTemplateFiles.put("api_doc.mustache", ".md"); defaultIncludes.clear(); defaultIncludes.add("bool"); @@ -199,6 +203,10 @@ public class ObjcClientCodegen extends DefaultCodegen implements CodegenConfig { additionalProperties.put(GIT_REPO_URL, gitRepoURL); additionalProperties.put(LICENSE, license); + // make api and model doc path available in mustache template + additionalProperties.put("apiDocPath", apiDocPath); + additionalProperties.put("modelDocPath", modelDocPath); + String swaggerFolder = podName; modelPackage = swaggerFolder; @@ -388,6 +396,26 @@ public class ObjcClientCodegen extends DefaultCodegen implements CodegenConfig { return name; } + @Override + public String apiDocFileFolder() { + return (outputFolder + File.separatorChar + apiDocPath); + } + + @Override + public String modelDocFileFolder() { + return (outputFolder + File.separatorChar + modelDocPath); + } + + @Override + public String toModelDocFilename(String name) { + return toModelName(name); + } + + @Override + public String toApiDocFilename(String name) { + return toApiName(name); + } + @Override public String apiFileFolder() { return outputFolder + File.separatorChar + apiPackage(); @@ -562,4 +590,71 @@ public class ObjcClientCodegen extends DefaultCodegen implements CodegenConfig { return null; } + @Override + public void setParameterExampleValue(CodegenParameter p) { + String example; + + if (p.defaultValue == null) { + example = p.example; + } else { + example = p.defaultValue; + } + + String type = p.baseType; + if (type == null) { + type = p.dataType; + } + + if ("NSString".equalsIgnoreCase(type) || "str".equalsIgnoreCase(type)) { + if (example == null) { + example = p.paramName + "_example"; + } + example = "'" + escapeText(example) + "'"; + } else if ("NSNumber".equals(type)) { + if (example == null) { + example = "56"; + } + /* OBJC uses NSNumber to represent both int, long, double and float + } else if ("Float".equalsIgnoreCase(type) || "Double".equalsIgnoreCase(type)) { + if (example == null) { + example = "3.4"; + } */ + } else if ("BOOLEAN".equalsIgnoreCase(type) || "bool".equalsIgnoreCase(type)) { + if (example == null) { + example = "True"; + } + } else if ("NSURL".equalsIgnoreCase(type)) { + if (example == null) { + example = "/path/to/file"; + } + example = "'" + escapeText(example) + "'"; + } else if ("NSDate".equalsIgnoreCase(type)) { + if (example == null) { + example = "2013-10-20"; + } + example = "'" + escapeText(example) + "'"; + } else if ("DateTime".equalsIgnoreCase(type)) { + if (example == null) { + example = "2013-10-20T19:20:30+01:00"; + } + example = "'" + escapeText(example) + "'"; + } else if (!languageSpecificPrimitives.contains(type)) { + // type is a model class, e.g. User + example = this.packageName + "." + type + "()"; + } else { + LOGGER.warn("Type " + type + " not handled properly in setParameterExampleValue"); + } + + if (example == null) { + example = "NULL"; + } else if (Boolean.TRUE.equals(p.isListContainer)) { + example = "[" + example + "]"; + } else if (Boolean.TRUE.equals(p.isMapContainer)) { + example = "{'key': " + example + "}"; + } + + p.example = example; + } + + }