Closed ghys closed 2 years ago
Shall we consider this critical and fix it @ghys?
I have no decision power over here, but if it's quick and easy enough, I'd advise to do it, yes - if we decide to change it later it would mean users would need to reconfigure their existing rules.
Do you know exactly what would need to be done?
The Handler Factory needs to return the mime type(s) it supports through an interface if i remember correctly, should not be a tough fix
Based on what's done for JSScripting here:
I'm assuming something similar has to be done here:
Correct , we would want to either replace, or add to this line
an additional mime type
So it is actually the JRuby factory itself that delivers the wrong mime-types? Well, if they do not make sense for us, we can override them - I just hope it won't have any other side-effects?
Maybe what's because of the order here? https://github.com/openhab/openhab-addons/blob/22b9be1390a8e46c6bbbeed67a807d813e8e645e/bundles/org.openhab.automation.jrubyscripting/src/main/java/org/openhab/automation/jrubyscripting/internal/JRubyScriptEngineFactory.java#L54-L56 i.e. the first script type would be what ends up being the content type advertised, so
.concat(factory.getMimeTypes().stream(), factory.getExtensions().stream())
(just speculating)
The JRuby engine factory seems to use application/x-ruby
as its MIME type - it seems the planets aligned on this one ;)
.e. the first script type would be what ends up being the content type advertised, so
Yep that sounds right maybe @boc-tothefuture is monitoring and can chime in ?
Seems the order has no importance in fact:
I think I nailed it down to:
private String getPreferredMimeType(ScriptEngineFactory factory) {
List<String> mimeTypes = new ArrayList<>(factory.getScriptTypes());
mimeTypes.removeIf(mimeType -> !mimeType.contains("application") || mimeType.contains("x-"));
return mimeTypes.isEmpty() ? factory.getScriptTypes().get(0) : mimeTypes.get(0);
}
Because application/x-ruby
contains x-
it is removed from the list of preferred mime types and therefore only the extension rb
remains.
Actually contrary to what I said in the OP Groovy has the problem too:
Looks as if this line is in already since April 2019... It isn't really clear to me why those entries are excluded, though.
Maybe the Nashorn script engine factory exposes several MIME types including application/javascript
and another with x-
and this was the way to choose the right one.
JSR-223 compliant script engine factory for Nashorn. The engine answers for: names "nashorn", "Nashorn", "js", "JS", "JavaScript", "javascript", "ECMAScript", and "ecmascript"; MIME types "application/javascript", "application/ecmascript", "text/javascript", and "text/ecmascript"; as well as for the extension "js".
So I can't find a reason either why those entries with x-
are removed.
Ok it's because of Jython...
public List<String> getMimeTypes() {
return Collections.unmodifiableList(Arrays.asList(
"text/python", "application/python", "text/x-python", "application/x-python"));
}
Right, that would also explain why it was added by Scott... So maybe we should just exclude "text/x-python" and "application/x-python" in that line then?
As I said above I believe application/x-python
is actually more correct ;)
Jython uses application/python (probably should have been application/x-python as you're not supposed to invent new types without x-, they have to be registered at https://www.iana.org/assignments/media-types/media-types.xhtml).
(to be perfectly complete, even using x-
to avoid the registration process has been deprecated since 2012 with RFC 6648, see also RFC 6838 §3.4)
Maybe existing modules configured with application/python
would not have to be changed either, because the Jython engine would still answer for that script type.
So I would exclude application/python
(the UI would need to be changed as well) but other opinions are welcome!
Fine for me. Shall we try to squeeze this in tonight? It's probably helpful as fixing it later on will be a breaking change for existing scripts...
I created https://github.com/openhab/openhab-core/pull/2629 and https://github.com/openhab/openhab-webui/pull/1248 - completely untested though!
Reading through here, thanks for the mention @digitaldan - looks like there is an issue in how the core is processing the returned mime-types and not something JRuby binding is doing, is that correct? Happy to adjust the binding if we need to.
Thanks @ghys - I have merged both and launched a new build. Let's test the result together briefly!
Time for testing: https://ci.openhab.org/job/openHAB3-Distribution/2648/
This looks quite good now:
Great! I'll assume the editor does the syntax highlighting for all these languages as well.
Yes, syntax highlighting seems to work as well. 👍
Can this be closed?
The content type of the JRuby script engine seems to be wrong:
Expected Behavior
There isn't a IANA-assigned media type (formerly MIME type) but in the UI I originally used
application/x-ruby
to activate the Ruby mode of the editor, there is a conversion method here: https://github.com/openhab/openhab-webui/blob/fb426697e95cb06ef4f94f05fee6750303aee90e/bundles/org.openhab.ui/web/src/components/config/controls/script-editor.vue#L169-L178 Note that I added the currentrb
as well but it would probably be better to use a proper media type.Current Behavior
The current content type
rb
doesn't follow the standardtype/subtype
format.Possible Solution
Change it to
application/x-ruby
. Jython usesapplication/python
(probably should have beenapplication/x-python
as you're not supposed to invent new types withoutx-
, they have to be registered at https://www.iana.org/assignments/media-types/media-types.xhtml). Groovy usesapplication/x-groovy
.Steps to Reproduce (for Bugs)
Your Environment
OH 3.2 RC1