-
-
Notifications
You must be signed in to change notification settings - Fork 440
[REST] New API for conversion between file formats #4779
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
I understand that you haven't implemented Items here yet. But I'm wondering how will we request it to convert JSON Item to Yaml? Currently I see /create accepts "text/vnd.openhab.dsl.thing", "application/yaml". application/yaml will return Thing. The same goes for /transform. Will we use different end points for item creation / transformation? |
As I understand it:
Why not remove /create and make /transform able to accept JSON as well? Bonus points that It could perform a round trip as a way to validate data (whichever the format) when source format == target format. If the source data is correct, return it, otherwise return an error. Also this way we can have |
There was a rather lengthy discussion in #4585 how the api should look like. So it was
@lolodomo also wanted to provide convenience to serialize existing objects more easily, that's why there was also a @lolodomo @jimtng |
No, this will be the same. I will just add support for items. |
Very good question, I was sure someone will ask it :) |
I was not yet sure what to do when input media and output media are identical. I applied the general transformation process leading to an output that can be different from the input. But it can be changed to simply return as output what was provided as input. |
As explained in a previous message just before, we don't need to distinguish things and items. The code is considering what it receives as input and what output media is requested and then builds the output. |
Yes, I found a way to make the transformation API almost fully generic. It was a wish of you or @mherwege I believe. I think it is really fantastic to have an API that can directly converts from DSL to YAML or YAML to DSL. My idea was a unique generic API being able to convert from JSON/DSL/YAML to JSON/DSL/YAML. It currently only covers DSL/YAML to JSON/DSL/YAML for a technical reason. What I wanted to remove is the fact that the APIs either takes a JSON as input or a JSON as output. I think it is good to be able to really convert from a file format to another one, DSL to YAML for example. That is the reason why I renamed parse into transform as it is not only a parser but also a generator. Parser + generator = transform. |
In fact, I have an option to remove the create API and have the transform API supporting also JSON as input, I can just provide a JSON example for the JSON content type and so receives the JSON in a String parameter (like for DSL and YAML) and then parses myself the JSON in a FileFormatDTO object (instead of being done automatically). I will apply this change, it's worth the effort. |
Thanks for the explanation.
Interesting! So the YAML input must contain either In the UI (thing-details page), the Also, since the UID cannot be changed anyway, maybe we'll strip that off before displaying in the UI too, so that there is no leading spaces in the display. i.e.
will be displayed as:
That's awesome! Did you consider the name |
This was @mherwege .
Examples:
Output: Items-DSL {
"items": ["List of ItemDtos"],
"links": ["List of LinkDtos"]
} if input json is selected or items:
Item1: ...
Item2: ... if input yaml is selected As you can see the required input schema is now dependent on the desired output (items-dsl) and the selected input (json or yaml).
All these possible error cases have to be properly reported in an api response. When serializing from/to json dtos it's easy:
It's unacceptable for the transformation API to guess if it's okay to drop data or not.
e.g. things-dsl + items-dsl -> yaml or things-DTO + items-DTO -> yaml. The generic translation endpoint is very complex and leads to strange edge cases and behavior. |
Unfortunately, still does not work. |
Please come back to a more reasonable level of discussion ... or all of this will end up exactly like the previous time you had this way of discussing with @J-N-K , that is to nowhere!
I understand some of your points but restricting features just for a question of API documentation, it is a shame. I like the idea to have a direct conversion between DSL and YAML. Just my opinion. Let's first listen at other opinions. @jimtng @mherwege and others. |
The only use case I'm aware of right now is to convert json object from mainui (which is retrieved from the rest api too) into yaml (and maybe dsl) and back so that what we see in the "CODE" tab in Thing-details and Item Details match the same format as the Yaml file format. I'm not sure what the application for DSL <=> YAML would be for, but maybe it can be used to offer a tool to convert user's existing DSL to YAML. The API in this PR would provide these capabilities.
What's the difference between that and the server simply doing the intermediate step for you? At the end of the day, there is one ultimate format: The Java Thing object itself. Any other format would be created in such a way that there is no data loss to/from that format.
Can you explain what documentation problem exists in the implementation from this PR? |
I see multiple use cases:
I am largely indifferent to the final REST structure, but based on the comments issue, I do think having a possibility for a direct conversion makes sense (even if we do not achieve keeping the comments just yet). I also think that if there is a way to limit the number of endpoints and still keep the documentation properly represented, we should strive for that. But I do respect @lolodomo when he does not find a good solution for that to use multiple. |
No
You all don't see an immediate interest to have a conversion between DSL and YAML file formats and the current identified use cases are currently with Main UI and will require the REST DTO object either as input or output. So let make @spacemanspiff2007 happy again and let's restrict the APIs to always go through a DTO object. |
I did not see this message. I don't know if this is a good idea to strip the code, in that case we will not show exactly the file format. The idea is to have the ability to copy from the code tab, and in that case it is better to have the full syntax. |
Hmm, maybe it's simplest to keep it as is? It's just awkward to see On the other hand, I do see your point about being able to copy paste to/from the file directly. Perhaps @merwege or @florian-h05 have a better idea on how to do this. |
I agree with you. We have pros and cons for the 2 options. |
I do think the UI editor should show only the context of what is being edited or viewed. In my view, that does mean stripping the higher level wrappers. At the moment, for things, there is only a per thing editor. I could imagine a code view on the thing list level where all things are shown, so that would include the higher level structure. But it wouldn’t be editable (or it would require considerable UI changes). for items, there is a similar consideration. The current item edit code view only shows the item data, and not the channels and links. They are not editable in that place. If we start showing them, we create the expectation they are editable. Maybe we should therefore have a non-editable code view on the item level (not the item edit level) that does show these. And then again on the item list level showing the code for all items (non-editable). These non-editable views could even include the file-defined items and things. For me, in summary, when it is editable, it should match the context of the object being edited, nothing else. I can imagine non-editable views on a higher level showing the more complete definition. This is only my opinion, open for anything. |
Note that for items I could also include a parameter in the API to hide or not the metadata and channel links.
But it makes sense. |
c8fb28f
to
b671fb7
Compare
Change done. |
b671fb7
to
f737139
Compare
When the API returns status 400, the errors are provided as a string separated by a carriage return. Maybe it would be better to return errors as a JSON ? |
f737139
to
84acb9a
Compare
If we only have a message, there is not much value in returning it in a JSON. It becomes interesting when we have line number (and potentially position) as a field as well, because the editor could point at the line and position then. |
The first part until " to YamlThingDTO: " is generated by us, the next part is th eerror message from the library. Note that the part "at [Source: UNKNOWN; byte offset: #UNKNOWN] " has been removed in a recent merged PR. |
Sometimes the error requires it to be displayed as monospaced text (much like the code fence here)
But sometimes doing so would look awkward. Also the |
Signed-off-by: Laurent Garnier <lg.hc@free.fr>
I pushed the change of the error message. |
We can't remove it in general as it is more than useful when parsing a file containing several things. Regarding the final part "(through reference chain ...)", I am not sure it has any value ? I could also filter it if you agree. |
Shall we just remove it on the JS side, as demonstrated here?
Yes I agree. |
Yes, could be better. |
Done |
Signed-off-by: Laurent Garnier <lg.hc@free.fr>
29c7329
to
4465838
Compare
...core/src/main/java/org/openhab/core/io/rest/core/internal/fileformat/FileFormatResource.java
Outdated
Show resolved
Hide resolved
I am getting an error when trying to define a channel description.
The old structure:
|
Rather something to discuss outside this particular PR. The description was not something you can provide with the DSL syntax. |
I presume you aren't going to include it in this PR? If so, I will remove it from the UI for now. But this will mean a regression in the UI - with users able to edit them before but now won't be able to. Even though DSL doesn't support this syntax, the description won't be lost, due to the way the round-trip (parse - create) is done in the UI. So right now, even though user can't see/edit it in Code (YAML / DSL), the description is still there when they switch out of Code and into Channel editor, and they can still edit it there. |
Signed-off-by: Laurent Garnier <lg.hc@free.fr>
I will create a new PR as it is a change in the YAML thing syntax. |
Does it mean that this PR can be merged as is, and when this other PR is merged, it will automatically allow |
Yes |
I am closing this PR, a new PR #4793 covers its scope. |
Related to #4585
This PR only supports management of things.
It supports DSL and YAML file formats.
A first new API (POST /file-format/create) allows to create a file format (DSL or YAML) from a JSON object.
A second new API (POST /file-format/parse) allows to parse a file format (DSL or YAML) to a JSON object.
These 2 APIs should help Main UI displaying DSL and YAML file formats for things.