JOOQ: mark column as "deprecated"? - jooq

I'd like to somehow mark a column in my schema as "deprecated" (probably as some kind of tag in the column comment or something), and then have that info bubble up through the code generation process to result in an #Deprecated annotation being added to the fields/methods for that column in the generated code.
Does JOOQ have any functionality related to that?
Browsing through the user manual doesn't seem to show anything relevant.

Using the jOOQ 3.15 synthetic comments feature
Starting from jOOQ 3.15, you can use the synthetic comments feature from the code generator, e.g.
<configuration xmlns="http://www.jooq.org/xsd/jooq-codegen-3.16.0.xsd">
<generator>
<database>
<comments>
<comment>
<!-- Regular expression matching all objects that have this comment. -->
<expression>CONFIGURED_COMMENT_TABLE</expression>
<!-- Whether the comment is a deprecation notice. -->
<deprecated>true</deprecated>
<!-- Whether the original schema comment should be included. -->
<includeSchemaComment>false</includeSchemaComment>
<!-- The actual comment text. Defaults to no message. -->
<message>Do not use this table.</message>
</comment>
</comments>
</database>
</generator>
</configuration>
Hack using SQL comments
Use this hack (if your RDBMS supports comments):
COMMENT ON COLUMN my_table.my_column IS '#deprecated';
It won't produce the annotation, but it will produce equivalent Javadoc, which has the same effect in IDEs.

Related

Different XSD for Create vs Update operations

consider the following object:
<Person>
<Name/>
<FirstName/>
<Street/>
<City/>
<SocialSecurityNr/>
<Gender/>
<Hobby/>
</Person>
Asume I use this object for a Create web service operation. When calling the Create operation, all fields of the Person object must be provided except 'hobby'
Now assume that I also have an Update operation. When updating, only socialsecuritynumber is mandatory: I do not need to update each field.
How do you process this in an XSD? Should you define seperate XSDs for the create and update operation?
The reason I want to make this distinction is that I do not want to send unnecessary fields from sender to recipient when it is not needed. Hence, I want to use minOccurs = 0 as much as possible.
It feels like this is a common problem, but can't find any references about it
It's really up to you. I've seen both approaches.
If I want a comprehensive description of the service contract, I would go with separate operations and just use common data types, defined in the xsd whenever needed.
The other option is to not use minOccurs=0. This will make the xsd shorter and somewhat more flexible, but also more open to interpretation (which, when describing a service more bad than good). If you need to give the wsdl to someone in order to consume your service in the future this will require more description of the different use cases (create/update) in the documentation. The other approach is more straightforward and intuitive for developers.
I always use a common structure for create and update.
When a data carrying element is declared in an XSD it should have the attrivute nilable set to true e.g.:
<xsd:element name="Result" type="xsd:string" nillable="true"/>
This allows for the xsi:nil attribute to be applied to the element.
[1]
<tag>Data</tag>
<tag xsi:nil=”false”>Data</tag>
<tag xsi:nil=””>Data</tag>
Tag is present and includes data. There may be an empty xsi:nil attribute or the attribute may not be present.
The target application needs to update the field with the specified data.
[2]
<tag></tag>
<tag xsi:nil=””></tag>
<tag xsi:nil=”false”></tag>
<tag/>
<tag xsi:nil=""/>
Tag is present and is self closing or does not include data. There may be an empty xsi:nil attribute or the attribute may not be present.
The target application needs to update the field to zero length data. E.g. an empty string.
Some applications may update the data to null.*
[3]
<tag xsi:nil=”true”>Data</tag>
<tag xsi:nil=”true”></tag>
<tag xsi:nil=”true”/>
Tag has the xsi:nil attribute set to true. It may or may not contain data and it may or may not be self closing.
The corresponding field should be updated to null.
[4]
Tag is missing from the xml.
No update should occur on the corresponding field

Where is the XSD file for "http://www.w3.org/2001/XMLSchema-instance"?

Where is the XSD schema definition file for the namespace "http://www.w3.org/2001/XMLSchema-instance"?
Strange it may sound, but the XML schema for http://www.w3.org/2001/XMLSchema-instance namespace does exist and is found exactly by the very URL denoted by the namespace URI:
http://www.w3.org/2001/XMLSchema-instance
For a proof, just open that link (URL) in an HTML browser (e.g. FireFox).
You will probably see some HTML text, like: "XML Schema instance namespace ...".
Then, save that 'HTML' as a file on your computer (e.g. File | Save Page As).
When you look into this file, you will see that it is not HTML at all. Rather, it is a complete XML schema for that namespace!
Equally, you can import the http://www.w3.org/2001/XMLSchema-instance namespace into your own schema as the following:
<xs:import namespace="http://www.w3.org/2001/XMLSchema-instance"
schemaLocation="http://www.w3.org/2001/XMLSchema-instance"/>
See also this question: Error while parsing xsd using xjc, which although sounds very differently, actually very much related to the same problem.
Just to add fuel to the fire -- many XML tools have knowledge of http://www.w3.org/2001/XMLSchema-instance baked-in, so it looks like you never need the schema at all. In fact, you need the schema if you are using an XML tool that does not bake-in this knowledge.
So is for that reason that we find actually always beginning of xml documents where there ins't xml-schema xsd declaration at all? For example like this:
<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.2.xsd">
Here is some updated information on this topic.
XSD 1.1 part 1 §2.6 states:
XML Schema Definition Language: Structures defines several attributes
for direct use in any XML documents. These attributes are in the
schema instance namespace (http://www.w3.org/2001/XMLSchema-instance)
described in The Schema Instance Namespace (xsi) (§1.3.1.2) above. All
schema processors must have appropriate attribute declarations for
these attributes built in.
Further, §3.2.6.4 says:
The {target namespace} of an attribute declaration, whether local or
top-level, must not match http://www.w3.org/2001/XMLSchema-instance
(unless it is one of the four built-in declarations given in the next
section). Note: This reinforces the special status of these
attributes, so that they not only need not be declared to be allowed
in instances, but in consequence of the rule just given must not be
declared.
So, you can't declare attributes such as xsi:type or xsi:schemaLocation in a schema document, and therefore you can't import a schema document that attempts to declare such attributes.
This of course is XSD 1.1 and therefore doesn't directly constrain an XSD 1.0 processor. However, it's one of the many areas where XSD 1.1 issues guidance for cases where XSD 1.0 said nothing, and where different implementations went off in different directions.

Making subtags dependent on an attribute of a parent in XML Schema

I have created an XML file like the following
<monitor>
<widget name="Widgets/TestWidget1">
<state code="VIC" />
<state code="TAS" />
</widget>
<widget name="Widgets/TestWidget2">
<client code="someclient" />
</widget>
</monitor>
The name attribute of the <widget> tag tells the parser what widget to load (they are asp.net user controls).
I am trying to create a schema file for the above, the problem is that inside the <widget> the supported subtags are dependent on the name attribute. So TestWidget1 supports the <state> tag and TestWidget2 supports the <client tag.
Currently my XML Schema file just displays all possible <widget> subtags regardless of whether they are supported or not.
How can I write an XML schema file that will only allow specific subtags based on the name attribute? If this is not possible, what options do I have?
You have several options. The simplest and most direct is to re-think your problem a bit. If the legal content of element E1 and the legal content of element E2 are different, then the simplest design is to call them different things, because in XSD as in DTDs the legal content of an element depends on the element type name. A devil's advocate would ask you "if you want different kinds of widget to obey different rules, why are you telling the validator that they are the same kind of widget? Tell the validator the truth, by giving them different names. So don't call them and so on, call them and ."
In XSD 1.1 you can also use conditional type assignment or assertions to define constraints on the legal combinations of attributes and children, but not every schema-aware editor is going to have the chops necessary to analyse the conditional type assignment rules and attributes and understand what to prompt you with.

Require that an element has another element as a descendant

Is there a way in an xsd schema to require that an element have another element somewhere as a descendant?
For example, element parent requires a descendant desc. This is valid:
<parent>
<a>
<b>
<desc></desc>
</b>
</a>
</parent>
As is this:
<parent>
<c>
<desc></desc>
</c>
</parent>
but this isn't:
<a>
<parent>
<b/>
</parent>
</a>
The potential child elements for parent are many and complicates, so it would be difficult to enumerate every possible valid configuration.
Something like the key/selector schema elements seems like it would work, where I could provide an xpath expression defining the valid locations for desc element, but all of the examples I've found are aimed at matching up the value of attributes.
No, (almost) all XML Schema validation is shallow, called "local" in the spec. Here's one excerpt that emphasizes type validation as "local" validation.
Element Validated by Type If an
element information item is ·valid·
with respect to a ·type definition· as
per Element Locally Valid (Type)
(§3.3.4), [it is marked as] ·validated·.
The only exception is for the identity constraints like uniqueness and key-references which have a broad scope in an XML document but narrow uses.
I don't know if XSD supports what you are trying to do, but there is a work-around.
You could do complex validations with a two-step process:
First simply use your XSD schema for basic validation
Next use an XSLT which does more complex validations, and outputs the result of that validation
This may not plug in well to whatever framework you are working with, but might work well for (partially) custom code. It also has the advantage (over doing the extra validations in code) that you can publish both documents.
From a quick google search, one effort towards this end is Schematron. It actually foregos XSD entirely, and just uses XSLT. It appears to be a published standard:
http://www.schematron.com/

Can you use key/keyref instead of restriction/enumeration in XML schema?

Suppose we have a stylesheet which pulls in metadata using the key() function. In other words we have instance documents like this:
<items>
<item type="some_type"/>
<item type="another_type"/>
</items>
and a table of additional data we would like to associate with items during processing:
<item-meta>
<item type="some_type" meta="foo"/>
<item type="another_type" meta="bar"/>
<item type="yet_another_type" meta="baz"/>
</item-meta>
Finally, suppose we want to do schema validation on the instance document, restricting the type attributes to the set of types which occur in item-meta. So in the schema we want to use key/keyref instead of restriction/enumeration. This is because using restriction/enumeration will require making a separate list of valid type attributes.
However, it doesn't look like key/keyref will actually work. Having tried it (with MSXML 6.0) it appears the selector of a schema key won't accept the document() function in its xpath argument, so we can't examine the item-meta data, whether it appears in an external file or in the schema file itself. It looks like the only place we can look for keys is the instance document.
So if we really don't want to have a separate list of valid types, we have to do a pre-validation transform, pulling in the item-meta stuff, then do the validation, then do our original transform. That seems overcomplicated for what ought to be a relatively straightforward use of XML schema and stylesheets.
Is there a better way?
Selectors in key/keyref allow only a very restricted xpath syntax. Short, but not completely accurate: The selector must point to a subnode of the element declared.
The full definition of the restricted syntax is -> here.
So, no I don't see a better way, sorry.
BTW: The W3C states that this restriction was made to make life easier on implementers of XML Schema processors. Keep in mind that one of the design goals of XML Schema was to make it possible to process a document in streaming mode. That explains really a lot of the sometimes seemingly random restrictions of XML Schema.
Having thought about it a little more, I came up with the idea of having the stylesheet do that part of the validation. The schema would define the item type as a plain string, and the stylesheet would emit a message and stop processing if it couldn't look up the item type in the item-meta table.
This solution fixes the original problem of having to write down the list of valid types more than once, but it introduces the problem that validation logic is now mixed in with the stylesheet logic. I don't have enough experience with XSD+XSLT to tell whether this new problem is less serious than the old one, but it seems to be more elegant than what I wrote earlier about pulling the item-meta table into each instance document in a pre-validation transform.
You wouldn't need to stop the XSLT with some error. Just let it produce something that the schema won't validate and that points to the original problem like
<error txt="Invalid-Item-Type 'invalid_type'"/>
Apart from that please keep in mind that there are no discussion threads here. The posts may go up and down, so it's better to edit your question accordingly.
Remember, the philosophy here is "One question, and the best answer wins".

Resources