How to can JIBX codeGen be instructed to not generate duplicate classes for 2 schemas - jibx

I am using JIBX maven plugin to generate the Java classes from the XSD schema. Below are 2 different sample XML types one representing a customer and other representing an account. The address field is similar in both the schema. However codegen generates 2 separate Address classes (i.e. Address and Address1) because they belong to different namespaces. How can I make JIBX codegen generate a single class file and make them reuse across repeating structure. Provided the author of the schema does not provide a common type schema.
<customer xmlns="xyz.com/cust">
<cust_number>97767</cust_number>
<name>John Doe</name>
<address>
<street_name>1st Street</street_name>
<address_line1>Line 1</address_line1>
<address_line2>Line 2</address_line2>
<city>San Jose</city>
</address>
</customer>
<account xmlns="xyz.com/acc">
<acc_number>97767</acc_number>
<acc_type>CHK</acc_type>
<name>John Doe</name>
<address>
<street_name>1st Street</street_name>
<address_line1>Line 1</address_line1>
<address_line2>Line 2</address_line2>
<city>San Jose</city>
</address>
</account>

Technically, if a schema is in a different namespace, it is a different schema. JiBX doesn't have a way to fake a namespace using configuration. If you have an issue with a vendor's schema, run it through a transformation tool such as XSLT to correct the issue with the schema (ie., change the namespace), then you can generate the shared schema. JiBX can then use the shared schema as it does in the example here:
https://github.com/jibx/maven-plugin/tree/master/test-suite/web-schema-test.
You can find an explanation here:
https://jibx.sourceforge.io/maven-jibx-plugin/modular-codegen.html. I use this technique successfully in my automated public schema to code scripts.

Related

WSO2 data service and data mapper

I am trying to apply the data mapper mediator to the output of the XML data service defined within WSO2EI. Documentation indicates, that to use the data mapper you need to have a fully qualified names in the XML input files.
The data service I am creating does not include qualified prefixes within the XML it generates.
I tried to export the XSLT data mapping from the CAR file and run it along the sample XML generated by the data service through the external XML transformer - it did not work. However, if I added qualified prefixes in the input XML manually, everything works fine.
It seems that the reason for my data mapper not working is the default, and not qualified, namespace in the input XML. Unfortunately, I cannot get the data service including namespace prexifes in its output. Any ideas?
To illustrate the nature of the problem let us consider two slightly different inputs; first XML input file uses the default names, second one qualified names:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<users xmlns="http://ws.wso2.org/dataservice">
<user>
<last>Waelchi</last>
<first>Xzavier</first>
<country>Swaziland</country>
</user>
</users>
</soapenv:Body>
</soapenv:Envelope>
This XML is not properly handled by the XSLT, no matter if within WSO2EI, or external XML processor. However, the same XML with qualified names:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<users xmlns:p="http://ws.wso2.org/dataservice">
<p:user>
<p:last>Waelchi</p:last>
<p:first>Xzavier</p:first>
<p:country>Swaziland</p:country>
</p:user>
</users>
</soapenv:Body>
</soapenv:Envelope>
is properly interpreted at least by the external XML processor. My problem is, that I cannot get WSO2EI data service to include qualified prefixes in its output.
OK, I have managed to bypass the problem transforming XML output by WSO2 data service into qualified XML using XSLT transform. However, I am still unable to get qualified XML directly from the data service; any suggestion shall be appreciated.

Use object keys as type in JSON Schema

Say I want to validate a YAML file against a JSON schema in Intellij IDEA. The file's structure would be like:
foo:
command: touch /tmp/a.txt # I know I don't need this but it's an example
bar:
command: echo "Hello World!" > /tmp/a.txt
baz:
command: cat /tmp/a.txt
dependencies:
- foo
- bar
So the property names can be any string, but the dependencies should only be keys/property names of the root object. Ideally I would specify an enum, but this question suggests it's not possible Use object property keys as enum in JSON schema (unless the answer is obsolete).
Still, I have noticed that when you write a schema in Intellij and you add a "required" = [...] it autocompletes the required fields with the property names of the "property" object (even though it doesn't use them to validate, but close enough for my purpose). I have checked out the schema for it http://json-schema.org/draft-07/schema# but haven't been able to understand how it does that.
Is there a way that I can define my schema so Intellij autocompletes based on another properties' keys like it does when you define a schema?
There is nothing in the schema itself that indicates possible values from data. There's actually no requirement that items in the required array also be defined in properties.
This sort of functionality is defined by the IDE only.
IntelliJ IDEA documents the ability to add custom schemas:
Besides schemas from JSON Schema Store, IntelliJ IDEA lets you
configure and use custom schemas from other storages. You can download
the required schema and store it under the project root or specify the
URL of the resource so IntelliJ IDEA can download the schema
automatically.
To configure a custom JSON Schema:
In the Settings/Preferences dialog ⌘,, go to Languages and Frameworks
| Schemas and DTDs | JSON Schema Mappings.
https://www.jetbrains.com/help/idea/json.html#ws_json_schema_add_custom
It also details later how to make the intelesense provide a rich preview:
Using HTML descriptions in JSON schema #
By default, IntelliJ IDEA escapes HTML characters when displaying
documentation for JSON schema definitions in documentation popups. To
get nice looking documentation with rich HTML markup, store the HTML
description in the x-intellij-html-description extension property
instead of description.
https://www.jetbrains.com/help/idea/json.html#ws_json_show_doc_in_html
However,
autocompletes based on another properties' keys
sounds like custom functionality specifically designed for writing JSON Schema. JSON Schema itself cannot reference data dynamically like that (which I assume is what you were thinking).

What is json schema equivalent of targetNamespace?

In any xml file i can say what namespace I refer to using xmlns-attributes to describe the namespace. It is well descibed here: What does "xmlns" in XML mean?
Then I can use a xml schema having a target namespace so that everyone knows that the schema describes that namespace. One question about that is found here: Why do we need targetNamespace?
Using json-schema we can define schemas for json documents. My mental model is that this is roughly equivalent to having a xsd file.
Now, how do I reference the schema in a json object? I can reference a schema using $schema attribute, but how do I declare the name of the schema i develop myself? I dont understand the equivalent of targetNamespace
Researching for writing the question I found the answer. The closest thing of a targetNamespace is the $id attribute. The standard states...
The "$id" keyword defines a URI for the schema, and the base URI that
other URI references within the schema are resolved against. A
subschema's "$id" is resolved against the base URI of its parent
schema. If no parent sets an explicit base with "$id", the base URI is
that of the entire document, as determined per RFC 3986 section 5
[RFC3986].
... which is kind of the mirror image of the leading text for $schema...
The "$schema" keyword is both used as a JSON Schema version identifier
and the location of a resource which is itself a JSON Schema, which
describes any schema written for this particular version. The value
of this keyword MUST be a URI [RFC3986] (containing a scheme) and this
URI MUST be normalized. The current schema MUST be valid against the
meta-schema identified by this URI.
so it is essentially the same thing.
Some things to note, however:
a) you use $schema in a schema to define what schema should be used for defining your own custom schema. It is not stated in the spec that $schema in any kind of object should indicate validation for a schema.
b) You may define in your schema that $schema should be an indication about what schema to use for validation.
c) there are other ways to indicate the schema for data. One such example is using content-type in http headers. Another is to use link http headers.
d) vscode and visual studio both interpret $schema as a reference to a schema for use in validation
The issue has been discussed at the github repo for the spec.
https://github.com/json-schema/json-schema/issues/235
https://github.com/json-schema/json-schema/issues/220#issuecomment-209452992

Biztalk 2013: Setting different namespace on schema elements

I'm developing a BizTalk application to query a number of web services that have been written and maintained by a third party, and I'm having some trouble getting the namespaces right on the Schemas.
Basically, I can't consume the wsdl to automatically generate the schemas because the namespaces and element names are all wrong within the generated schemas (due lazy C# wsdl generation), so I'm having to write them from scratch. This would be fine, but the Web Service endpoints are requiring that the elements within the schema all be qualified with specific namespaces, and none of them match the namespace of the overall schema.
I have figured out how to import other namespaces/schemas into my schema, but I can't figure out how to change the namespace of the elements to anything but the default. Does anyone know how to do this?
For example, the Schema root has to have a namespace of "http:/tempuri.org/", but one of the elements requires the namespace "http://schemas.datacontract.org/2004/07/ReadService.DTO.Inbound.Supplier", but within BizTalk, I can't edit the namespace of that element to change it.
The body of one of the requests looks like this:
<tem:GetSupplierIdWithExternalId>
<tem:request>
<com:Header>
<com1:Username></com1:Username>
<com1:Locale></com1:Locale>
</com:Header>
<read:ExternalSupplierId></read:ExternalSupplierId>
</tem:request>
</tem:GetSupplierIdWithExternalId>
"tem" in this case is http://tempuri.org/". "com", "com1" and "read" are all different namespaces, which, as Gruff has pointed out, are all default namespaces for WCF projects.
Generating from WSDL in Biztalk creates 2 issues:
The default namespace applied to the root note is not tempuri.org (as it recognises this as a default), it's the standard Biztalk http://..Folder.SchemaName namespace. Changing this to tempuri.org causes a cascade of errors that have to be fixed, and it doesn't resolve the more major issue which is:
Because of the way the WCF functions the WSDL has been generated from are written, the major element names (GetSupplierIdWithExternalId above) are all named incorrectly - in most cases, something like "GetSupplierIdWithExternalIdRequest", because that's the name of the function that schema is generate from. Again it's due to lazy programming on the endpoints, because the name of the element isn't being properly defined, it's just assumed by the generation process.
If I try and create a single flat file schema, I can only define a single namespace for the whole file, and if I set that to tempuri.org I get:
<ns0:GetSupplierWithExternalId xmlns:ns0="http://tempuri.org/">
<Header>
<Username>Username_0</Username>
<Locale>Locale_0</Locale>
</Header>
<ExternalSupplierId>ExternalSupplierId_0</ExternalSupplierId>
</ns0:GetSupplierWithExternalId>
...which fails the a SOAP request because the namespaces on the internal elements aren't correct.
Thanks in advance!
You will need to define the element with the namespace of "http://schemas.datacontract.org/2004/07/ReadService.DTO.Inbound.Supplier" in its own schema file, and import it into the schema root and compose the root that way. The element will keep the namespace it was defined as.
Looking at the namespace "http://schemas.datacontract.org/2004/07/ReadService.DTO.Inbound.Supplier", it seems it is the default namespace that WCF gives the data contract because it was not explicitly defined. (The CLR namespace of the class is ReadService.DTO.Inbound.Supplier) When the DataContractSerializer serializes the message when sending the request, it will serialize it with that namespace. You should not try and change it in the BizTalk schema, otherwise there will be a schema mismatch.
UPDATE:
In your update you mention 2 issues when generating the schema from the WSDL.
Can you paste a screenshot of this?
Are you sure GetSupplierIdWithExternalIdRequest is incorrect? If you search in the WSDL for that term, can you find it?
The operation's request and response wrappers typically get suffixed with -Request and -Response, so this might be perfectly correct.

What's a strategy for accessing Plinqo metadata from an external program?

I am using Plinqo and Linq-to-SQL to implement a repository. I'd like to inform the UI of validation rules by examining metadata and acting accordingly. Problem is, the Metadata classes in Plinqo are marked internal and are nested inside the classes they decorate.
How can I get at these classes and enumerate their attributes from another assembly?
The only way that I'm aware of accomplishing this is to use Reflection. The following code uses reflection and looks for all of the rule's attributes defined on the internal metadata class. DynamicData also does a similar lookup of the attributes defined in the Metadata class by using an attribute defined on the class that can be found in the generated partial class:
[System.ComponentModel.DataAnnotations.MetadataType(typeof(PetShop.Data.Category.Metadata))]
Thanks
-Blake Niemyjski