jsonix-schema-compiler icon indicating copy to clipboard operation
jsonix-schema-compiler copied to clipboard

Generate JS-mapping with default values

Open martinpfannemueller opened this issue 10 years ago • 12 comments
trafficstars

Referring stackoverflow question: http://stackoverflow.com/questions/29256036/jsonix-get-restrictions-and-default-value-for-properties

martinpfannemueller avatar Mar 25 '15 23:03 martinpfannemueller

See also highsource/jaxb2-basics#26.

highsource avatar Apr 11 '15 22:04 highsource

Hi, is there any change of default attributes being supported?

st-sloth avatar Oct 26 '18 13:10 st-sloth

Didn't work on it, sorry.

highsource avatar Oct 28 '18 11:10 highsource

That's okay. I also apologize for misspelling the question :woman_facepalming: I meant to write "is there any chance". I forked the repo and tried to add this but I've never worked with Java and this change turned out to be not that trivial, so I didn't succeed. I could however add processing for default in jsonix, if the mappings would include it.

I apologize again for being a nuisance, just wanted to know whether this feature has a future.

st-sloth avatar Oct 28 '18 15:10 st-sloth

No need to apologize.

I've checked the code, the Java part should not be too hard to implement.

highsource avatar Oct 28 '18 21:10 highsource

I've implemented the first draft of defaultValue generation. Please give it a try and let me know if it works for you.

highsource avatar Oct 28 '18 22:10 highsource

Wow, @highsource, thank you so so much, this is amazing!!! :tada: :heart:

It certainly works for me. There are minor issues, but in my case there are workarounds: defaultValue does not appear on attributes that

  1. have neither type attribute nor simpleType element. In mappings they have typeInfo: 'AnySimpleType';
  2. refer to external simpleType with enumeration. Internal simpleType with enumeration or simpleType with pattern, both external and internal, work. But also in case of external simpleType with pattern, typeInfo is missing in the result;
  3. also from the test schema - while attribute of type xs:NCName has defaultValue, attribute of type xs:IDREF, which in turn refers to xs:NCName, does not.

I added several cases to the test schema in my branch.


There is also some weird behavior not related to default values - if attribute is named package, then in the mappings it is prefixed with an underscore, though it is unchanged in attributeName:

{
  name: '_package',
  attributeName: {
    localPart: 'package'
  },
  type: 'attribute'
}

is it by design or an issue?


I'm yet working on support of defaultValue in jsonix, and will make a PR in some time.

st-sloth avatar Oct 29 '18 13:10 st-sloth

Please file separate issues for individual problems. I'd be grateful if you send PR with reproducing test cases (please use tests/issues/src/main/resources/schema.xsd for that).

  1. anySimpleType - not quite sure about types here. Probably just copy the string value.
  2. Not sure what you mean by "external simpleType".
  3. ID/IDREF/IDREFS are a special type of beast.

highsource avatar Oct 31 '18 19:10 highsource

For your other question about _package see #82.

highsource avatar Oct 31 '18 19:10 highsource

Created the PR for extending the test schema.

  1. anySimpleType - not quite sure about types here. Probably just copy the string value.

It does seem to be the most logical and expected behaviour.

  1. Not sure what you mean by "external simpleType".

An attribute declaration that references its type via type, like this. In contrast with "internal" that is inside the attribute declaration, like this.

  1. ID/IDREF/IDREFS are a special type of beast.

Which is totally fine by me :) There is no problem here, just wrote down accidental observation


Please file separate issues for individual problems

Do you mean the _package thing? If so, it's not much of a problem, just thought it would be more appropriate to ask about it on the side of an ongoing discussion rather than create whole another issue. Which probably was a mistake. Do you want me to create an issue about _package?

If you meant something other than _package, I'm afraid I don't understand what is that.

st-sloth avatar Nov 05 '18 12:11 st-sloth

Thank you, I'll take a look at it as soon as I have time.

With "separate issues" I mean one isse per points 1, 2 and 3. ok, 3 is not relevant so no it's just points 1 ("Support default values for anySimpleType") and 2 ("Support default values for named simple types").

Basically I'd be gratefult to have several small specific issues vs. one big "kill em all" generic issue.

highsource avatar Nov 05 '18 13:11 highsource

New issues are created.

I truly appreciate it a huge lot that you look into this! Infinite thanks!

st-sloth avatar Nov 05 '18 13:11 st-sloth