Skip to content
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

feat: add schemaLocation if present #68

Merged
merged 9 commits into from
Sep 10, 2024
Merged

Conversation

HassanAkbar
Copy link
Member

@HassanAkbar HassanAkbar commented Sep 6, 2024

Automatically support xsi:schemaLocation for all XML elements if present in XML

closes #64

@HassanAkbar HassanAkbar marked this pull request as ready for review September 6, 2024 13:10
@ronaldtse ronaldtse force-pushed the add_schema_location_by_default branch from e09547b to e4d2b50 Compare September 8, 2024 11:20
Copy link
Contributor

@ronaldtse ronaldtse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@HassanAkbar I've added a README and some specs to test for multiple schemaLocations. As you can see here, there could be multiple schemaLocations, and we need to support that. Can you help revise and update the feature/specs?

@HassanAkbar
Copy link
Member Author

@ronaldtse I've updated the code according to the specs you added, I just changed how the schemaLocation object was being created in the test cases that you added.
Can you check that and let me know if that is correct or do I need to change that? Thanks

@ronaldtse
Copy link
Contributor

Somehow this is failing...

class Ceramic < Lutaml::Model::Serializable
  attribute :type, :string
  attribute :glaze, :string
  attribute :color, :string

  xml do
    root 'Ceramic'
    namespace 'http://example.com/ceramic', 'cera'
    map_element 'Type', to: :type, namespace: :inherit
    map_element 'Glaze', to: :glaze
    map_attribute 'color', to: :color, namespace: 'http://example.com/color', prefix: 'clr'
  end
end

xml = <<~HERE
<cera:Ceramic
  xmlns:cera="http://example.com/ceramic"
  xmlns:clr="http://example.com/color"
  xsi:schemaLocation=
    "http://example.com/ceramic http://example.com/ceramic.xsd
     http://example.com/color http://example.com/color.xsd"
  clr:color="navy-blue">
  <cera:Type>Porcelain</cera:Type>
  <Glaze>Clear</Glaze>
</cera:Ceramic>
HERE

Ceramic.from_xml(xml)

@HassanAkbar any idea?

@ronaldtse
Copy link
Contributor

Actually it works! Sorry!!

@ronaldtse
Copy link
Contributor

No actually it doesn't?

$ bundle console
# ...
require_relative "../lib/lutaml/model/xml_adapter/nokogiri_adapter"
Lutaml::Model::Config.configure do |config|
  config.xml_adapter_type = :nokogiri
end

class Ceramic < Lutaml::Model::Serializable
  attribute :type, :string
  attribute :glaze, :string
  attribute :color, :string

  xml do
    root 'Ceramic'
    namespace 'http://example.com/ceramic', 'cera'
    map_element 'Type', to: :type, namespace: :inherit
    map_element 'Glaze', to: :glaze
    map_attribute 'color', to: :color, namespace: 'http://example.com/color', prefix: 'clr'
  end
end

xml_content = <<~HERE
<cera:Ceramic
  xmlns:cera="http://example.com/ceramic"
  xmlns:clr="http://example.com/color"
  xsi:schemaLocation=
    "http://example.com/ceramic http://example.com/ceramic.xsd
     http://example.com/color http://example.com/color.xsd"
  clr:color="navy-blue">
  <cera:Type>Porcelain</cera:Type>
  <Glaze>Clear</Glaze>
</cera:Ceramic>
HERE

a = Ceramic.from_xml(xml_content)
a.schema_location
# => nil

@ronaldtse
Copy link
Contributor

@ronaldtse I've updated the code according to the specs you added, I just changed how the schemaLocation object was being created in the test cases that you added. Can you check that and let me know if that is correct or do I need to change that? Thanks

The tests are just illustrative but the way it works seems fine to me.

@HassanAkbar
Copy link
Member Author

No actually it doesn't?

$ bundle console
# ...
require_relative "../lib/lutaml/model/xml_adapter/nokogiri_adapter"
Lutaml::Model::Config.configure do |config|
  config.xml_adapter_type = :nokogiri
end

class Ceramic < Lutaml::Model::Serializable
  attribute :type, :string
  attribute :glaze, :string
  attribute :color, :string

  xml do
    root 'Ceramic'
    namespace 'http://example.com/ceramic', 'cera'
    map_element 'Type', to: :type, namespace: :inherit
    map_element 'Glaze', to: :glaze
    map_attribute 'color', to: :color, namespace: 'http://example.com/color', prefix: 'clr'
  end
end

xml_content = <<~HERE
<cera:Ceramic
  xmlns:cera="http://example.com/ceramic"
  xmlns:clr="http://example.com/color"
  xsi:schemaLocation=
    "http://example.com/ceramic http://example.com/ceramic.xsd
     http://example.com/color http://example.com/color.xsd"
  clr:color="navy-blue">
  <cera:Type>Porcelain</cera:Type>
  <Glaze>Clear</Glaze>
</cera:Ceramic>
HERE

a = Ceramic.from_xml(xml_content)
a.schema_location
# => nil

@ronaldtse This is happening because the xmlns:xsi is not defined and nokogiri is treating xis:schemaLocation as a attribute name and not xsi as prefix and schemaLocation as attribute name which we are expecting.

I'll update the code to handle this scenario.

@ronaldtse
Copy link
Contributor

Ahh @HassanAkbar yes you are right. We need to define the namespace xsi first. My example XML was mistaken. It’s fine then.

@HassanAkbar
Copy link
Member Author

@ronaldtse Then I think this is done. Is there something else left to be done in this PR?

@ronaldtse
Copy link
Contributor

This is all good, I've tested it with the (updated) sample in the README. Merging once tests pass.

@ronaldtse ronaldtse merged commit f2d2796 into main Sep 10, 2024
13 of 14 checks passed
@ronaldtse ronaldtse deleted the add_schema_location_by_default branch September 10, 2024 23:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Automatically support xsi:schemaLocation for all XML elements
2 participants