joelittlejohn / jsonschema2pojo

Generate Java types from JSON or JSON Schema and annotate those types for data-binding with Jackson, Gson, etc
http://www.jsonschema2pojo.org
Apache License 2.0
6.24k stars 1.66k forks source link

Spaces in field name causes Exception #1623

Open solsticesurfer opened 4 months ago

solsticesurfer commented 4 months ago

When parsing a json file that has spaces in the field names, the following is thrown: java.lang.IllegalArgumentException: Path not present:. It's then followed by the URL-encoded version of the property name (e.g. "My%20Property"). Unfortunately, I don't have control over the source and design of the payload.

I've scoured through the documentation, issues, and source but haven't found a solution. I thought this might be covered under propertyWordDelimiters but that doesn't seem to be the case.

Ideally, I would like to either remove the space in a property name or replace it with another valid character.

Is this possible or is there a better approach to dealing with this scenario?

sdegroot commented 4 months ago

I have a similar issue with properties that have a question mark in them :) This is since 1.1.2, where it worked at 1.1.1

joelittlejohn commented 4 months ago

There are some characters that need to be escaped because they can the way that JSON Pointer is resolved. The next release (1.2.2) will fix question marks. For the change that was needed, see #1522.

@solsticesurfer What version of jsonschema2pojo are you using? Spaces in the property name should work. If you can provide a minimal snippet of JSON Schema that fails, that would be good.

unkish commented 4 months ago

@joelittlejohn spaces in property names work for JSON Schema but fail with regular json, ie. generating pojo's from following json fails:

{
   "a b": "text"
}

whilst everything is OK with json schema:

{
  "type" : "object",
  "properties" : {
    "a b" : {
      "type" : "string"
    }
  }
}
solsticesurfer commented 4 months ago

@joelittlejohn Appreciate the help and thank you. This is using version 1.2.1 via Gradle. This JSON sample below is enough to reproduce the behavior.

As @unkish pointed out, the conversion fails when calling Json2Pojo.convertJsonToJavaClass() due to the field having a space. Removing the last line in the sample results in the target class being created as expected.

Unfortunately, the schema is not known before processing and is not constant, so using json schema is not an option.

I do see that the field with the underscores is successfully interpreted and that the result has the underscores removed. If we had the ability to specify a substitution rule to use in a case like this, it would resolve the problem.

{
    "fieldA": 123,
    "fieldB": 456,
    "My_Custom_Field": "abcdef",    
    "My Other Field": "abcdef"
}
re1llyj commented 4 weeks ago

Is there any plans to fix this..? The many different types of json files, each has several fields with spaces in it, and would be awesome if this could be fixed...? Tkx!

unkish commented 2 weeks ago

Is there any plans to fix this..? The many different types of json files, each has several fields with spaces in it, and would be awesome if this could be fixed...? Tkx!

Eventually. No specific timeline though as to when that might happen