Skip to content

Commit e01c1a9

Browse files
author
iliaskarim
authored
Readonly properties for Swift 5 (#6048)
* Generate readonly properties in Swift5 with additional readonlyProperties parameter * add sample
1 parent 7424582 commit e01c1a9

File tree

126 files changed

+7263
-4
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

126 files changed

+7263
-4
lines changed

bin/swift5-petstore-all.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@
99
./bin/swift5-petstore-urlsession.sh
1010
./bin/swift5-petstore-alamofire.sh
1111
./bin/swift5-petstore-combine.sh
12-
12+
./bin/swift5-petstore-readonlyProperties.sh
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"podSummary": "PetstoreClient",
3+
"podHomepage": "https://github.com/openapitools/openapi-generator",
4+
"podAuthors": "",
5+
"projectName": "PetstoreClient",
6+
"readonlyProperties": true
7+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/bin/sh
2+
3+
SCRIPT="$0"
4+
echo "# START SCRIPT: $SCRIPT"
5+
6+
while [ -h "$SCRIPT" ] ; do
7+
ls=`ls -ld "$SCRIPT"`
8+
link=`expr "$ls" : '.*-> \(.*\)$'`
9+
if expr "$link" : '/.*' > /dev/null; then
10+
SCRIPT="$link"
11+
else
12+
SCRIPT=`dirname "$SCRIPT"`/"$link"
13+
fi
14+
done
15+
16+
if [ ! -d "${APP_DIR}" ]; then
17+
APP_DIR=`dirname "$SCRIPT"`/..
18+
APP_DIR=`cd "${APP_DIR}"; pwd`
19+
fi
20+
21+
executable="./modules/openapi-generator-cli/target/openapi-generator-cli.jar"
22+
23+
if [ ! -f "$executable" ]
24+
then
25+
mvn -B clean package
26+
fi
27+
28+
# if you've executed sbt assembly previously it will use that instead.
29+
export JAVA_OPTS="${JAVA_OPTS} -Xmx1024M -DloggerPath=conf/log4j.properties"
30+
ags="generate -t modules/openapi-generator/src/main/resources/swift5 -i modules/openapi-generator/src/test/resources/2_0/swift/petstore-with-fake-endpoints-models-for-testing.yaml -g swift5 -c ./bin/swift5-petstore-readonlyProperties.json -o samples/client/petstore/swift5/readonlyProperties --generate-alias-as-model $@"
31+
32+
java $JAVA_OPTS -jar $executable $ags
33+
34+
if type "xcodegen" > /dev/null 2>&1; then
35+
cd samples/client/petstore/swift5/readonlyProperties
36+
xcodegen generate
37+
fi
38+
39+
if type "swiftlint" > /dev/null 2>&1; then
40+
cd samples/client/petstore/swift5/readonlyProperties
41+
swiftlint autocorrect
42+
fi

bin/windows/swift5-petstore-all.bat

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ call .\bin\windows\swift5-petstore-nonPublicApi.bat
77
call .\bin\windows\swift5-petstore-urlsession.bat
88
call .\bin\windows\swift5-petstore-alamofire.bat
99
call .\bin\windows\swift5-petstore-combine.bat
10+
call .\bin\windows\swift5-petstore-readonlyProperties.bat
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
set executable=.\modules\openapi-generator-cli\target\openapi-generator-cli.jar
2+
3+
If Not Exist %executable% (
4+
mvn clean package
5+
)
6+
7+
REM set JAVA_OPTS=%JAVA_OPTS% -Xmx1024M
8+
set ags=generate -i modules\openapi-generator\src\test\resources\2_0\swift\petstore-with-fake-endpoints-models-for-testing.yaml -g swift5 -c bin\swift5-petstore-readonlyProperties.json -o samples\client\petstore\swift5\readonlyProperties
9+
10+
java %JAVA_OPTS% -jar %executable% %ags%

docs/generators/swift5.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ sidebar_label: swift5
2626
|podVersion|Version used for Podspec| |null|
2727
|prependFormOrBodyParameters|Add form or body parameters to the beginning of the parameter list.| |false|
2828
|projectName|Project name in Xcode| |null|
29+
|readonlyProperties|Make properties readonly (default: false)| |null|
2930
|responseAs|Optionally use libraries to manage response. Currently PromiseKit, RxSwift, Result, Combine are available.| |null|
3031
|sortModelPropertiesByRequiredFlag|Sort model properties to place required parameters before optional parameters.| |true|
3132
|sortParamsByRequiredFlag|Sort method arguments to place required parameters before optional parameters.| |true|

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/Swift5ClientCodegen.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public class Swift5ClientCodegen extends DefaultCodegen implements CodegenConfig
5353
public static final String POD_DESCRIPTION = "podDescription";
5454
public static final String POD_SCREENSHOTS = "podScreenshots";
5555
public static final String POD_DOCUMENTATION_URL = "podDocumentationURL";
56+
public static final String READONLY_PROPERTIES = "readonlyProperties";
5657
public static final String SWIFT_USE_API_NAMESPACE = "swiftUseApiNamespace";
5758
public static final String DEFAULT_POD_AUTHORS = "OpenAPI Generator";
5859
public static final String LENIENT_TYPE_CAST = "lenientTypeCast";
@@ -67,6 +68,7 @@ public class Swift5ClientCodegen extends DefaultCodegen implements CodegenConfig
6768
protected boolean nonPublicApi = false;
6869
protected boolean objcCompatible = false;
6970
protected boolean lenientTypeCast = false;
71+
protected boolean readonlyProperties = false;
7072
protected boolean swiftUseApiNamespace;
7173
protected String[] responseAs = new String[0];
7274
protected String sourceFolder = "Classes" + File.separator + "OpenAPIs";
@@ -237,6 +239,8 @@ public Swift5ClientCodegen() {
237239
cliOptions.add(new CliOption(POD_SCREENSHOTS, "Screenshots used for Podspec"));
238240
cliOptions.add(new CliOption(POD_DOCUMENTATION_URL,
239241
"Documentation URL used for Podspec"));
242+
cliOptions.add(new CliOption(READONLY_PROPERTIES, "Make properties "
243+
+ "readonly (default: false)"));
240244
cliOptions.add(new CliOption(SWIFT_USE_API_NAMESPACE,
241245
"Flag to make all the API classes inner-class "
242246
+ "of {{projectName}}API"));
@@ -392,6 +396,13 @@ public void processOpts() {
392396
additionalProperties.put("useCombine", true);
393397
}
394398

399+
// Setup readonlyProperties option, which declares properties so they can only
400+
// be set at initialization
401+
if (additionalProperties.containsKey(READONLY_PROPERTIES)) {
402+
setReadonlyProperties(convertPropertyToBooleanAndWriteBack(READONLY_PROPERTIES));
403+
}
404+
additionalProperties.put(READONLY_PROPERTIES, readonlyProperties);
405+
395406
// Setup swiftUseApiNamespace option, which makes all the API
396407
// classes inner-class of {{projectName}}API
397408
if (additionalProperties.containsKey(SWIFT_USE_API_NAMESPACE)) {
@@ -777,6 +788,10 @@ public void setLenientTypeCast(boolean lenientTypeCast) {
777788
this.lenientTypeCast = lenientTypeCast;
778789
}
779790

791+
public void setReadonlyProperties(boolean readonlyProperties) {
792+
this.readonlyProperties = readonlyProperties;
793+
}
794+
780795
public void setResponseAs(String[] responseAs) {
781796
this.responseAs = responseAs;
782797
}

modules/openapi-generator/src/main/resources/swift5/modelObject.mustache

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
{{#allVars}}
1010
{{#isEnum}}
1111
{{#description}}/** {{description}} */
12-
{{/description}}{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} var {{name}}: {{{datatypeWithEnum}}}{{#required}}{{#isNullable}}?{{/isNullable}}{{/required}}{{^required}}?{{/required}}{{#defaultValue}} = {{{defaultValue}}}{{/defaultValue}}
12+
{{/description}}{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} {{#readonlyProperties}}private(set) {{/readonlyProperties}}var {{name}}: {{{datatypeWithEnum}}}{{#required}}{{#isNullable}}?{{/isNullable}}{{/required}}{{^required}}?{{/required}}{{#defaultValue}} = {{{defaultValue}}}{{/defaultValue}}
1313
{{/isEnum}}
1414
{{^isEnum}}
1515
{{#description}}/** {{description}} */
16-
{{/description}}{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} var {{name}}: {{{datatype}}}{{#required}}{{#isNullable}}?{{/isNullable}}{{/required}}{{^required}}?{{/required}}{{#defaultValue}} = {{{defaultValue}}}{{/defaultValue}}
16+
{{/description}}{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} {{#readonlyProperties}}private(set) {{/readonlyProperties}}var {{name}}: {{{datatype}}}{{#required}}{{#isNullable}}?{{/isNullable}}{{/required}}{{^required}}?{{/required}}{{#defaultValue}} = {{{defaultValue}}}{{/defaultValue}}
1717
{{#objcCompatible}}
1818
{{#vendorExtensions.x-swift-optional-scalar}}
1919
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} var {{name}}Num: NSNumber? {
@@ -34,7 +34,7 @@
3434
}
3535
{{/hasVars}}
3636
{{#additionalPropertiesType}}
37-
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} var additionalProperties: [String:{{{additionalPropertiesType}}}] = [:]
37+
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} {{#readonlyProperties}}private(set) {{/readonlyProperties}}var additionalProperties: [String:{{{additionalPropertiesType}}}] = [:]
3838

3939
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} subscript(key: String) -> {{{additionalPropertiesType}}}? {
4040
get {

modules/openapi-generator/src/test/java/org/openapitools/codegen/options/Swift5OptionsProvider.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public class Swift5OptionsProvider implements OptionsProvider {
4343
public static final String POD_DESCRIPTION_VALUE = "podDescription";
4444
public static final String POD_SCREENSHOTS_VALUE = "podScreenshots";
4545
public static final String POD_DOCUMENTATION_URL_VALUE = "podDocumentationURL";
46+
public static final String READONLY_PROPERTIES_VALUE = "false";
4647
public static final String SWIFT_USE_API_NAMESPACE_VALUE = "swiftUseApiNamespace";
4748
public static final String ALLOW_UNICODE_IDENTIFIERS_VALUE = "false";
4849
public static final String PREPEND_FORM_OR_BODY_PARAMETERS_VALUE = "true";
@@ -74,6 +75,7 @@ public Map<String, String> createOptions() {
7475
.put(Swift5ClientCodegen.POD_DESCRIPTION, POD_DESCRIPTION_VALUE)
7576
.put(Swift5ClientCodegen.POD_SCREENSHOTS, POD_SCREENSHOTS_VALUE)
7677
.put(Swift5ClientCodegen.POD_DOCUMENTATION_URL, POD_DOCUMENTATION_URL_VALUE)
78+
.put(Swift5ClientCodegen.READONLY_PROPERTIES, READONLY_PROPERTIES_VALUE)
7779
.put(Swift5ClientCodegen.SWIFT_USE_API_NAMESPACE, SWIFT_USE_API_NAMESPACE_VALUE)
7880
.put(CodegenConstants.HIDE_GENERATION_TIMESTAMP, "true")
7981
.put(CodegenConstants.ALLOW_UNICODE_IDENTIFIERS, ALLOW_UNICODE_IDENTIFIERS_VALUE)

modules/openapi-generator/src/test/java/org/openapitools/codegen/swift5/Swift5OptionsTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.openapitools.codegen.AbstractOptionsTest;
2121
import org.openapitools.codegen.CodegenConfig;
2222
import org.openapitools.codegen.languages.Swift5ClientCodegen;
23+
import org.openapitools.codegen.options.Swift4OptionsProvider;
2324
import org.openapitools.codegen.options.Swift5OptionsProvider;
2425

2526
import static org.mockito.Mockito.mock;
@@ -47,5 +48,6 @@ protected void verifyOptions() {
4748
verify(clientCodegen).setObjcCompatible(Boolean.parseBoolean(Swift5OptionsProvider.OBJC_COMPATIBLE_VALUE));
4849
verify(clientCodegen).setLenientTypeCast(Boolean.parseBoolean(Swift5OptionsProvider.LENIENT_TYPE_CAST_VALUE));
4950
verify(clientCodegen).setPrependFormOrBodyParameters(Boolean.valueOf(Swift5OptionsProvider.PREPEND_FORM_OR_BODY_PARAMETERS_VALUE));
51+
verify(clientCodegen).setReadonlyProperties(Boolean.valueOf(Swift5OptionsProvider.READONLY_PROPERTIES_VALUE));
5052
}
5153
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Xcode
2+
#
3+
# gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore
4+
5+
## Build generated
6+
build/
7+
DerivedData
8+
9+
## Various settings
10+
*.pbxuser
11+
!default.pbxuser
12+
*.mode1v3
13+
!default.mode1v3
14+
*.mode2v3
15+
!default.mode2v3
16+
*.perspectivev3
17+
!default.perspectivev3
18+
xcuserdata
19+
20+
## Other
21+
*.xccheckout
22+
*.moved-aside
23+
*.xcuserstate
24+
*.xcscmblueprint
25+
26+
## Obj-C/Swift specific
27+
*.hmap
28+
*.ipa
29+
30+
## Playgrounds
31+
timeline.xctimeline
32+
playground.xcworkspace
33+
34+
# Swift Package Manager
35+
#
36+
# Add this line if you want to avoid checking in source code from Swift Package Manager dependencies.
37+
# Packages/
38+
.build/
39+
40+
# CocoaPods
41+
#
42+
# We recommend against adding the Pods directory to your .gitignore. However
43+
# you should judge for yourself, the pros and cons are mentioned at:
44+
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
45+
#
46+
# Pods/
47+
48+
# Carthage
49+
#
50+
# Add this line if you want to avoid checking in source code from Carthage dependencies.
51+
# Carthage/Checkouts
52+
53+
Carthage/Build
54+
55+
# fastlane
56+
#
57+
# It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the
58+
# screenshots whenever they are needed.
59+
# For more information about the recommended setup visit:
60+
# https://github.com/fastlane/fastlane/blob/master/docs/Gitignore.md
61+
62+
fastlane/report.xml
63+
fastlane/screenshots
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# OpenAPI Generator Ignore
2+
# Generated by openapi-generator https://github.com/openapitools/openapi-generator
3+
4+
# Use this file to prevent files from being overwritten by the generator.
5+
# The patterns follow closely to .gitignore or .dockerignore.
6+
7+
# As an example, the C# client generator defines ApiClient.cs.
8+
# You can make changes and tell OpenAPI Generator to ignore just this file by uncommenting the following line:
9+
#ApiClient.cs
10+
11+
# You can match any string of characters against a directory, file or extension with a single asterisk (*):
12+
#foo/*/qux
13+
# The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux
14+
15+
# You can recursively match patterns against a directory, file or extension with a double asterisk (**):
16+
#foo/**/qux
17+
# This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux
18+
19+
# You can also negate patterns with an exclamation (!).
20+
# For example, you can ignore all files in a docs folder with the file extension .md:
21+
#docs/*.md
22+
# Then explicitly reverse the ignore rule for a single file:
23+
#!docs/README.md
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
4.3.1-SNAPSHOT
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>CFBundleDevelopmentRegion</key>
6+
<string>$(DEVELOPMENT_LANGUAGE)</string>
7+
<key>CFBundleExecutable</key>
8+
<string>$(EXECUTABLE_NAME)</string>
9+
<key>CFBundleIdentifier</key>
10+
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
11+
<key>CFBundleInfoDictionaryVersion</key>
12+
<string>6.0</string>
13+
<key>CFBundleName</key>
14+
<string>$(PRODUCT_NAME)</string>
15+
<key>CFBundlePackageType</key>
16+
<string>FMWK</string>
17+
<key>CFBundleShortVersionString</key>
18+
<string>1.0</string>
19+
<key>CFBundleVersion</key>
20+
<string>1</string>
21+
</dict>
22+
</plist>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// swift-tools-version:5.0
2+
3+
import PackageDescription
4+
5+
let package = Package(
6+
name: "PetstoreClient",
7+
platforms: [
8+
.iOS(.v9),
9+
.macOS(.v10_11),
10+
.tvOS(.v9),
11+
.watchOS(.v3)
12+
],
13+
products: [
14+
// Products define the executables and libraries produced by a package, and make them visible to other packages.
15+
.library(
16+
name: "PetstoreClient",
17+
targets: ["PetstoreClient"])
18+
],
19+
dependencies: [
20+
// Dependencies declare other packages that this package depends on.
21+
],
22+
targets: [
23+
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
24+
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
25+
.target(
26+
name: "PetstoreClient",
27+
dependencies: [],
28+
path: "PetstoreClient/Classes"
29+
)
30+
]
31+
)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
Pod::Spec.new do |s|
2+
s.name = 'PetstoreClient'
3+
s.ios.deployment_target = '9.0'
4+
s.osx.deployment_target = '10.11'
5+
s.tvos.deployment_target = '9.0'
6+
s.watchos.deployment_target = '3.0'
7+
s.version = '1.0.0'
8+
s.source = { :git => '[email protected]:OpenAPITools/openapi-generator.git', :tag => 'v1.0.0' }
9+
s.authors = ''
10+
s.license = 'Proprietary'
11+
s.homepage = 'https://github.com/openapitools/openapi-generator'
12+
s.summary = 'PetstoreClient'
13+
s.source_files = 'PetstoreClient/Classes/**/*.swift'
14+
end

0 commit comments

Comments
 (0)