Repository: eclipse/microprofile-open-api
Pull request #345 (2019-04-12 21:18:37)

Remove Scope
For issue #328 deprecated `Scope` class to fit OpenAPI 3.0, now `scope` is just a simple **Map** of strings.


Patch:
diff --git a/api/src/main/java/org/eclipse/microprofile/openapi/OASFactory.java b/api/src/main/java/org/eclipse/microprofile/openapi/OASFactory.java
index 77d8751..24cfe40 100644
--- a/api/src/main/java/org/eclipse/microprofile/openapi/OASFactory.java
+++ b/api/src/main/java/org/eclipse/microprofile/openapi/OASFactory.java
@@ -42,7 +42,6 @@ import org.eclipse.microprofile.openapi.models.responses.APIResponse;
 import org.eclipse.microprofile.openapi.models.responses.APIResponses;
 import org.eclipse.microprofile.openapi.models.security.OAuthFlow;
 import org.eclipse.microprofile.openapi.models.security.OAuthFlows;
-import org.eclipse.microprofile.openapi.models.security.Scopes;
 import org.eclipse.microprofile.openapi.models.security.SecurityRequirement;
 import org.eclipse.microprofile.openapi.models.security.SecurityScheme;
 import org.eclipse.microprofile.openapi.models.servers.Server;
@@ -306,15 +305,6 @@ public final class OASFactory {
         return createObject(OAuthFlows.class);
     }
 
-    /**
-     * This method creates a new {@link org.eclipse.microprofile.openapi.models.security.Scopes} instance.
-     *
-     * @return a new Scopes instance
-     */
-    public static Scopes createScopes() {
-        return createObject(Scopes.class);
-    }
-
     /**
      * This method creates a new {@link org.eclipse.microprofile.openapi.models.security.SecurityRequirement} instance.
      *
diff --git a/api/src/main/java/org/eclipse/microprofile/openapi/models/security/OAuthFlow.java b/api/src/main/java/org/eclipse/microprofile/openapi/models/security/OAuthFlow.java
index f9b5913..7f04868 100644
--- a/api/src/main/java/org/eclipse/microprofile/openapi/models/security/OAuthFlow.java
+++ b/api/src/main/java/org/eclipse/microprofile/openapi/models/security/OAuthFlow.java
@@ -17,6 +17,8 @@
 
 package org.eclipse.microprofile.openapi.models.security;
 
+import java.util.Map;
+
 import org.eclipse.microprofile.openapi.models.Constructible;
 import org.eclipse.microprofile.openapi.models.Extensible;
 
@@ -139,13 +141,20 @@ public interface OAuthFlow extends Constructible, Extensible<OAuthFlow> {
     }
 
     /**
-     * The available scopes for the OAuth2 security scheme. A map between the scope name and a short description for it. This is a REQUIRED property.
-     * <p>
-     * This method returns the scopes property from OAuthFlow instance.
-     * </p> 
-     * @return Scopes scopes
-     **/
-    Scopes getScopes();
+     * Adds name of an existing scope object and item parameters to scopes as a key-value pair in a map.
+     *
+     * @param scope the name of a scope
+     * @param description description of the scope.
+     * @return the current OAuthFlow instance
+     */
+    OAuthFlow addScope(String scope, String description);
+
+    /**
+     * Removes the given scope item to this scope mapping.
+     * 
+     * @param scope the name of a scope
+     */
+    void removeScope(String scope);
 
     /**
      * The available scopes for the OAuth2 security scheme. A map between the scope name and a short description for it. This is a REQUIRED property.
@@ -157,7 +166,7 @@ public interface OAuthFlow extends Constructible, Extensible<OAuthFlow> {
      * </p>
      * @param scopes the available scopes for the OAuth2 security scheme
      */
-    void setScopes(Scopes scopes);
+    void setScopes(Map<String, String> scopes);
 
     /**
      * The available scopes for the OAuth2 security scheme. A map between the scope name and a short description for it. This is a REQUIRED property.
@@ -170,9 +179,18 @@ public interface OAuthFlow extends Constructible, Extensible<OAuthFlow> {
      * @param scopes the available scopes for the OAuth2 security scheme
      * @return OAuthFlow instance with the set scopes property
      */
-    default OAuthFlow scopes(Scopes scopes) {
+    default OAuthFlow scopes(Map<String, String> scopes) {
         setScopes(scopes);
         return this;
     }
 
+    /**
+     * The available scopes for the OAuth2 security scheme. A map between the scope name and a short description for it. This is a REQUIRED property.
+     * <p>
+     * This method returns the scopes property from OAuthFlow instance.
+     * </p> 
+     * @return a copy Map (potentially immutable) containing scopes and their descriptions
+     **/
+    Map<String, String> getScopes();
+
 }
\ No newline at end of file
diff --git a/api/src/main/java/org/eclipse/microprofile/openapi/models/security/Scopes.java b/api/src/main/java/org/eclipse/microprofile/openapi/models/security/Scopes.java
deleted file mode 100644
index bb7993b..0000000
--- a/api/src/main/java/org/eclipse/microprofile/openapi/models/security/Scopes.java
+++ /dev/null
@@ -1,91 +0,0 @@
-/**
- * Copyright (c) 2017 Contributors to the Eclipse Foundation
- * Copyright 2017 SmartBear Software
- * <p>
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- * <p>
- * http://www.apache.org/licenses/LICENSE-2.0
- * <p>
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.eclipse.microprofile.openapi.models.security;
-
-import java.util.Map;
-
-import org.eclipse.microprofile.openapi.models.Constructible;
-import org.eclipse.microprofile.openapi.models.Extensible;
-
-/**
- * Scopes is a property of OAuth Flow Object.
- * 
- * @see <a href="https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#oauthFlowObject">OAuthFlow Object</a>
- **/
-
-public interface Scopes extends Constructible, Extensible<Scopes> {
-
-    /**
-     * Adds name of an existing scope object and item parameters to a Scopes instance as a key-value pair in a map.
-     *
-     * @param scope the name of a scope
-     * @param description description of the scope.
-     * @return Scopes instance with the added key-value pair
-     */
-    Scopes addScope(String scope, String description);
-
-    /**
-     * Removes the given scope item to this Scopes.
-     * 
-     * @param scope the name of a scope
-     */
-    void removeScope(String scope);
-
-    /**
-     * Returns a copy map (potentially immutable) of scopes.
-     * 
-     * @return all items
-     */
-    Map<String, String> getScopes();
-
-    /**
-     * Set the scope items map to this Scopes
-     * 
-     * @param items key-value pair in a map.
-     */
-    void setScopes(Map<String, String> items);
-
-    /**
-     * Check whether a scope item is present in the map. This is a convenience method for <code>getScopes().containsKey(name)</code>
-     * 
-     * @param scope the name of a scope.
-     * @return a boolean to indicate if the scope item is present or not.
-     */
-    default boolean hasScope(String scope) {
-        Map<String, String> map = getScopes();
-        if (map == null) {
-            return false;
-        }
-        return map.containsKey(scope);
-    }
-
-    /**
-     * Returns a scope description for a given scope name. This is a convenience method for <code>getScopes().get(name)</code>
-     * 
-     * @param scope the name of a scope.
-     * @return the corresponding description or null.
-     */
-    default String getScope(String scope) {
-        Map<String, String> map = getScopes();
-        if (map == null) {
-            return null;
-        }
-        return map.get(scope);
-    }
-
-}
\ No newline at end of file


Test patch:
diff --git a/tck/src/main/java/org/eclipse/microprofile/openapi/tck/ModelConstructionTest.java b/tck/src/main/java/org/eclipse/microprofile/openapi/tck/ModelConstructionTest.java
index 6df6a5a..8f06e23 100644
--- a/tck/src/main/java/org/eclipse/microprofile/openapi/tck/ModelConstructionTest.java
+++ b/tck/src/main/java/org/eclipse/microprofile/openapi/tck/ModelConstructionTest.java
@@ -69,7 +69,6 @@ import org.eclipse.microprofile.openapi.models.responses.APIResponse;
 import org.eclipse.microprofile.openapi.models.responses.APIResponses;
 import org.eclipse.microprofile.openapi.models.security.OAuthFlow;
 import org.eclipse.microprofile.openapi.models.security.OAuthFlows;
-import org.eclipse.microprofile.openapi.models.security.Scopes;
 import org.eclipse.microprofile.openapi.models.security.SecurityRequirement;
 import org.eclipse.microprofile.openapi.models.security.SecurityScheme;
 import org.eclipse.microprofile.openapi.models.servers.Server;
@@ -1154,7 +1153,17 @@ public class ModelConstructionTest extends Arquillian {
     
     @Test
     public void oAuthFlowTest() {
-        processConstructible(OAuthFlow.class);
+        final OAuthFlow o = processConstructible(OAuthFlow.class);
+        final String key = "myKey";
+        final String value = new String("myValue");
+        o.setScopes(Collections.singletonMap(key, value));
+        Map<String,String> scopes = o.getScopes();
+        assertEquals(scopes.size(), 1, "The list is expected to contain one entry.");
+        assertTrue(scopes.containsKey("myKey"), "The map is expected to contain a 'myKey' entry.");
+        assertEquals(scopes.get(key), value, "The value corresponding to the 'myKey' is wrong.");
+        
+        o.setScopes((Map<String, String>) null);
+        assertNull(o.getScopes(), "The value is expected to be null.");
     }
     
     @Test
@@ -1162,46 +1171,6 @@ public class ModelConstructionTest extends Arquillian {
         processConstructible(OAuthFlows.class);
     }
     
-    @Test
-    public void scopesTest() {
-        final Scopes s = processConstructible(Scopes.class);
-        
-        final String scopeKey = "myScope";
-        final String scopeValue = new String("myDescription");
-        s.setScopes(Collections.singletonMap(scopeKey, scopeValue));
-        assertTrue(s.hasScope(scopeKey), scopeKey + " is present in the map");
-        assertEquals(s.getScopes().size(), 1, "The map is expected to contain one entry.");
-        assertSame(s.getScope(scopeKey), scopeValue, 
-                "The value associated with the key: " + scopeKey + " is expected to be the same one that was added.");
-        checkMapEntry(s.getScopes(), scopeKey, scopeValue);
-        
-        final String scopeKey2 = "myScope2";
-        assertFalse(s.hasScope(scopeKey2), scopeKey2 + " is absent in the map");
-        final String scopeValue2 = new String("myDescription2");
-        checkSameObject(s, s.addScope(scopeKey2, scopeValue2));
-        assertTrue(s.hasScope(scopeKey2), scopeKey2 + " is present in the map");
-        assertEquals(s.getScopes().size(), 2, "The map is expected to contain two entries.");
-        assertSame(s.getScope(scopeKey2), scopeValue2, 
-                "The value associated with the key: " + scopeKey2 + " is expected to be the same one that was added.");
-        checkMapEntry(s.getScopes(), scopeKey2, scopeValue2);
-        
-        s.removeScope(scopeKey);
-        assertFalse(s.hasScope(scopeKey), scopeKey + " is absent in the map");
-        assertEquals(s.getScopes().size(), 1, "The map is expected to contain one entry.");
-        
-        s.removeScope(scopeKey2);
-        assertFalse(s.hasScope(scopeKey2), scopeKey + " is absent in the map");
-        assertEquals(s.getScopes().size(), 0, "The map is expected to contain 0 entries.");
-        
-        final String otherValue = new String("otherDescription");
-        checkMapImmutable(s, Scopes::getScopes, "otherScope", otherValue);
-        
-        String scopeKey3 = "myScope3";
-        s.addScope(scopeKey3, null);
-        assertTrue(s.hasScope(scopeKey3), "Expected " + scopeKey3 + " to be present in the map");
-        assertNull(s.getScope(scopeKey3), "Expected the value of " + scopeKey3 + " to be null.");
-    }
-    
     @Test
     public void securityRequirementTest() {
         final SecurityRequirement sr = processConstructible(SecurityRequirement.class);