Skip to content
This repository has been archived by the owner on Oct 23, 2024. It is now read-only.

Sth. wrong with MaxSegment and MinSegment in JSONPath #3066

Closed
sinojyj opened this issue Mar 18, 2020 · 1 comment
Closed

Sth. wrong with MaxSegment and MinSegment in JSONPath #3066

sinojyj opened this issue Mar 18, 2020 · 1 comment
Assignees
Labels
Milestone

Comments

@sinojyj
Copy link

sinojyj commented Mar 18, 2020

The inner class MaxSegment and MinSegment in JSONPath has a bug.
e.g. 1 there is a JSONString like this:
{ 'id' : 0, 'items' : [ {'name': 'apple', 'price' : 30 }, {'name': 'pear', 'price' : 40 } ] }
the Path String as this:
$.items[*].price.max()
exactly it should be "40" but throw a UnsupportedOperationException.

e.g. 2 when the path string change to $.items[*].price.size() , I got a correct result as "2".

so, I read the source codes like this:
` static class SizeSegment implements Segment {

    public final static SizeSegment instance = new SizeSegment();

    public Integer eval(JSONPath path, Object rootObject, Object currentObject) {
        return path.evalSize(currentObject);
    }

    public void extract(JSONPath path, DefaultJSONParser parser, Context context) {
        throw new UnsupportedOperationException();
    }
}`

`static class MaxSegment implements Segment {

    public final static MaxSegment instance = new MaxSegment();

    public Object eval(JSONPath path, Object rootObject, Object currentObject) {
        Object max = null;
        if (rootObject instanceof Collection) {
            Iterator iterator = ((Collection) rootObject).iterator();
            while (iterator.hasNext()) {
                Object next = iterator.next();
                if (next == null) {
                    continue;
                }

                if (max == null) {
                    max = next;
                } else if (compare(max, next) < 0) {
                    max = next;
                }
            }
        } else {
            throw new UnsupportedOperationException();
        }

        return max;
    }

    public void extract(JSONPath path, DefaultJSONParser parser, Context context) {
        throw new UnsupportedOperationException();
    }
}`

In class SizeSegment,the eval function use "currentObject" , it was $.items[*].price .
In class MaxSegment and MinSegment , they use "rootObject" , it was $.

so, Is it a bug?

@wenshao wenshao added this to the 1.2.68 milestone Mar 22, 2020
@wenshao wenshao self-assigned this Mar 22, 2020
@wenshao wenshao added the bug label Mar 22, 2020
@wenshao
Copy link
Member

wenshao commented Mar 30, 2020

https://github.com/alibaba/fastjson/releases/tag/1.2.68
bug fixed, please use the new version

@wenshao wenshao closed this as completed Mar 30, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants