Invalid UTF-8 middle byte 0x22

3,071 views
Skip to first unread message

Chuan Chuan Law

unread,
Jun 22, 2015, 7:11:09 PM6/22/15
to pact-s...@googlegroups.com
Hi,


I am using the JVM Groovy module to generate body with regex like below:

 withBody{
                ComplianceDate{
                    Month (integer())
                    Year (integer())
                }
                BuildDate{}
                ListingType (~/\w+/)
                Odometer (integer())
                StockNumber (~/\w+/)



And occasionally when we run pact verify we get the error:

> com.fasterxml.jackson.core.JsonParseException: Invalid UTF-8 middle byte 0x22

Is there a way to ensure that the generated json file will be a valid UTF-8 char?

Please advice.


Thanks.

Ronald Holshausen

unread,
Jun 22, 2015, 11:46:38 PM6/22/15
to Chuan Chuan Law, pact-s...@googlegroups.com
Hi Chuan,

UTF-8 is the default encoding on the JVM. Did you get this when running the pact verification, or in the consumer tests?

Can you post the full stacktrace?

--
You received this message because you are subscribed to the Google Groups "Pact" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pact-support...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Ronald Holshausen

DiUS Computing Pty Ltd

Level 10, 99 Queens Street
Melbourne, VIC 3000

Phone: +61 3 9008 5400
Mobile: +61 413 162 439

http://www.diuscomputing.com.au

Chuan Chuan Law

unread,
Jun 23, 2015, 2:58:23 AM6/23/15
to pact-s...@googlegroups.com, chuan.c...@gmail.com
Hi Ronald,


I get this error sometimes when I run pact verification.

For eg the build failed this morning:

Verifying a pact between oag_consumer_es_industry and ElasticSearchCarProvider
  [Using file D:\programs\JenkinsSlave\workspace\Trade API Tests\TradeApiTests\target\pacts\OneAutoGate-Elastic Industry Search.json]
:pactVerify_ElasticSearchCarProvider FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':pactVerify_ElasticSearchCarProvider'.
> com.fasterxml.jackson.core.JsonParseException: Invalid UTF-8 middle byte 0x22
 at [Source: D:\programs\JenkinsSlave\workspace\Trade API Tests\TradeApiTests\target\pacts\OneAutoGate-Elastic Industry Search.json; line: 49, column: 44]

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.


Looks like sometimes the consumer test is generating some non-UTF8 chars.


Thanks.


Chuan

Ronald Holshausen

unread,
Jun 23, 2015, 3:08:45 AM6/23/15
to Chuan Chuan Law, pact-s...@googlegroups.com
If you open the pact file in a text editor, what do you see at line line: 49, column: 44?

Chuan Chuan Law

unread,
Jun 23, 2015, 3:11:25 AM6/23/15
to pact-s...@googlegroups.com

I re-ran the build and the file got over written.

When I get this again, I shall post more information here.

Thanks Ronald.


Cheers,

Chuan

Chuan Chuan Law

unread,
Jun 23, 2015, 3:14:07 AM6/23/15
to pact-s...@googlegroups.com
Hi Ronald,


I just ran the test locally and is able to re-produce the issue.

The generated pact file contains weird character like this:


        }, {
          "url" : "w?",
          "photoAngle" : "w�?"
        } ]
      },

Thanks.


Chuan


On Tuesday, 23 June 2015 17:08:45 UTC+10, Ronald Holshausen wrote:

Ronald Holshausen

unread,
Jun 23, 2015, 4:52:16 AM6/23/15
to Chuan Chuan Law, pact-s...@googlegroups.com
Ok, the regular expression are not being escaped correctly. This is a bug in the consumer library. You can use strings to get your tests working.

E.g.,

        withBody{
                ComplianceDate{
                    Month (integer())
                    Year (integer())
                }
                BuildDate{}
                 ListingType regexp('\\w+')
                Odometer integer()
                StockNumber regexp('\\w+')

Ronald Holshausen

unread,
Jun 23, 2015, 5:37:58 AM6/23/15
to Chuan Chuan Law, pact-s...@googlegroups.com
Actually, it is the generated example values that are not UTF-8. You can give a second parameter for the example and this should fix your problem:

        withBody{
                ComplianceDate{
                    Month (integer())
                    Year (integer())
                }
                BuildDate{}
                 ListingType regexp('\\w+', 'ABCDEF')
                Odometer integer()
                StockNumber regexp('\\w+', '1234567')

Chuan Chuan Law

unread,
Jun 23, 2015, 6:55:44 PM6/23/15
to pact-s...@googlegroups.com
Hi Ronald,


I tried both 

   withBody {
                "spotId" (integer())
                "redbookCode" ('\\w+')

and

   withBody {
                "spotId" (integer())
                "redbookCode" ('\\w+', 'ABCDEF')


produces:

 "body" : {
        "spotId" : 1978289031,
        "redbookCode" : "\\w+",


It's generating "\\w+" in the Json file instead of producing a regex.


Thanks.

Chuan

Ronald Holshausen

unread,
Jun 23, 2015, 6:59:20 PM6/23/15
to Chuan Chuan Law, pact-s...@googlegroups.com
You need to use  redbookCode regexp('\\w+', 'ABCDEF')

Reply all
Reply to author
Forward
0 new messages