Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix JSON decoding error when empty optional is returned. #16

Merged
merged 5 commits into from
Sep 10, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ public PythonClass generateClient(
}
return false;
}).orElse(false))
.isOptionalReturnType(ed.getReturns()
.map(rt -> rt.accept(TypeVisitor.IS_OPTIONAL))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't work on the test-case for this in conjure-verification 0.10.0 because it is an alias to an optional type. This type visitor is used in a handful of places to determine whether or not a value is optional. Should the visitor also return true if the type is an alias to an optional? Should there be a separate visitor which handles the alias case?

  {
    "type" : "alias",
    "alias" : {
      "typeName" : {
        "name" : "RawOptionalExample",
        "package" : "com.palantir.conjure.verification"
      },
      "alias" : {
        "type" : "optional",
        "optional" : {
          "itemType" : {
            "type" : "primitive",
            "primitive" : "INTEGER"
          }
        }
      }
    }
  }

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ferozco @iamdanfox I'm not actually sure what the right thing to do is

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We encountered something similar in conjure-core and made: https://github.com/palantir/conjure/blob/develop/conjure-core/src/main/java/com/palantir/conjure/defs/DealiasingTypeVisitor.java . Perhaps we can factor that out into the generator commons and you can then use it here

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 would really like to see that in generator-commons

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

4.2.0 is out with the new visitor

.orElse(false))
.build();
})
.collect(Collectors.toList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public interface PythonEndpointDefinition extends Emittable {

boolean isBinary();

boolean isOptionalReturnType();

Optional<String> pythonReturnType();

Optional<String> myPyReturnType();
Expand All @@ -63,6 +65,7 @@ default void check() {
"expected both return types or neither");
}

@SuppressWarnings("checkstyle:CyclomaticComplexity")
@Override
default void emit(PythonPoetWriter poetWriter) {
poetWriter.maintainingIndent(() -> {
Expand Down Expand Up @@ -192,7 +195,14 @@ default void emit(PythonPoetWriter poetWriter) {
poetWriter.writeIndentedLine("return _raw");
} else if (pythonReturnType().isPresent()) {
poetWriter.writeIndentedLine("_decoder = ConjureDecoder()");
poetWriter.writeIndentedLine("return _decoder.decode(_response.json(), %s)", pythonReturnType().get());
if (isOptionalReturnType()) {
poetWriter.writeIndentedLine(
"return None if _response.status_code == 204 else _decoder.decode(_response.json(), %s)",
pythonReturnType().get());
} else {
poetWriter.writeIndentedLine("return _decoder.decode(_response.json(), %s)",
pythonReturnType().get());
}
} else {
poetWriter.writeIndentedLine("return");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def get_dataset(self, auth_header, dataset_rid):
json=_json)

_decoder = ConjureDecoder()
return _decoder.decode(_response.json(), OptionalType(Dataset))
return None if _response.status_code == 204 else _decoder.decode(_response.json(), OptionalType(Dataset))

def get_raw_data(self, auth_header, dataset_rid):
# type: (str, str) -> Any
Expand Down Expand Up @@ -165,7 +165,7 @@ def maybe_get_raw_data(self, auth_header, dataset_rid):
json=_json)

_decoder = ConjureDecoder()
return _decoder.decode(_response.json(), OptionalType(BinaryType()))
return None if _response.status_code == 204 else _decoder.decode(_response.json(), OptionalType(BinaryType()))

def upload_raw_data(self, auth_header, input):
# type: (str, Any) -> None
Expand Down Expand Up @@ -288,7 +288,7 @@ def resolve_branch(self, auth_header, branch, dataset_rid):
json=_json)

_decoder = ConjureDecoder()
return _decoder.decode(_response.json(), OptionalType(str))
return None if _response.status_code == 204 else _decoder.decode(_response.json(), OptionalType(str))

def test_param(self, auth_header, dataset_rid):
# type: (str, str) -> Optional[str]
Expand Down Expand Up @@ -318,7 +318,7 @@ def test_param(self, auth_header, dataset_rid):
json=_json)

_decoder = ConjureDecoder()
return _decoder.decode(_response.json(), OptionalType(str))
return None if _response.status_code == 204 else _decoder.decode(_response.json(), OptionalType(str))

def test_query_params(self, auth_header, implicit, something):
# type: (str, str, str) -> int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def get_dataset(self, auth_header, dataset_rid):
json=_json)

_decoder = ConjureDecoder()
return _decoder.decode(_response.json(), OptionalType(Dataset))
return None if _response.status_code == 204 else _decoder.decode(_response.json(), OptionalType(Dataset))

def get_raw_data(self, auth_header, dataset_rid):
# type: (str, str) -> Any
Expand Down Expand Up @@ -165,7 +165,7 @@ def maybe_get_raw_data(self, auth_header, dataset_rid):
json=_json)

_decoder = ConjureDecoder()
return _decoder.decode(_response.json(), OptionalType(BinaryType()))
return None if _response.status_code == 204 else _decoder.decode(_response.json(), OptionalType(BinaryType()))

def upload_raw_data(self, auth_header, input):
# type: (str, Any) -> None
Expand Down Expand Up @@ -288,7 +288,7 @@ def resolve_branch(self, auth_header, branch, dataset_rid):
json=_json)

_decoder = ConjureDecoder()
return _decoder.decode(_response.json(), OptionalType(str))
return None if _response.status_code == 204 else _decoder.decode(_response.json(), OptionalType(str))

def test_param(self, auth_header, dataset_rid):
# type: (str, str) -> Optional[str]
Expand Down Expand Up @@ -318,7 +318,7 @@ def test_param(self, auth_header, dataset_rid):
json=_json)

_decoder = ConjureDecoder()
return _decoder.decode(_response.json(), OptionalType(str))
return None if _response.status_code == 204 else _decoder.decode(_response.json(), OptionalType(str))

def test_query_params(self, auth_header, implicit, something):
# type: (str, str, str) -> int
Expand Down