From 378cb5a861c69c7f42eabb47b5bbe5e0a86cc913 Mon Sep 17 00:00:00 2001 From: Shahzaib Date: Thu, 27 May 2021 16:57:06 -0700 Subject: [PATCH 01/17] Add project for json rules --- JsonRules/.gitignore | 92 +++++++++++++++++++ JsonRules/build.gradle | 12 +++ .../AuthClientJsonSchemaRuleFactory.java | 52 +++++++++++ .../rules/AuthClientJsonSchemaTypeRule.java | 59 ++++++++++++ .../identity/json/rules/MapRule.java | 48 ++++++++++ settings.gradle | 2 + 6 files changed, 265 insertions(+) create mode 100644 JsonRules/.gitignore create mode 100644 JsonRules/build.gradle create mode 100644 JsonRules/src/main/java/com/microsoft/identity/json/rules/AuthClientJsonSchemaRuleFactory.java create mode 100644 JsonRules/src/main/java/com/microsoft/identity/json/rules/AuthClientJsonSchemaTypeRule.java create mode 100644 JsonRules/src/main/java/com/microsoft/identity/json/rules/MapRule.java diff --git a/JsonRules/.gitignore b/JsonRules/.gitignore new file mode 100644 index 00000000..01a292f1 --- /dev/null +++ b/JsonRules/.gitignore @@ -0,0 +1,92 @@ +# Built application files +*.apk +*.ap_ + +# Files for the ART/Dalvik VM +*.dex + +# Java class files +*.class + +# VSCode project file. +.project +.classpath +.settings + +# Generated files +bin/ +gen/ +out/ + +# Gradle files +.gradle/ +build/ + +# Local configuration file (sdk path, etc) +local.properties + +# Proguard folder generated by Eclipse +proguard/ + +# Log Files +*.log + +# Android Studio Navigation editor temp files +.navigation/ + +# Android Studio captures folder +captures/ + +# IntelliJ +*.iml +.idea + +# Keystore files +# Uncomment the following line if you do not want to check your keystore files in. +#*.jks + +# External native build folder generated in Android Studio 2.2 and later +.externalNativeBuild + +# Google Services (e.g. APIs or Firebase) +google-services.json + +# Freeline +freeline.py +freeline/ +freeline_project_description.json + +# Ignore poms +*.pom + +# General +.DS_Store +.AppleDouble +.LSOverride + +# Icon must end with two \r +Icon + +# Thumbnails +._* + +# Files that might appear in the root of a volume +.DocumentRevisions-V100 +.fseventsd +.Spotlight-V100 +.TemporaryItems +.Trashes +.VolumeIcon.icns +.com.apple.timemachine.donotpresent + +# Directories potentially created on remote AFP share +.AppleDB +.AppleDesktop +Network Trash Folder +Temporary Items +.apdisk + +# gradle stuffs +gradlew +gradlew.bat +gradle-wrapper.jar diff --git a/JsonRules/build.gradle b/JsonRules/build.gradle new file mode 100644 index 00000000..7183f2b0 --- /dev/null +++ b/JsonRules/build.gradle @@ -0,0 +1,12 @@ +plugins { + id 'java-library' +} + +java { + sourceCompatibility = JavaVersion.VERSION_1_7 + targetCompatibility = JavaVersion.VERSION_1_7 +} + +dependencies { + implementation "org.jsonschema2pojo:jsonschema2pojo-core:1.1.1" +} diff --git a/JsonRules/src/main/java/com/microsoft/identity/json/rules/AuthClientJsonSchemaRuleFactory.java b/JsonRules/src/main/java/com/microsoft/identity/json/rules/AuthClientJsonSchemaRuleFactory.java new file mode 100644 index 00000000..61686e47 --- /dev/null +++ b/JsonRules/src/main/java/com/microsoft/identity/json/rules/AuthClientJsonSchemaRuleFactory.java @@ -0,0 +1,52 @@ +// Copyright (c) Microsoft Corporation. +// All rights reserved. +// +// This code is licensed under the MIT License. +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files(the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions : +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +package com.microsoft.identity.json.rules; + +import com.sun.codemodel.JClass; +import com.sun.codemodel.JClassContainer; +import com.sun.codemodel.JType; + +import org.jsonschema2pojo.rules.Rule; +import org.jsonschema2pojo.rules.RuleFactory; + +/** + * A {@link RuleFactory} that provides a custom implementation of the {@link org.jsonschema2pojo.rules.TypeRule} + * and also provides an additional {@link MapRule} as well. + */ +public class AuthClientJsonSchemaRuleFactory extends RuleFactory { + + @Override + public Rule getTypeRule() { + return new AuthClientJsonSchemaTypeRule(this); + } + + /** + * Provides a rule instance that should be applied when an "object" + * declaration is found in the schema. + * + * @return a schema rule that can handle the "object" declaration. + */ + public Rule getMapRule() { + return new MapRule(); + } +} diff --git a/JsonRules/src/main/java/com/microsoft/identity/json/rules/AuthClientJsonSchemaTypeRule.java b/JsonRules/src/main/java/com/microsoft/identity/json/rules/AuthClientJsonSchemaTypeRule.java new file mode 100644 index 00000000..e1243839 --- /dev/null +++ b/JsonRules/src/main/java/com/microsoft/identity/json/rules/AuthClientJsonSchemaTypeRule.java @@ -0,0 +1,59 @@ +// Copyright (c) Microsoft Corporation. +// All rights reserved. +// +// This code is licensed under the MIT License. +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files(the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions : +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +package com.microsoft.identity.json.rules; + +import com.fasterxml.jackson.databind.JsonNode; +import com.sun.codemodel.JClassContainer; +import com.sun.codemodel.JType; + +import org.jsonschema2pojo.Schema; +import org.jsonschema2pojo.rules.TypeRule; + +/** + * An implementation of {@link TypeRule} that will use a {@link MapRule} to generate a field of a + * Map data type if the type of the field in schema was defined as an "object" AND the field did not + * have any properties on it. Otherwise, this rule will just delegate the operation to its parent. + */ +public class AuthClientJsonSchemaTypeRule extends TypeRule { + + private final AuthClientJsonSchemaRuleFactory ruleFactory; + + protected AuthClientJsonSchemaTypeRule(final AuthClientJsonSchemaRuleFactory ruleFactory) { + super(ruleFactory); + this.ruleFactory = ruleFactory; + } + + @Override + public JType apply(String nodeName, JsonNode node, JsonNode parent, JClassContainer jClassContainer, Schema schema) { + if (node != null && + node.has("type") + && node.get("type") != null + && node.get("type").isTextual() + && node.get("type").asText().equals("object") + && (!node.has("properties") || node.path("properties").size() == 0)) { + return ruleFactory.getMapRule().apply(nodeName, node, parent, jClassContainer, schema); + } else { + return super.apply(nodeName, node, parent, jClassContainer, schema); + } + } +} diff --git a/JsonRules/src/main/java/com/microsoft/identity/json/rules/MapRule.java b/JsonRules/src/main/java/com/microsoft/identity/json/rules/MapRule.java new file mode 100644 index 00000000..d3d69964 --- /dev/null +++ b/JsonRules/src/main/java/com/microsoft/identity/json/rules/MapRule.java @@ -0,0 +1,48 @@ +// Copyright (c) Microsoft Corporation. +// All rights reserved. +// +// This code is licensed under the MIT License. +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files(the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions : +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +package com.microsoft.identity.json.rules; + +import com.fasterxml.jackson.databind.JsonNode; +import com.sun.codemodel.JClass; +import com.sun.codemodel.JClassContainer; + +import org.jsonschema2pojo.Schema; +import org.jsonschema2pojo.rules.Rule; + +import java.util.Map; + +/** + * A {@link Rule} to generate fields with the Map data type. + */ +public class MapRule implements Rule { + @Override + public JClass apply(String nodeName, JsonNode node, JsonNode parent, JClassContainer jClassContainer, Schema schema) { + return addPropertyAsMap(jClassContainer); + } + + private JClass addPropertyAsMap(final JClassContainer jclass) { + return jclass.owner().ref(Map.class).narrow( + jclass.owner().ref(String.class), jclass.owner().ref(String.class) + ); + } +} diff --git a/settings.gradle b/settings.gradle index 30277d82..022178c7 100644 --- a/settings.gradle +++ b/settings.gradle @@ -84,6 +84,8 @@ project(':broker4j').projectDir = new File('broker/broker4j') include(":LinuxBroker") project(':LinuxBroker').projectDir = new File('broker/LinuxBroker') +include(":JsonRules") + //include(":AzureSample") //project(':AzureSample').projectDir = new File('azuresample/app') From 1e9fe4abf58592e949f1b0f7a7c3e16ce71c86d1 Mon Sep 17 00:00:00 2001 From: Shahzaib Date: Thu, 27 May 2021 17:01:53 -0700 Subject: [PATCH 02/17] Add gradle wrapper properties --- JsonRules/gradle/wrapper/gradle-wrapper.properties | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 JsonRules/gradle/wrapper/gradle-wrapper.properties diff --git a/JsonRules/gradle/wrapper/gradle-wrapper.properties b/JsonRules/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 00000000..be52383e --- /dev/null +++ b/JsonRules/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,5 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-bin.zip +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists From 48600841bbcf066e28f834a35a9074ad98e8ff35 Mon Sep 17 00:00:00 2001 From: Shahzaib Date: Thu, 27 May 2021 17:02:02 -0700 Subject: [PATCH 03/17] Add changelog for JsonRules --- JsonRules/changelog | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 JsonRules/changelog diff --git a/JsonRules/changelog b/JsonRules/changelog new file mode 100644 index 00000000..9cb9c134 --- /dev/null +++ b/JsonRules/changelog @@ -0,0 +1,4 @@ +Version 0.0.1 +---------------------------- +- Add a MapRule to generate Map fields +- Add a custom rule factory that that overrides the TypeRule and also provides a MapRule From b1a346d8cb2f41e0f9271feefe20418a95477691 Mon Sep 17 00:00:00 2001 From: Shahzaib Date: Thu, 27 May 2021 17:08:53 -0700 Subject: [PATCH 04/17] Ad publishing logic for json rules --- JsonRules/build.gradle | 104 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) diff --git a/JsonRules/build.gradle b/JsonRules/build.gradle index 7183f2b0..99d6214a 100644 --- a/JsonRules/build.gradle +++ b/JsonRules/build.gradle @@ -1,5 +1,6 @@ plugins { id 'java-library' + id 'maven-publish' } java { @@ -10,3 +11,106 @@ java { dependencies { implementation "org.jsonschema2pojo:jsonschema2pojo-core:1.1.1" } + +project.ext.vstsUsername = System.getenv("ENV_VSTS_MVN_ANDROIDCOMMON_USERNAME") != null ? System.getenv("ENV_VSTS_MVN_ANDROIDCOMMON_USERNAME") : project.findProperty("vstsUsername") +project.ext.vstsPassword = System.getenv("ENV_VSTS_MVN_ANDROIDCOMMON_ACCESSTOKEN") != null ? System.getenv("ENV_VSTS_MVN_ANDROIDCOMMON_ACCESSTOKEN") : project.findProperty("vstsMavenAccessToken") + +task sourcesJar(type: Jar) { + from sourceSets.main.java.srcDirs + classifier = 'sources' + destinationDirectory = reporting.file("$project.buildDir/output/jars") +} + +// Task to generate javadoc +task generateJavadoc(type: Javadoc) { + failOnError false + title = "Microsoft Identity Json2POJO Rules" + source = sourceSets.main.java + classpath += project.sourceSets.main.compileClasspath + + options.memberLevel = JavadocMemberLevel.PUBLIC + options.addStringOption('Xdoclint:none', '-quiet') + + exclude '**/BuildConfig.Java' + exclude '**/R.java' + destinationDir = reporting.file("$project.buildDir/output/javadoc") +} + +// Task to generate javadoc.jar +task javadocJar(type: Jar, dependsOn: generateJavadoc) { + from javadoc.destinationDir + classifier 'javadoc' + destinationDirectory = reporting.file("$project.buildDir/output/jars") +} + +jar { + manifest { + attributes('Implementation-Title': project.name, + 'Implementation-Version': project.version) + } +} + +publishing { + publications { + aar(MavenPublication) { + groupId 'com.microsoft.identity.json' + artifactId 'rules' + from components.java + + pom { + name = 'common4j' + description = 'This library contains rules for generating Java classes from Json ' + + 'schema using the jsonschema2pojo library.' + url = 'https://github.com/AzureAD/android-complete' + developers { + developer { + id = 'microsoft' + name = 'Microsoft' + } + } + licenses { + license { + name = 'MIT License' + } + } + inceptionYear = '2021' + scm { + url = 'https://github.com/AzureAD/android-complete' + } + properties = [ + branch : 'master', + version: project.version + ] + } + } + } + + repositories { + maven { + name "vsts-maven-adal-android" + url "https://identitydivision.pkgs.visualstudio.com/_packaging/AndroidADAL/maven/v1" + credentials { + username project.ext.vstsUsername + password project.ext.vstsPassword + } + } + maven { + name "vsts-maven-android" + url 'https://identitydivision.pkgs.visualstudio.com/IDDP/_packaging/Android/maven/v1' + credentials { + username project.vstsUsername + password project.vstsPassword + } + } + } +} + +sourceSets { + main { + java.srcDirs = ['src/main'] + } + test { + java.srcDirs = ['src/test'] + } +} + From d1506d46c776c78bb1303bd83accfd5148548b1d Mon Sep 17 00:00:00 2001 From: Shahzaib Date: Thu, 27 May 2021 17:10:15 -0700 Subject: [PATCH 05/17] Fix package name --- .../identity/json/rules/AuthClientJsonSchemaRuleFactory.java | 0 .../identity/json/rules/AuthClientJsonSchemaTypeRule.java | 0 .../{java => }/com/microsoft/identity/json/rules/MapRule.java | 0 3 files changed, 0 insertions(+), 0 deletions(-) rename JsonRules/src/main/{java => }/com/microsoft/identity/json/rules/AuthClientJsonSchemaRuleFactory.java (100%) rename JsonRules/src/main/{java => }/com/microsoft/identity/json/rules/AuthClientJsonSchemaTypeRule.java (100%) rename JsonRules/src/main/{java => }/com/microsoft/identity/json/rules/MapRule.java (100%) diff --git a/JsonRules/src/main/java/com/microsoft/identity/json/rules/AuthClientJsonSchemaRuleFactory.java b/JsonRules/src/main/com/microsoft/identity/json/rules/AuthClientJsonSchemaRuleFactory.java similarity index 100% rename from JsonRules/src/main/java/com/microsoft/identity/json/rules/AuthClientJsonSchemaRuleFactory.java rename to JsonRules/src/main/com/microsoft/identity/json/rules/AuthClientJsonSchemaRuleFactory.java diff --git a/JsonRules/src/main/java/com/microsoft/identity/json/rules/AuthClientJsonSchemaTypeRule.java b/JsonRules/src/main/com/microsoft/identity/json/rules/AuthClientJsonSchemaTypeRule.java similarity index 100% rename from JsonRules/src/main/java/com/microsoft/identity/json/rules/AuthClientJsonSchemaTypeRule.java rename to JsonRules/src/main/com/microsoft/identity/json/rules/AuthClientJsonSchemaTypeRule.java diff --git a/JsonRules/src/main/java/com/microsoft/identity/json/rules/MapRule.java b/JsonRules/src/main/com/microsoft/identity/json/rules/MapRule.java similarity index 100% rename from JsonRules/src/main/java/com/microsoft/identity/json/rules/MapRule.java rename to JsonRules/src/main/com/microsoft/identity/json/rules/MapRule.java From cf1ffee42fbe8f03409c8fbc51c6589cbed9db87 Mon Sep 17 00:00:00 2001 From: Shahzaib Date: Thu, 27 May 2021 17:26:01 -0700 Subject: [PATCH 06/17] Add version tasks to json rules --- JsonRules/build.gradle | 4 +++ JsonRules/versioning/version.properties | 3 ++ JsonRules/versioning/version_tasks.gradle | 38 +++++++++++++++++++++++ 3 files changed, 45 insertions(+) create mode 100644 JsonRules/versioning/version.properties create mode 100644 JsonRules/versioning/version_tasks.gradle diff --git a/JsonRules/build.gradle b/JsonRules/build.gradle index 99d6214a..70c34dd1 100644 --- a/JsonRules/build.gradle +++ b/JsonRules/build.gradle @@ -12,6 +12,10 @@ dependencies { implementation "org.jsonschema2pojo:jsonschema2pojo-core:1.1.1" } +apply from: './versioning/version_tasks.gradle' + +version = getAppVersionName() + project.ext.vstsUsername = System.getenv("ENV_VSTS_MVN_ANDROIDCOMMON_USERNAME") != null ? System.getenv("ENV_VSTS_MVN_ANDROIDCOMMON_USERNAME") : project.findProperty("vstsUsername") project.ext.vstsPassword = System.getenv("ENV_VSTS_MVN_ANDROIDCOMMON_ACCESSTOKEN") != null ? System.getenv("ENV_VSTS_MVN_ANDROIDCOMMON_ACCESSTOKEN") : project.findProperty("vstsMavenAccessToken") diff --git a/JsonRules/versioning/version.properties b/JsonRules/versioning/version.properties new file mode 100644 index 00000000..45c13b73 --- /dev/null +++ b/JsonRules/versioning/version.properties @@ -0,0 +1,3 @@ +#Wed May 12 20:08:39 UTC 2021 +versionName=0.0.1 +versionCode=1 diff --git a/JsonRules/versioning/version_tasks.gradle b/JsonRules/versioning/version_tasks.gradle new file mode 100644 index 00000000..de76c8b2 --- /dev/null +++ b/JsonRules/versioning/version_tasks.gradle @@ -0,0 +1,38 @@ +def getVersionFile() { + File file = new File("$rootDir/JsonRules/versioning/version.properties") + if(file.exists()){ + return file; + }else{ + return new File("$rootDir/versioning/version.properties") + } +} + +def getVersionProps() { + def versionProps = new Properties(); + getVersionFile().withInputStream {stream -> versionProps.load(stream)} + return versionProps +} + +private String getVersionNamePatch() { + return (getVersionProps()['versionName'] =~ /[^.]+/)[2].toString() +} + +private Integer getVersionNameMinor() { + return (getVersionProps()['versionName'] =~ /\d+/)[1].toInteger() +} + +private Integer getVersionNameMajor() { + return (getVersionProps()['versionName'] =~ /\d+/)[0].toInteger() +} + +private Integer getLatestPatchVersion() { + return getVersionProps()['latestPatchVersion'].toInteger() +} + +ext.getAppVersionCode = { + getVersionProps()['versionCode'].toInteger() +} + +ext.getAppVersionName = { + getVersionProps()['versionName'].toString() +} From 808836affbadc61ed6b53888a798acf83d4e96ab Mon Sep 17 00:00:00 2001 From: Shahzaib Date: Thu, 27 May 2021 17:29:36 -0700 Subject: [PATCH 07/17] Add pipeline for doing vsts release for json rules --- azure-pipelines/vsts-releases/json-rules.yml | 44 ++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 azure-pipelines/vsts-releases/json-rules.yml diff --git a/azure-pipelines/vsts-releases/json-rules.yml b/azure-pipelines/vsts-releases/json-rules.yml new file mode 100644 index 00000000..26a1dd49 --- /dev/null +++ b/azure-pipelines/vsts-releases/json-rules.yml @@ -0,0 +1,44 @@ +# File: azure-pipelines\pull-request-validation\broker4j.yml +# Description: Assemble Json Rules +# Variable: 'ENV_VSTS_MVN_ANDROIDCOMMON_USERNAME' was defined in the Variables tab +# Variable: 'mvnAccessToken' was defined in the Variables tab +# https://docs.microsoft.com/en-us/azure/devops/organizations/accounts/use-personal-access-tokens-to-authenticate +name: $(date:yyyyMMdd)$(rev:.r) + +trigger: none +pr: none + +resources: + repositories: + - repository: common + type: github + name: AzureAD/microsoft-authentication-library-common-for-android + ref: dev + endpoint: ANDROID_GITHUB + +jobs: + - job: Phase_1 + displayName: Build & Publish + cancelTimeoutInMinutes: 1 + pool: + name: Hosted Windows 2019 with VS2019 + steps: + - checkout: self + clean: true + submodules: recursive + persistCredentials: True + - template: azure-pipelines/templates/steps/credscan-policheck.yml@common + parameters: + policheckCmdLineArgsDir: broker + - template: azure-pipelines/templates/steps/automation-cert.yml@common + - task: Gradle@1 + name: Gradle1 + displayName: Assemble + inputs: + tasks: JsonRules:clean JsonRules:assemble --build-cache --info + publishJUnitResults: false + jdkArchitecture: x86 + sqAnalysisBreakBuildIfQualityGateFailed: false + - task: ComponentGovernanceComponentDetection@0 + displayName: Component Detection +... From 1c5c7056b6188408ecf598bb84919ea8fc5504dc Mon Sep 17 00:00:00 2001 From: Shahzaib Date: Thu, 27 May 2021 17:38:55 -0700 Subject: [PATCH 08/17] Add publish task to json rules publish pipeline --- azure-pipelines/vsts-releases/json-rules.yml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/azure-pipelines/vsts-releases/json-rules.yml b/azure-pipelines/vsts-releases/json-rules.yml index 26a1dd49..ba1128e7 100644 --- a/azure-pipelines/vsts-releases/json-rules.yml +++ b/azure-pipelines/vsts-releases/json-rules.yml @@ -39,6 +39,11 @@ jobs: publishJUnitResults: false jdkArchitecture: x86 sqAnalysisBreakBuildIfQualityGateFailed: false + - task: Gradle@2 + displayName: Publish + inputs: + tasks: JsonRules:publish + publishJUnitResults: false - task: ComponentGovernanceComponentDetection@0 displayName: Component Detection ... From b8beeac55a34619a1c564b321c644af9938e06db Mon Sep 17 00:00:00 2001 From: Shahzaib Date: Thu, 27 May 2021 17:53:05 -0700 Subject: [PATCH 09/17] Attempt to fix json rules vsts release pipeline --- azure-pipelines/vsts-releases/json-rules.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/azure-pipelines/vsts-releases/json-rules.yml b/azure-pipelines/vsts-releases/json-rules.yml index ba1128e7..15c64340 100644 --- a/azure-pipelines/vsts-releases/json-rules.yml +++ b/azure-pipelines/vsts-releases/json-rules.yml @@ -31,6 +31,8 @@ jobs: parameters: policheckCmdLineArgsDir: broker - template: azure-pipelines/templates/steps/automation-cert.yml@common + parameters: + envVstsMvnAt: ENV_VSTS_MVN_ANDROIDCOMMON_ACCESSTOKEN - task: Gradle@1 name: Gradle1 displayName: Assemble From fc3002020b6ad50bb4008c6204af7898f433a1bb Mon Sep 17 00:00:00 2001 From: Shahzaib Date: Fri, 28 May 2021 11:09:32 -0700 Subject: [PATCH 10/17] Add a pipeline for JsonRules pull request validation --- .../pull-request-validation/json-rules.yml | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 azure-pipelines/pull-request-validation/json-rules.yml diff --git a/azure-pipelines/pull-request-validation/json-rules.yml b/azure-pipelines/pull-request-validation/json-rules.yml new file mode 100644 index 00000000..3611b6ad --- /dev/null +++ b/azure-pipelines/pull-request-validation/json-rules.yml @@ -0,0 +1,51 @@ +# File: azure-pipelines\pull-request-validation\json-rules.yml +# Description: Assemble Json Rules +# Variable: 'ENV_VSTS_MVN_ANDROIDCOMMON_USERNAME' was defined in the Variables tab +# Variable: 'mvnAccessToken' was defined in the Variables tab +# https://docs.microsoft.com/en-us/azure/devops/organizations/accounts/use-personal-access-tokens-to-authenticate +name: $(date:yyyyMMdd)$(rev:.r) + +trigger: + branches: + include: + - dev + - master + - release/* + batch: True + +resources: + repositories: + - repository: common + type: github + name: AzureAD/microsoft-authentication-library-common-for-android + ref: dev + endpoint: ANDROID_GITHUB + +jobs: + - job: Phase_1 + displayName: Build & Test + cancelTimeoutInMinutes: 1 + pool: + name: Hosted Windows 2019 with VS2019 + steps: + - checkout: self + clean: true + submodules: recursive + persistCredentials: True + - template: azure-pipelines/templates/steps/credscan-policheck.yml@common + parameters: + policheckCmdLineArgsDir: broker + - template: azure-pipelines/templates/steps/automation-cert.yml@common + parameters: + envVstsMvnAt: ENV_VSTS_MVN_ANDROIDCOMMON_ACCESSTOKEN + - task: Gradle@1 + name: Gradle1 + displayName: Assemble + inputs: + tasks: JsonRules:clean JsonRules:assemble --build-cache --info + publishJUnitResults: false + jdkArchitecture: x86 + sqAnalysisBreakBuildIfQualityGateFailed: false + - task: ComponentGovernanceComponentDetection@0 + displayName: Component Detection +... From 9a4f13dbbcc88848d04b1bc9b6ff9a3b88009078 Mon Sep 17 00:00:00 2001 From: Shahzaib Date: Fri, 28 May 2021 11:12:19 -0700 Subject: [PATCH 11/17] Fix comment in json rule yml file --- azure-pipelines/vsts-releases/json-rules.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/azure-pipelines/vsts-releases/json-rules.yml b/azure-pipelines/vsts-releases/json-rules.yml index 15c64340..edc1e3a1 100644 --- a/azure-pipelines/vsts-releases/json-rules.yml +++ b/azure-pipelines/vsts-releases/json-rules.yml @@ -1,4 +1,4 @@ -# File: azure-pipelines\pull-request-validation\broker4j.yml +# File: azure-pipelines\vsts-releases\json-rules.yml # Description: Assemble Json Rules # Variable: 'ENV_VSTS_MVN_ANDROIDCOMMON_USERNAME' was defined in the Variables tab # Variable: 'mvnAccessToken' was defined in the Variables tab From 19a64dc9de59893f915a0faf2ba866b8b5e711a1 Mon Sep 17 00:00:00 2001 From: Shahzaib Date: Fri, 28 May 2021 20:18:16 -0700 Subject: [PATCH 12/17] Add json schema rule to generate a private constructor --- .../rules/AuthClientJsonSchemaObjectRule.java | 66 +++++++++++++++++++ .../AuthClientJsonSchemaRuleFactory.java | 8 +++ .../json/rules/PrivateConstructorRule.java | 46 +++++++++++++ 3 files changed, 120 insertions(+) create mode 100644 JsonRules/src/main/com/microsoft/identity/json/rules/AuthClientJsonSchemaObjectRule.java create mode 100644 JsonRules/src/main/com/microsoft/identity/json/rules/PrivateConstructorRule.java diff --git a/JsonRules/src/main/com/microsoft/identity/json/rules/AuthClientJsonSchemaObjectRule.java b/JsonRules/src/main/com/microsoft/identity/json/rules/AuthClientJsonSchemaObjectRule.java new file mode 100644 index 00000000..fe812cdc --- /dev/null +++ b/JsonRules/src/main/com/microsoft/identity/json/rules/AuthClientJsonSchemaObjectRule.java @@ -0,0 +1,66 @@ +// Copyright (c) Microsoft Corporation. +// All rights reserved. +// +// This code is licensed under the MIT License. +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files(the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions : +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +package com.microsoft.identity.json.rules; + +import com.fasterxml.jackson.databind.JsonNode; +import com.sun.codemodel.JDefinedClass; +import com.sun.codemodel.JPackage; +import com.sun.codemodel.JType; + +import org.jsonschema2pojo.Schema; +import org.jsonschema2pojo.rules.ObjectRule; +import org.jsonschema2pojo.util.ParcelableHelper; +import org.jsonschema2pojo.util.ReflectionHelper; + +/** + * A custom {@link ObjectRule} that has the capability to generate private constructor if the + * config specified to NOT generate constructor AND also NOT generate any setters. + *

+ * We do this because if no constructor is generated, then JAVA will by default have a public + * NO-arg constructor for the class. This is a problem because this would allow someone to + * instantiate the object without actually populating any of its fields and also not having the + * ability to populate any fields post object construction because setters were not generated. To + * overcome this problem, we would simply generate a PRIVATE no-arg constructor so that no one can + * actually instantiate the object without any properties on it. + */ +public class AuthClientJsonSchemaObjectRule extends ObjectRule { + + private AuthClientJsonSchemaRuleFactory ruleFactory; + + protected AuthClientJsonSchemaObjectRule(AuthClientJsonSchemaRuleFactory ruleFactory, ParcelableHelper parcelableHelper, ReflectionHelper reflectionHelper) { + super(ruleFactory, parcelableHelper, reflectionHelper); + this.ruleFactory = ruleFactory; + } + + @Override + public JType apply(final String nodeName, final JsonNode node, final JsonNode parent, + final JPackage jPackage, final Schema schema) { + final JType jType = super.apply(nodeName, node, parent, jPackage, schema); + if (jType instanceof JDefinedClass + && !ruleFactory.getGenerationConfig().isIncludeConstructors() + && !ruleFactory.getGenerationConfig().isIncludeSetters()) { + new PrivateConstructorRule().apply(nodeName, node, parent, (JDefinedClass) jType, schema); + } + return jType; + } +} diff --git a/JsonRules/src/main/com/microsoft/identity/json/rules/AuthClientJsonSchemaRuleFactory.java b/JsonRules/src/main/com/microsoft/identity/json/rules/AuthClientJsonSchemaRuleFactory.java index 61686e47..555f7395 100644 --- a/JsonRules/src/main/com/microsoft/identity/json/rules/AuthClientJsonSchemaRuleFactory.java +++ b/JsonRules/src/main/com/microsoft/identity/json/rules/AuthClientJsonSchemaRuleFactory.java @@ -24,10 +24,13 @@ import com.sun.codemodel.JClass; import com.sun.codemodel.JClassContainer; +import com.sun.codemodel.JDefinedClass; +import com.sun.codemodel.JPackage; import com.sun.codemodel.JType; import org.jsonschema2pojo.rules.Rule; import org.jsonschema2pojo.rules.RuleFactory; +import org.jsonschema2pojo.util.ParcelableHelper; /** * A {@link RuleFactory} that provides a custom implementation of the {@link org.jsonschema2pojo.rules.TypeRule} @@ -49,4 +52,9 @@ public Rule getTypeRule() { public Rule getMapRule() { return new MapRule(); } + + @Override + public Rule getObjectRule() { + return new AuthClientJsonSchemaObjectRule(this, new ParcelableHelper(), getReflectionHelper()); + } } diff --git a/JsonRules/src/main/com/microsoft/identity/json/rules/PrivateConstructorRule.java b/JsonRules/src/main/com/microsoft/identity/json/rules/PrivateConstructorRule.java new file mode 100644 index 00000000..1110383f --- /dev/null +++ b/JsonRules/src/main/com/microsoft/identity/json/rules/PrivateConstructorRule.java @@ -0,0 +1,46 @@ +// Copyright (c) Microsoft Corporation. +// All rights reserved. +// +// This code is licensed under the MIT License. +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files(the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions : +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +package com.microsoft.identity.json.rules; + +import com.fasterxml.jackson.databind.JsonNode; +import com.sun.codemodel.JDefinedClass; +import com.sun.codemodel.JMod; + +import org.jsonschema2pojo.Schema; +import org.jsonschema2pojo.rules.Rule; + +/** + * A {@link Rule} that can generate a no-arg private constructor on a {@link JDefinedClass}. + */ +public class PrivateConstructorRule implements Rule { + @Override + public JDefinedClass apply(String nodeName, JsonNode node, JsonNode parent, JDefinedClass instanceClass, Schema currentSchema) { + generateNoArgsPrivateConstructor(instanceClass); + return instanceClass; + } + + private void generateNoArgsPrivateConstructor(final JDefinedClass jclass) { + // add a no-args constructor to this class + jclass.constructor(JMod.PRIVATE); + } +} From f287e0893dd04a366bcbaf594558da98ee81bfa3 Mon Sep 17 00:00:00 2001 From: Shahzaib Date: Fri, 28 May 2021 20:18:42 -0700 Subject: [PATCH 13/17] Bump json rules to 0.0.2 --- JsonRules/versioning/version.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/JsonRules/versioning/version.properties b/JsonRules/versioning/version.properties index 45c13b73..dfe16e6a 100644 --- a/JsonRules/versioning/version.properties +++ b/JsonRules/versioning/version.properties @@ -1,3 +1,3 @@ #Wed May 12 20:08:39 UTC 2021 -versionName=0.0.1 +versionName=0.0.2 versionCode=1 From d5e903d7866037548d6dd9d35c85f245fad2e44d Mon Sep 17 00:00:00 2001 From: Shahzaib Date: Fri, 28 May 2021 20:20:27 -0700 Subject: [PATCH 14/17] Update changelog --- JsonRules/changelog | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/JsonRules/changelog b/JsonRules/changelog index 9cb9c134..72872280 100644 --- a/JsonRules/changelog +++ b/JsonRules/changelog @@ -1,3 +1,7 @@ +Version 0.0.2 +---------------------------- +- Add a Rule to generate private no-arg constructor if both constructors and setters disabled + Version 0.0.1 ---------------------------- - Add a MapRule to generate Map fields From 875c41c83aed026ecfaa7a2ad0a252d632b4433e Mon Sep 17 00:00:00 2001 From: Shahzaib Date: Tue, 1 Jun 2021 20:41:18 -0700 Subject: [PATCH 15/17] Update Map Rule to generate Map that can hold any Object --- .../src/main/com/microsoft/identity/json/rules/MapRule.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/JsonRules/src/main/com/microsoft/identity/json/rules/MapRule.java b/JsonRules/src/main/com/microsoft/identity/json/rules/MapRule.java index d3d69964..a2d2e3cf 100644 --- a/JsonRules/src/main/com/microsoft/identity/json/rules/MapRule.java +++ b/JsonRules/src/main/com/microsoft/identity/json/rules/MapRule.java @@ -42,7 +42,7 @@ public JClass apply(String nodeName, JsonNode node, JsonNode parent, JClassConta private JClass addPropertyAsMap(final JClassContainer jclass) { return jclass.owner().ref(Map.class).narrow( - jclass.owner().ref(String.class), jclass.owner().ref(String.class) + jclass.owner().ref(String.class), jclass.owner().ref(Object.class) ); } } From 883eb6631755e69b0786b8165c10b1b178a4b01b Mon Sep 17 00:00:00 2001 From: Shahzaib Date: Tue, 1 Jun 2021 20:42:10 -0700 Subject: [PATCH 16/17] Bump to 0.0.3 --- JsonRules/versioning/version.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/JsonRules/versioning/version.properties b/JsonRules/versioning/version.properties index dfe16e6a..1fa6df94 100644 --- a/JsonRules/versioning/version.properties +++ b/JsonRules/versioning/version.properties @@ -1,3 +1,3 @@ #Wed May 12 20:08:39 UTC 2021 -versionName=0.0.2 +versionName=0.0.3 versionCode=1 From 866f08c18d99e5d74e149cbeb443ca8c76bfaabc Mon Sep 17 00:00:00 2001 From: Shahzaib Date: Tue, 1 Jun 2021 20:42:57 -0700 Subject: [PATCH 17/17] Update changelog --- JsonRules/changelog | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/JsonRules/changelog b/JsonRules/changelog index 72872280..1242302d 100644 --- a/JsonRules/changelog +++ b/JsonRules/changelog @@ -1,3 +1,7 @@ +Version 0.0.3 +---------------------------- +- Update Map Rule to generate Map that maps Strings keys to Object values + Version 0.0.2 ---------------------------- - Add a Rule to generate private no-arg constructor if both constructors and setters disabled