This is an automated email from the ASF dual-hosted git repository.

jdaugherty pushed a commit to branch 9.0.x
in repository https://gitbox.apache.org/repos/asf/grails-core.git

commit 6fc2a3cbf0abd4f98cb5f0932d79e254f7e585a2
Merge: 8efcc85ea8 005dee1afc
Author: James Daugherty <[email protected]>
AuthorDate: Fri Oct 16 13:39:53 2026 -0400

    Merge branch '8.1.x' into 9.0.x

 .../web/json/PathCapturingJSONWriterWrapper.java   | 46 +++++++++++++---------
 .../org/grails/web/json/JSONWriterSpec.groovy      |  4 +-
 .../PathCapturingJSONWriterWrapperApiSpec.groovy   | 41 +++++++++----------
 3 files changed, 50 insertions(+), 41 deletions(-)

diff --cc 
grails-web-common/src/main/groovy/org/grails/web/json/PathCapturingJSONWriterWrapper.java
index 361352005e,61f340007e..17ce228add
--- 
a/grails-web-common/src/main/groovy/org/grails/web/json/PathCapturingJSONWriterWrapper.java
+++ 
b/grails-web-common/src/main/groovy/org/grails/web/json/PathCapturingJSONWriterWrapper.java
@@@ -54,35 -52,6 +54,35 @@@ public class PathCapturingJSONWriterWra
          return this;
      }
  
 +    @Override
 +    protected JSONWriter append(Writable writableValue) {
 +        if (log.isDebugEnabled()) {
-             if (debugCurrentStack) log.debug("{} > >> {}", 
delegate.mode.name(), getCurrentStrackReference());
++            if (debugCurrentStack) log.debug("{} > >> {}", 
delegate.mode.name(), getCurrentStackReference());
 +            log.debug("{} > append({})", delegate.mode.name(), writableValue);
 +        }
 +        delegate.append(writableValue);
 +        return this;
 +    }
 +
 +    @Override
 +    public void flush() {
 +        delegate.flush();
 +    }
 +
 +    @Override
 +    public void writeNested(Runnable writeValue) {
 +        Stack<PathElement> outerPath = pathStack;
 +        pathStack = new Stack<>();
 +        pathStack.addAll(outerPath);
 +        pathStack.push(new NestedElement(delegate.nestedPath()));
 +        try {
 +            delegate.writeNested(writeValue);
 +        }
 +        finally {
 +            pathStack = outerPath;
 +        }
 +    }
 +
      @Override
      public void comma() {
          if (log.isDebugEnabled()) {
@@@ -245,38 -214,6 +245,38 @@@
          return this;
      }
  
 +    @Override
 +    public JSONWriter value(Number number) {
 +        if (log.isDebugEnabled()) {
-             if (debugCurrentStack) log.debug("{} > >> {}", 
delegate.mode.name(), getCurrentStrackReference());
++            if (debugCurrentStack) log.debug("{} > >> {}", 
delegate.mode.name(), getCurrentStackReference());
 +            log.debug("{} > value(Number {})", delegate.mode.name(), number);
 +        }
 +        if (delegate.mode == Mode.ARRAY) {
 +            pushNextIndex();
 +        }
 +        else if (!pathStack.isEmpty()) {
 +            pathStack.pop();
 +        }
 +        delegate.value(number);
 +        return this;
 +    }
 +
 +    @Override
 +    public JSONWriter valueNull() {
 +        if (log.isDebugEnabled()) {
-             if (debugCurrentStack) log.debug("{} > >> {}", 
delegate.mode.name(), getCurrentStrackReference());
++            if (debugCurrentStack) log.debug("{} > >> {}", 
delegate.mode.name(), getCurrentStackReference());
 +            log.debug("{} > valueNull()", delegate.mode.name());
 +        }
 +        if (delegate.mode == Mode.ARRAY) {
 +            pushNextIndex();
 +        }
 +        else if (!pathStack.isEmpty()) {
 +            pathStack.pop();
 +        }
 +        delegate.valueNull();
 +        return this;
 +    }
 +
      @Override
      public JSONWriter value(Object o) {
          if (log.isDebugEnabled()) {
@@@ -363,4 -277,12 +363,12 @@@
          }
          return out.toString();
      }
+ 
+     /**
+      * @deprecated the name is misspelled, use {@link 
#getCurrentStackReference()}
+      */
+     @Deprecated
 -    public String getCurrentStrackReference() {
++    public String getCurrentStackReference() {
+         return getCurrentStackReference();
+     }
  }
diff --cc 
grails-web-common/src/test/groovy/org/grails/web/json/JSONWriterSpec.groovy
index 46e18d8b98,0000000000..8f8f6dd80c
mode 100644,000000..100644
--- 
a/grails-web-common/src/test/groovy/org/grails/web/json/JSONWriterSpec.groovy
+++ 
b/grails-web-common/src/test/groovy/org/grails/web/json/JSONWriterSpec.groovy
@@@ -1,332 -1,0 +1,332 @@@
 +/*
 + *  Licensed to the Apache Software Foundation (ASF) under one
 + *  or more contributor license agreements.  See the NOTICE file
 + *  distributed with this work for additional information
 + *  regarding copyright ownership.  The ASF licenses this file
 + *  to you 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
 + *
 + *    https://www.apache.org/licenses/LICENSE-2.0
 + *
 + *  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.grails.web.json
 +
 +import java.util.function.Predicate
 +
 +import groovy.transform.CompileStatic
 +import spock.lang.Issue
 +import spock.lang.Specification
 +import tools.jackson.core.JsonGenerator
 +import tools.jackson.databind.SerializationContext
 +import tools.jackson.databind.cfg.DateTimeFeature
 +import tools.jackson.databind.json.JsonMapper
 +import tools.jackson.databind.module.SimpleModule
 +import tools.jackson.databind.ser.std.StdSerializer
 +
 +import org.grails.buffer.StreamCharBuffer
 +
 +class JSONWriterSpec extends Specification {
 +
 +    StringWriter out = new StringWriter()
 +
 +    JsonMapperSupport jsonMapper = new 
JsonMapperSupport(JsonMapper.builder().build())
 +
 +    JSONWriter writer = new JSONWriter(out, jsonMapper, false)
 +
 +    JsonMapperSupport boxes = new JsonMapperSupport(JsonMapper.builder()
 +            .addModule(new SimpleModule().addSerializer(Box, new 
BoxSerializer())).build())
 +
 +    @Issue('GRAILS-10823')
 +    void 'Test rendering a forward slash'() {
 +        when:
 +        writer.object().key('namespace').value('alpha/beta').endObject()
 +
 +        then:
 +        '{"namespace":"alpha/beta"}' == out.toString()
 +    }
 +
 +    void 'should handle nulls'() {
 +        when:
 +        writer.array()
 +        writeNumber(writer, null)
 +        writeObject(writer, null)
 +        writer.endArray()
 +
 +        then:
 +        '[{"key":null},{"key":null}]' == out.toString()
 +    }
 +
 +    @CompileStatic
 +    private writeNumber(JSONWriter jsonWriter, Number n) {
 +        jsonWriter.object().key('key').value(n).endObject()
 +    }
 +
 +    @CompileStatic
 +    private writeObject(JSONWriter jsonWriter, Object o) {
 +        jsonWriter.object().key('key').value(o).endObject()
 +    }
 +
 +    void "writes objects, arrays and values"() {
 +        when:
 +        writer.object()
 +                .key('a').value(1L)
 +                
.key('b').array().value(true).value(2.5d).value('x').valueNull().endArray()
 +                .key('c').object().key('d').value(new JsonMapperValue(new 
Date(0L), jsonMapper)).endObject()
 +                .endObject()
 +
 +        then: 'the complete text is flushed'
 +        out.toString() == 
'{"a":1,"b":[true,2.5,"x",null],"c":{"d":"1970-01-01T00:00:00.000Z"}}'
 +    }
 +
 +    void "rejects #description"() {
 +        when:
 +        calls.call(writer)
 +
 +        then:
 +        def e = thrown(JSONException)
 +        e.message.startsWith(message)
 +
 +        where:
 +        description               | calls                                     
        || message
 +        'a key outside an object' | { JSONWriter w -> w.array().key('a') }    
        || 'Misplaced key'
 +        'a value without a key'   | { JSONWriter w -> w.object().value('a') } 
        || 'Value out of sequence'
 +        'a null key'              | { JSONWriter w -> w.object().key(null) }  
        || 'Null key.'
 +        'an unbalanced end'       | { JSONWriter w -> w.object().endArray() } 
        || 'Misplaced endArray.'
 +    }
 +
 +    void "a PathCapturingJSONWriterWrapper can wrap it"() {
 +        given:
 +        def wrapper = new PathCapturingJSONWriterWrapper(writer)
 +
 +        when:
 +        wrapper.object().key('list').array().object().key('a')
-         def path = wrapper.currentStrackReference
++        def path = wrapper.currentStackReference
 +        wrapper.value(1L).endObject().endArray().endObject()
 +
 +        then:
 +        path == '.list[0].a'
 +        out.toString() == '{"list":[{"a":1}]}'
 +    }
 +
 +    void "a GString and a StreamCharBuffer are escaped for an HTML script 
element, as in Grails 8"() {
 +        given:
 +        def buffer = new StreamCharBuffer()
 +        buffer.writer.write('</b>
')
 +
 +        when:
 +        writer.array().value("</a>${1}
").value(buffer).value('</c>

').endArray()
 +
 +        then: 'by the Grails JavaScript encoder, as JSONWriter quoted them in 
Grails 8, and a String by the generator'
 +        out.toString() == 
'["<\\/a>1\\u2028","<\\/b>\\u2028","<\\u002fc>\\u2028"]'
 +    }
 +
 +    void "a single value is a JSON text, written without a flush"() {
 +        when:
 +        new JSONWriter(out).value('x')
 +
 +        then:
 +        out.toString() == '"x"'
 +    }
 +
 +    void "flush() writes an incomplete text so far"() {
 +        when:
 +        writer.object().key('a')
 +
 +        then:
 +        out.toString() == ''
 +
 +        when:
 +        writer.flush()
 +
 +        then:
 +        out.toString() == '{"a"'
 +    }
 +
 +    void "the Writer is neither flushed nor closed"() {
 +        given:
 +        def calls = []
 +        def target = new StringWriter() {
 +            void flush() { calls << 'flush'; super.flush() }
 +            void close() { calls << 'close'; super.close() }
 +        }
 +
 +        when:
 +        def jsonWriter = new JSONWriter(target, jsonMapper, false)
 +        jsonWriter.object().key('a').value(1L).endObject()
 +        jsonWriter.flush()
 +
 +        then:
 +        target.toString() == '{"a":1}'
 +        calls == []
 +    }
 +
 +    void "a JsonMapperValue is written by its own mapper"() {
 +        given:
 +        def timestamps = new 
JsonMapperSupport(JsonMapper.builder().enable(DateTimeFeature.WRITE_DATES_AS_TIMESTAMPS).build())
 +
 +        when:
 +        writer.array()
 +                .value(new JsonMapperValue(new Date(0L), jsonMapper))
 +                .value(new JsonMapperValue(new Date(0L), timestamps))
 +                .endArray()
 +
 +        then:
 +        out.toString() == '["1970-01-01T00:00:00.000Z",0]'
 +    }
 +
 +    void "a PrettyPrintJSONWriter indents as Grails 8 did"() {
 +        given:
 +        def writer = new PrettyPrintJSONWriter(out)
 +        def newline = PrettyPrintJSONWriter.NEWLINE
 +
 +        when:
 +        writer.object()
 +                .key('a').value(1)
 +                
.key('b').array().value(1).object().key('c').value('d').endObject().endArray()
 +                .key('e').object().endObject()
 +                .endObject()
 +
 +        then: 'an array that is not an element on a line of its own, and 
"key": value'
 +        out.toString() == ['{', '  "a": 1,', '  "b": ', '  [', '    1,', '    
{', '      "c": "d"', '    }', '  ],',
 +                           '  "e": {', '  }', '}'].join(newline)
 +    }
 +
 +    void "a PathCapturingJSONWriterWrapper passes flush() on"() {
 +        given:
 +        def wrapper = new PathCapturingJSONWriterWrapper(writer)
 +
 +        when:
 +        wrapper.object().key('a')
 +        wrapper.flush()
 +
 +        then:
 +        out.toString() == '{"a"'
 +    }
 +
 +    void "a JsonMapperValue is the JSON text the mapper writes"() {
 +        expect:
 +        new JsonMapperValue(new Date(0L), jsonMapper).toString() == 
'"1970-01-01T00:00:00.000Z"'
 +    }
 +
 +    void "a JSONElement is written as the JSON text it writes, and other 
objects as their quoted text"() {
 +        when:
 +        writer.array().value(new JSONObject([a: 1])).value(new 
URI('http://x/y')).endArray()
 +
 +        then:
 +        out.toString() == '[{"a":1},"http://x/y";]'
 +    }
 +
 +    void "a generator failure is reported as a JSONException"() {
 +        given:
 +        def failing = new JSONWriter(new Writer() {
 +            void write(char[] cbuf, int off, int len) { throw new 
IOException('closed') }
 +            void flush() { throw new IOException('closed') }
 +            void close() {}
 +        }, jsonMapper, false)
 +
 +        when:
 +        failing.object().key('a').value('x' * 10000).endObject()
 +
 +        then:
 +        thrown(JSONException)
 +    }
 +
 +    void "a value nested in a JsonMapperValue is written by its nested value 
writer, and the writer then carries on"() {
 +        given: 'a nested value writer for maps'
 +        def writer = new JSONWriter(out, boxes, false)
 +        def nested = { Object value ->
 +            if (value instanceof Map) {
 +                writer.writeNested { 
writer.object().key('size').value(value.size()).endObject() }
 +                return true
 +            }
 +            false
 +        } as Predicate<Object>
 +
 +        when:
 +        writer.object()
 +                .key('map').value(new JsonMapperValue(new Box(content: [a: 1, 
b: 2]), boxes, nested))
 +                .key('date').value(new JsonMapperValue(new Box(content: new 
Date(0L)), boxes, nested))
 +                .key('after').value(1)
 +                .endObject()
 +
 +        then: 'the map is written by the nested value writer, and the date by 
the mapper'
 +        out.toString() == 
'{"map":{"content":{"size":2}},"date":{"content":"1970-01-01T00:00:00.000Z"},"after":1}'
 +    }
 +
 +    void "a nested value writer must write one complete value"() {
 +        given:
 +        def writer = new JSONWriter(out, boxes, false)
 +        def nested = { Object value ->
 +            writer.writeNested { writer.object().key('a') }
 +            true
 +        } as Predicate<Object>
 +
 +        when:
 +        writer.object().key('box').value(new JsonMapperValue(new Box(content: 
[a: 1]), boxes, nested))
 +
 +        then:
 +        thrown(JSONException)
 +    }
 +
 +    void "a JsonMapperValue of another mapper is written without its nested 
value writer"() {
 +        given:
 +        def nested = { Object value -> throw new AssertionError("offered 
$value") } as Predicate<Object>
 +
 +        when:
 +        writer.array().value(new JsonMapperValue(new Box(content: [a: 1]), 
boxes, nested)).endArray()
 +
 +        then:
 +        out.toString() == '[{"content":{"a":1}}]'
 +    }
 +
 +    void "a PathCapturingJSONWriterWrapper tracks the path of a nested value 
through the mapper's output"() {
 +        given:
 +        def wrapper = new PathCapturingJSONWriterWrapper(new JSONWriter(out, 
boxes, false))
 +        String path = null
 +        def nested = { Object value ->
 +            wrapper.writeNested {
 +                wrapper.object().key('x')
-                 path = wrapper.currentStrackReference
++                path = wrapper.currentStackReference
 +                wrapper.value(1).endObject()
 +            }
 +            true
 +        } as Predicate<Object>
 +
 +        when:
 +        wrapper.object().key('list').array()
 +                .value(1)
 +                .value(new JsonMapperValue(new Box(content: [a: 1]), boxes, 
nested))
 +                .endArray()
 +                .endObject()
 +
 +        then:
 +        path == '.list[1].content.x'
 +        out.toString() == '{"list":[1,{"content":{"x":1}}]}'
 +    }
 +}
 +
 +class Box {
 +    Object content
 +}
 +
 +class BoxSerializer extends StdSerializer<Box> {
 +
 +    BoxSerializer() {
 +        super(Box)
 +    }
 +
 +    @Override
 +    void serialize(Box box, JsonGenerator generator, SerializationContext 
context) {
 +        generator.writeStartObject()
 +        generator.writeName('content')
 +        generator.writePOJO(box.content)
 +        generator.writeEndObject()
 +    }
 +}
diff --cc 
grails-web-common/src/test/groovy/org/grails/web/json/PathCapturingJSONWriterWrapperApiSpec.groovy
index ce55f258b2,110936c01c..23e75540f7
--- 
a/grails-web-common/src/test/groovy/org/grails/web/json/PathCapturingJSONWriterWrapperApiSpec.groovy
+++ 
b/grails-web-common/src/test/groovy/org/grails/web/json/PathCapturingJSONWriterWrapperApiSpec.groovy
@@@ -110,13 -111,13 +111,13 @@@ class PathCapturingJSONWriterWrapperApi
          then:
          result.is(writer)
          output.toString() == '[true]'
 -        commaOutput.toString() == ','
 +        commaOutput.toString() == ''
-         writer.getCurrentStrackReference() == ''
+         writer.getCurrentStackReference() == ''
      }
  
 -    void "delegated raw append rejects null and invalid sequences"() {
 +    void "delegated raw append rejects null"() {
          when:
 -        appendRaw(new PathCapturingJSONWriterWrapper(new JSONWriter(new 
StringWriter())), value)
 +        appendRaw(new PathCapturingJSONWriterWrapper(new JSONWriter(new 
StringWriter())), null)
  
          then:
          def error = thrown(JSONException)

Reply via email to