diff --git a/.openapi-generator/FILES b/.openapi-generator/FILES index 57f091d..f828c98 100644 --- a/.openapi-generator/FILES +++ b/.openapi-generator/FILES @@ -16,6 +16,7 @@ api/openapi.yaml build.gradle docs/Any.md docs/Assertion.md +docs/AssertionTupleKey.md docs/AuthorizationModel.md docs/CheckRequest.md docs/CheckRequestTupleKey.md @@ -146,6 +147,7 @@ src/main/java/dev/openfga/sdk/api/configuration/CredentialsMethod.java src/main/java/dev/openfga/sdk/api/model/AbstractOpenApiSchema.java src/main/java/dev/openfga/sdk/api/model/Any.java src/main/java/dev/openfga/sdk/api/model/Assertion.java +src/main/java/dev/openfga/sdk/api/model/AssertionTupleKey.java src/main/java/dev/openfga/sdk/api/model/AuthorizationModel.java src/main/java/dev/openfga/sdk/api/model/CheckRequest.java src/main/java/dev/openfga/sdk/api/model/CheckRequestTupleKey.java @@ -222,6 +224,7 @@ src/main/java/dev/openfga/sdk/util/StringUtil.java src/test-integration/java/dev/openfga/sdk/api/OpenFgaApiIntegrationTest.java src/test-integration/java/dev/openfga/sdk/api/client/OpenFgaClientIntegrationTest.java src/test-integration/java/package-info.java +src/test-integration/resources/auth-model.json src/test/java/dev/openfga/sdk/api/OpenFgaApiTest.java src/test/java/dev/openfga/sdk/api/OpenFgaApiTest.java src/test/java/dev/openfga/sdk/api/auth/OAuth2ClientTest.java @@ -230,6 +233,7 @@ src/test/java/dev/openfga/sdk/api/configuration/ClientCredentialsTest.java src/test/java/dev/openfga/sdk/api/configuration/ConfigurationTest.java src/test/java/dev/openfga/sdk/api/model/AnyTest.java src/test/java/dev/openfga/sdk/api/model/AssertionTest.java +src/test/java/dev/openfga/sdk/api/model/AssertionTupleKeyTest.java src/test/java/dev/openfga/sdk/api/model/AuthorizationModelTest.java src/test/java/dev/openfga/sdk/api/model/CheckRequestTest.java src/test/java/dev/openfga/sdk/api/model/CheckRequestTupleKeyTest.java diff --git a/README.md b/README.md index de21721..0335407 100644 --- a/README.md +++ b/README.md @@ -740,6 +740,8 @@ fgaClient.writeAssertions(assertions, options).get(); - [Assertion](https://github.com/openfga/java-sdk/blob/main/docs/Assertion.md) +- [AssertionTupleKey](https://github.com/openfga/java-sdk/blob/main/docs/AssertionTupleKey.md) + - [AuthorizationModel](https://github.com/openfga/java-sdk/blob/main/docs/AuthorizationModel.md) - [CheckRequest](https://github.com/openfga/java-sdk/blob/main/docs/CheckRequest.md) diff --git a/docs/Assertion.md b/docs/Assertion.md index b1513a0..704cc8e 100644 --- a/docs/Assertion.md +++ b/docs/Assertion.md @@ -7,7 +7,7 @@ | Name | Type | Description | Notes | |------------ | ------------- | ------------- | -------------| -|**tupleKey** | [**CheckRequestTupleKey**](CheckRequestTupleKey.md) | | | +|**tupleKey** | [**AssertionTupleKey**](AssertionTupleKey.md) | | | |**expectation** | **Boolean** | | | diff --git a/docs/AssertionTupleKey.md b/docs/AssertionTupleKey.md new file mode 100644 index 0000000..a38f7dd --- /dev/null +++ b/docs/AssertionTupleKey.md @@ -0,0 +1,15 @@ + + +# AssertionTupleKey + + +## Properties + +| Name | Type | Description | Notes | +|------------ | ------------- | ------------- | -------------| +|**_object** | **String** | | | +|**relation** | **String** | | | +|**user** | **String** | | | + + + diff --git a/src/main/java/dev/openfga/sdk/api/client/ClientAssertion.java b/src/main/java/dev/openfga/sdk/api/client/ClientAssertion.java index 8618fe1..527f8d3 100644 --- a/src/main/java/dev/openfga/sdk/api/client/ClientAssertion.java +++ b/src/main/java/dev/openfga/sdk/api/client/ClientAssertion.java @@ -13,7 +13,7 @@ package dev.openfga.sdk.api.client; import dev.openfga.sdk.api.model.Assertion; -import dev.openfga.sdk.api.model.CheckRequestTupleKey; +import dev.openfga.sdk.api.model.AssertionTupleKey; import java.util.List; import java.util.stream.Collectors; @@ -72,7 +72,7 @@ public boolean getExpectation() { } public Assertion asAssertion() { - var tupleKey = new CheckRequestTupleKey().user(user).relation(relation)._object(_object); + var tupleKey = new AssertionTupleKey().user(user).relation(relation)._object(_object); return new Assertion().tupleKey(tupleKey).expectation(expectation); } diff --git a/src/main/java/dev/openfga/sdk/api/model/Assertion.java b/src/main/java/dev/openfga/sdk/api/model/Assertion.java index 0f1f3b5..e68236f 100644 --- a/src/main/java/dev/openfga/sdk/api/model/Assertion.java +++ b/src/main/java/dev/openfga/sdk/api/model/Assertion.java @@ -26,14 +26,14 @@ @JsonPropertyOrder({Assertion.JSON_PROPERTY_TUPLE_KEY, Assertion.JSON_PROPERTY_EXPECTATION}) public class Assertion { public static final String JSON_PROPERTY_TUPLE_KEY = "tuple_key"; - private CheckRequestTupleKey tupleKey; + private AssertionTupleKey tupleKey; public static final String JSON_PROPERTY_EXPECTATION = "expectation"; private Boolean expectation; public Assertion() {} - public Assertion tupleKey(CheckRequestTupleKey tupleKey) { + public Assertion tupleKey(AssertionTupleKey tupleKey) { this.tupleKey = tupleKey; return this; } @@ -45,13 +45,13 @@ public Assertion tupleKey(CheckRequestTupleKey tupleKey) { @javax.annotation.Nonnull @JsonProperty(JSON_PROPERTY_TUPLE_KEY) @JsonInclude(value = JsonInclude.Include.ALWAYS) - public CheckRequestTupleKey getTupleKey() { + public AssertionTupleKey getTupleKey() { return tupleKey; } @JsonProperty(JSON_PROPERTY_TUPLE_KEY) @JsonInclude(value = JsonInclude.Include.ALWAYS) - public void setTupleKey(CheckRequestTupleKey tupleKey) { + public void setTupleKey(AssertionTupleKey tupleKey) { this.tupleKey = tupleKey; } diff --git a/src/main/java/dev/openfga/sdk/api/model/AssertionTupleKey.java b/src/main/java/dev/openfga/sdk/api/model/AssertionTupleKey.java new file mode 100644 index 0000000..8869d9c --- /dev/null +++ b/src/main/java/dev/openfga/sdk/api/model/AssertionTupleKey.java @@ -0,0 +1,217 @@ +/* + * OpenFGA + * A high performance and flexible authorization/permission engine built for developers and inspired by Google Zanzibar. + * + * The version of the OpenAPI document: 0.1 + * Contact: community@openfga.dev + * + * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). + * https://openapi-generator.tech + * Do not edit the class manually. + */ + +package dev.openfga.sdk.api.model; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonPropertyOrder; +import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; +import java.util.Objects; +import java.util.StringJoiner; + +/** + * AssertionTupleKey + */ +@JsonPropertyOrder({ + AssertionTupleKey.JSON_PROPERTY_OBJECT, + AssertionTupleKey.JSON_PROPERTY_RELATION, + AssertionTupleKey.JSON_PROPERTY_USER +}) +public class AssertionTupleKey { + public static final String JSON_PROPERTY_OBJECT = "object"; + private String _object; + + public static final String JSON_PROPERTY_RELATION = "relation"; + private String relation; + + public static final String JSON_PROPERTY_USER = "user"; + private String user; + + public AssertionTupleKey() {} + + public AssertionTupleKey _object(String _object) { + this._object = _object; + return this; + } + + /** + * Get _object + * @return _object + **/ + @javax.annotation.Nonnull + @JsonProperty(JSON_PROPERTY_OBJECT) + @JsonInclude(value = JsonInclude.Include.ALWAYS) + public String getObject() { + return _object; + } + + @JsonProperty(JSON_PROPERTY_OBJECT) + @JsonInclude(value = JsonInclude.Include.ALWAYS) + public void setObject(String _object) { + this._object = _object; + } + + public AssertionTupleKey relation(String relation) { + this.relation = relation; + return this; + } + + /** + * Get relation + * @return relation + **/ + @javax.annotation.Nonnull + @JsonProperty(JSON_PROPERTY_RELATION) + @JsonInclude(value = JsonInclude.Include.ALWAYS) + public String getRelation() { + return relation; + } + + @JsonProperty(JSON_PROPERTY_RELATION) + @JsonInclude(value = JsonInclude.Include.ALWAYS) + public void setRelation(String relation) { + this.relation = relation; + } + + public AssertionTupleKey user(String user) { + this.user = user; + return this; + } + + /** + * Get user + * @return user + **/ + @javax.annotation.Nonnull + @JsonProperty(JSON_PROPERTY_USER) + @JsonInclude(value = JsonInclude.Include.ALWAYS) + public String getUser() { + return user; + } + + @JsonProperty(JSON_PROPERTY_USER) + @JsonInclude(value = JsonInclude.Include.ALWAYS) + public void setUser(String user) { + this.user = user; + } + + /** + * Return true if this AssertionTupleKey object is equal to o. + */ + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + AssertionTupleKey assertionTupleKey = (AssertionTupleKey) o; + return Objects.equals(this._object, assertionTupleKey._object) + && Objects.equals(this.relation, assertionTupleKey.relation) + && Objects.equals(this.user, assertionTupleKey.user); + } + + @Override + public int hashCode() { + return Objects.hash(_object, relation, user); + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("class AssertionTupleKey {\n"); + sb.append(" _object: ").append(toIndentedString(_object)).append("\n"); + sb.append(" relation: ").append(toIndentedString(relation)).append("\n"); + sb.append(" user: ").append(toIndentedString(user)).append("\n"); + sb.append("}"); + return sb.toString(); + } + + /** + * Convert the given object to string with each line indented by 4 spaces + * (except the first line). + */ + private String toIndentedString(Object o) { + if (o == null) { + return "null"; + } + return o.toString().replace("\n", "\n "); + } + + /** + * Convert the instance into URL query string. + * + * @return URL query string + */ + public String toUrlQueryString() { + return toUrlQueryString(null); + } + + /** + * Convert the instance into URL query string. + * + * @param prefix prefix of the query string + * @return URL query string + */ + public String toUrlQueryString(String prefix) { + String suffix = ""; + String containerSuffix = ""; + String containerPrefix = ""; + if (prefix == null) { + // style=form, explode=true, e.g. /pet?name=cat&type=manx + prefix = ""; + } else { + // deepObject style e.g. /pet?id[name]=cat&id[type]=manx + prefix = prefix + "["; + suffix = "]"; + containerSuffix = "]"; + containerPrefix = "["; + } + + StringJoiner joiner = new StringJoiner("&"); + + // add `object` to the URL query string + if (getObject() != null) { + joiner.add(String.format( + "%sobject%s=%s", + prefix, + suffix, + URLEncoder.encode(String.valueOf(getObject()), StandardCharsets.UTF_8) + .replaceAll("\\+", "%20"))); + } + + // add `relation` to the URL query string + if (getRelation() != null) { + joiner.add(String.format( + "%srelation%s=%s", + prefix, + suffix, + URLEncoder.encode(String.valueOf(getRelation()), StandardCharsets.UTF_8) + .replaceAll("\\+", "%20"))); + } + + // add `user` to the URL query string + if (getUser() != null) { + joiner.add(String.format( + "%suser%s=%s", + prefix, + suffix, + URLEncoder.encode(String.valueOf(getUser()), StandardCharsets.UTF_8) + .replaceAll("\\+", "%20"))); + } + + return joiner.toString(); + } +} diff --git a/src/test-integration/java/dev/openfga/sdk/api/OpenFgaApiIntegrationTest.java b/src/test-integration/java/dev/openfga/sdk/api/OpenFgaApiIntegrationTest.java index 8535a8a..8c75ae7 100644 --- a/src/test-integration/java/dev/openfga/sdk/api/OpenFgaApiIntegrationTest.java +++ b/src/test-integration/java/dev/openfga/sdk/api/OpenFgaApiIntegrationTest.java @@ -296,7 +296,7 @@ public void write_readAssertions() throws Exception { String authModelId = writeAuthModel(storeId); WriteAssertionsRequest writeRequest = new WriteAssertionsRequest() .assertions(List.of(new Assertion() - .tupleKey(new CheckRequestTupleKey() + .tupleKey(new AssertionTupleKey() .user(DEFAULT_USER) .relation("reader") ._object(DEFAULT_DOC)) @@ -310,7 +310,7 @@ public void write_readAssertions() throws Exception { // Then String responseJson = mapper.writeValueAsString(response.getAssertions()); assertEquals( - "[{\"tuple_key\":{\"user\":\"user:81684243-9356-4421-8fbf-a4f8d36aa31b\",\"relation\":\"reader\",\"object\":\"document:2021-budget\"},\"expectation\":true}]", + "[{\"tuple_key\":{\"object\":\"document:2021-budget\",\"relation\":\"reader\",\"user\":\"user:81684243-9356-4421-8fbf-a4f8d36aa31b\"},\"expectation\":true}]", responseJson); } diff --git a/src/test-integration/java/dev/openfga/sdk/api/client/OpenFgaClientIntegrationTest.java b/src/test-integration/java/dev/openfga/sdk/api/client/OpenFgaClientIntegrationTest.java index 9a3ce51..98c9021 100644 --- a/src/test-integration/java/dev/openfga/sdk/api/client/OpenFgaClientIntegrationTest.java +++ b/src/test-integration/java/dev/openfga/sdk/api/client/OpenFgaClientIntegrationTest.java @@ -322,7 +322,7 @@ public void write_readAssertions() throws Exception { // Then String responseJson = mapper.writeValueAsString(response.getAssertions()); assertEquals( - "[{\"tuple_key\":{\"user\":\"user:81684243-9356-4421-8fbf-a4f8d36aa31b\",\"relation\":\"reader\",\"object\":\"document:2021-budget\"},\"expectation\":true}]", + "[{\"tuple_key\":{\"object\":\"document:2021-budget\",\"relation\":\"reader\",\"user\":\"user:81684243-9356-4421-8fbf-a4f8d36aa31b\"},\"expectation\":true}]", responseJson); } diff --git a/src/test-integration/resources/auth-model.json b/src/test-integration/resources/auth-model.json index f720d3c..3ef492c 100644 --- a/src/test-integration/resources/auth-model.json +++ b/src/test-integration/resources/auth-model.json @@ -1,66 +1,67 @@ { - "schema_version": "1.1", - "type_definitions": [ - { - "type": "user" - }, - { - "type": "document", - "relations": { - "reader": { - "this": {} - }, - "writer": { - "this": {} - }, - "owner": { - "this": {} - } + "schema_version": "1.1", + "type_definitions": [ + { + "type": "user" }, - "metadata": { + { + "type": "document", "relations": { "reader": { - "directly_related_user_types": [ - { - "type": "user" - } - ] + "this": {} }, "writer": { - "directly_related_user_types": [ - { - "type": "user" - } - ] + "this": {} }, "owner": { - "directly_related_user_types": [ - { - "type": "user" - } - ] - }, - "conditional_reader": { - "directly_related_user_types": [ - { - "condition": "name_starts_with_a", - "type": "user" - } - ] + "this": {} + } + }, + "metadata": { + "relations": { + "reader": { + "directly_related_user_types": [ + { + "type": "user" + } + ] + }, + "writer": { + "directly_related_user_types": [ + { + "type": "user" + } + ] + }, + "owner": { + "directly_related_user_types": [ + { + "type": "user" + } + ] + }, + "conditional_reader": { + "directly_related_user_types": [ + { + "condition": "name_starts_with_a", + "type": "user" + } + ] + } } } } - } - ], - "conditions": { - "name_starts_with_a": { - "name": "name_starts_with_a", - "expression": "name.startsWith(\"a\")", - "parameters": { - "name": { - "type_name": "TYPE_NAME_STRING" + ], + "conditions": { + "name_starts_with_a": { + "name": "name_starts_with_a", + "expression": "name.startsWith(\"a\")", + "parameters": { + "name": { + "type_name": "TYPE_NAME_STRING" + } } } } } -} + \ No newline at end of file diff --git a/src/test/java/dev/openfga/sdk/api/OpenFgaApiTest.java b/src/test/java/dev/openfga/sdk/api/OpenFgaApiTest.java index 8de3bbf..df99673 100644 --- a/src/test/java/dev/openfga/sdk/api/OpenFgaApiTest.java +++ b/src/test/java/dev/openfga/sdk/api/OpenFgaApiTest.java @@ -1815,12 +1815,12 @@ public void writeAssertionsTest() throws Exception { // Given String putUrl = "https://localhost/stores/01YCP46JKYM8FJCQ37NMBYHE5X/assertions/01G5JAVJ41T49E9TT3SKVS7X1J"; String expectedBody = String.format( - "{\"assertions\":[{\"tuple_key\":{\"user\":\"%s\",\"relation\":\"%s\",\"object\":\"%s\"},\"expectation\":true}]}", - DEFAULT_USER, DEFAULT_RELATION, DEFAULT_OBJECT); + "{\"assertions\":[{\"tuple_key\":{\"object\":\"%s\",\"relation\":\"%s\",\"user\":\"%s\"},\"expectation\":true}]}", + DEFAULT_OBJECT, DEFAULT_RELATION, DEFAULT_USER); mockHttpClient.onPut(putUrl).withBody(is(expectedBody)).doReturn(200, EMPTY_RESPONSE_BODY); WriteAssertionsRequest request = new WriteAssertionsRequest() .assertions(List.of(new Assertion() - .tupleKey(new CheckRequestTupleKey() + .tupleKey(new AssertionTupleKey() ._object(DEFAULT_OBJECT) .relation(DEFAULT_RELATION) .user(DEFAULT_USER)) diff --git a/src/test/java/dev/openfga/sdk/api/client/OpenFgaClientTest.java b/src/test/java/dev/openfga/sdk/api/client/OpenFgaClientTest.java index 619e25a..a113a7d 100644 --- a/src/test/java/dev/openfga/sdk/api/client/OpenFgaClientTest.java +++ b/src/test/java/dev/openfga/sdk/api/client/OpenFgaClientTest.java @@ -2217,8 +2217,8 @@ public void writeAssertionsTest() throws Exception { String putUrl = String.format("https://localhost/stores/%s/assertions/%s", DEFAULT_STORE_ID, DEFAULT_AUTH_MODEL_ID); String expectedBody = String.format( - "{\"assertions\":[{\"tuple_key\":{\"user\":\"%s\",\"relation\":\"%s\",\"object\":\"%s\"},\"expectation\":true}]}", - DEFAULT_USER, DEFAULT_RELATION, DEFAULT_OBJECT); + "{\"assertions\":[{\"tuple_key\":{\"object\":\"%s\",\"relation\":\"%s\",\"user\":\"%s\"},\"expectation\":true}]}", + DEFAULT_OBJECT, DEFAULT_RELATION, DEFAULT_USER); mockHttpClient.onPut(putUrl).withBody(is(expectedBody)).doReturn(200, EMPTY_RESPONSE_BODY); List assertions = List.of(new ClientAssertion() .user(DEFAULT_USER) diff --git a/src/test/java/dev/openfga/sdk/api/model/AssertionTupleKeyTest.java b/src/test/java/dev/openfga/sdk/api/model/AssertionTupleKeyTest.java new file mode 100644 index 0000000..1340c16 --- /dev/null +++ b/src/test/java/dev/openfga/sdk/api/model/AssertionTupleKeyTest.java @@ -0,0 +1,54 @@ +/* + * OpenFGA + * A high performance and flexible authorization/permission engine built for developers and inspired by Google Zanzibar. + * + * The version of the OpenAPI document: 0.1 + * Contact: community@openfga.dev + * + * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). + * https://openapi-generator.tech + * Do not edit the class manually. + */ + +package dev.openfga.sdk.api.model; + +import org.junit.jupiter.api.Test; + +/** + * Model tests for AssertionTupleKey + */ +public class AssertionTupleKeyTest { + private final AssertionTupleKey model = new AssertionTupleKey(); + + /** + * Model tests for AssertionTupleKey + */ + @Test + public void testAssertionTupleKey() { + // TODO: test AssertionTupleKey + } + + /** + * Test the property '_object' + */ + @Test + public void _objectTest() { + // TODO: test _object + } + + /** + * Test the property 'relation' + */ + @Test + public void relationTest() { + // TODO: test relation + } + + /** + * Test the property 'user' + */ + @Test + public void userTest() { + // TODO: test user + } +}