Code Monkey home page Code Monkey logo

Comments (1)

Rkuro avatar Rkuro commented on August 25, 2024

Reason this doesn't work today is due to the rules defined in the test-cases yml file: https://github.com/palantir/conjure-verification/blob/85ec2c15cde616415e79d6b3b03c34559e287ab0/master-test-cases.yml#L390

  - type: KebabCaseObjectExample
    positive:
        - '{"kebab-cased-field":1}'
    negative:
        - '{"kebabCasedField":1}'
        - '{"kebab_cased_field":1}'

For example:

Given a generated conjure bean like so:

class CreateDatasetRequest(ConjureBeanType):
    @classmethod
    def _fields(cls) -> Dict[str, ConjureFieldDefinition]:
        return {
            "file_system_id": ConjureFieldDefinition("fileSystemId", str),
            "path": ConjureFieldDefinition("path", str),
        }

    _file_system_id: str = None
    _path: str = None

    def __init__(self, file_system_id: str, path: str) -> None:
        self._file_system_id = file_system_id
        self._path = path

    @property
    def file_system_id(self) -> str:
        return self._file_system_id

    @property
    def path(self) -> str:
        return self._path

Called like this:

def test_object_with_hyphen():
    decoded = ConjureDecoder().decode({"file-system-id": "foo", "path": "bar"}, CreateDatasetRequest)
    assert decoded == CreateDatasetRequest("foo", "bar")

And this implementation:

@classmethod
    def decode_conjure_bean_type(cls, obj, conjure_type):
        """Decodes json into a conjure bean type (a plain bean, not enum
        or union).
        Args:
            obj: the json object to decode <----------------------------------- #  {"file-system-id": "foo", "path": "bar"}
            conjure_type: a class object which is the bean type
                we're decoding into
        Returns:
            A instance of a bean of type conjure_type.
        """
        deserialized: Dict[str, Any] = {}

        for (
            python_arg_name,    <----------------------------------- #  ends up being "'file_system_id'"
            field_definition,
        ) in conjure_type._fields().items():
            field_identifier = field_definition.identifier  <----------------------------------- # ends up being "'fileSystemId'

             # only checking the field_identifier means incorrectly flagging the valid hyphenated case here
            if field_identifier not in obj or obj[field_identifier] is None:
                # Logic passes here
                cls.check_null_field(
                    obj, deserialized, python_arg_name, field_definition
                )
            else:
                value = obj[field_identifier]
                field_type = field_definition.field_type
                deserialized[python_arg_name] = cls.do_decode(
                    value, field_type
                )
        return conjure_type(**deserialized)

which passes through to

@classmethod
    def check_null_field(
        cls, obj, deserialized, python_arg_name, field_definition
    ):
        type_origin = get_origin(field_definition.field_type)
        if isinstance(field_definition.field_type, ListType):
            deserialized[python_arg_name] = []
        elif isinstance(field_definition.field_type, DictType):
            deserialized[python_arg_name] = {}
        elif isinstance(field_definition.field_type, OptionalType):
            deserialized[python_arg_name] = None
        elif type_origin is OptionalTypeWrapper:
            deserialized[python_arg_name] = None
        elif type_origin is list:
            deserialized[python_arg_name] = []
        elif type_origin is dict:
            deserialized[python_arg_name] = {}
        else:
            raise Exception( <-----------------------------------  this is where we are throwing the exception
                "field {} not found in object {}".format(
                    field_definition.identifier, obj
                )
            )

This fails with the above exception:

Exception: field fileSystemId not found in object {'file-system-id': 'foo', 'path': 'bar'}

When it shouldn't given the conjure client generator separates it out with underscores already and is fairly straightforward to at least look for it.

"file_system_id": ConjureFieldDefinition("fileSystemId", str),

from conjure-python-client.

Related Issues (17)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.