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

♻️ Improved jsonschema exporter #432

Merged

Conversation

sschleemilch
Copy link
Collaborator

@sschleemilch sschleemilch commented Nov 25, 2024

Improved the jsonschema exporter

  • Added typed arrays using items

    Vehicle:
      type: branch
      description: Vehicle
    
    Vehicle.Attribs:
      type: attribute
      description: Attribs
      datatype: string[]

    Before:

    {
      "$schema": "https://json-schema.org/draft/2020-12/schema",
      "title": "Vehicle",
      "type": "object",
      "description": "Vehicle",
      "additionalProperties": false,
      "properties": {
        "Attribs": {
          "description": "Attribs",
          "type": "array"
        }
      }
    }

    Now:

    {
      "$schema": "https://json-schema.org/draft/2020-12/schema",
      "title": "Vehicle",
      "type": "object",
      "description": "Vehicle",
      "properties": {
        "Attribs": {
          "type": "array",
          "description": "Attribs",
          "items": {
            "type": "string"
          }
        }
      },
    }
  • Added support for custom types a.k.a structs:

    Vehicle:
      type: branch
      description: Vehicle
    
    Vehicle.Doors:
      type: attribute
      description: Doors
      datatype: Types.Door[]
    Types:
      type: branch
      description: Types
    
    Types.Door:
      type: struct
      description: Door
    
    Types.Door.Windows:
      type: property
      description: Windows
      datatype: uint8
    {
      "$schema": "https://json-schema.org/draft/2020-12/schema",
      "title": "Vehicle",
      "type": "object",
      "description": "Vehicle",
      "properties": {
        "Doors": {
          "type": "array",
          "description": "Doors",
          "items": {
            "type": "object",
            "description": "Door",
            "properties": {
              "Windows": {
                "type": "integer",
                "description": "Windows",
                "minimum": 0,
                "maximum": 255
              }
            },
            "additionalProperties": false
          }
        }
      },
      "additionalProperties": false
    }
  • Including minimum and maximum automatically for types to express their max mins (only until uint/int64 exclusive)

    Vehicle:
      type: branch
      description: Vehicle
    
    Vehicle.Speed:
      type: sensor
      description: Speed
      datatype: int16
    {
      "$schema": "https://json-schema.org/draft/2020-12/schema",
      "title": "Vehicle",
      "type": "object",
      "description": "Vehicle",
      "properties": {
        "Speed": {
          "type": "integer",
          "description": "Speed",
          "minimum": -32768,
          "maximum": 32767
        }
      },
      "additionalProperties": false
    }
  • Fixed support for numeric

Added

  • Model check that min and max are in the range of the datatype
    Vehicle:
      type: branch
      description: Vehicle
    
    Vehicle.Something:
      type: sensor
      description: Something
      datatype: int8
      max: 300
      ❯ vspec export tree -s test.vspec
    [10:26:23] WARNING  No 'quantity' files defined. Default not existing: /Users/Q534774/workspace/vss-tools/quantities.yaml                                                                                                                             main.py:60
               WARNING  No 'unit' files defined. Default not existing: /Users/Q534774/workspace/vss-tools/units.yaml                                                                                                                                      main.py:66
               INFO     VSpecs loaded, amount=1                                                                                                                                                                                                         vspec.py:130
               CRITICAL 'Vehicle.Something' has 1 model error(s):                                                                                                                                                                                        main.py:234
                        [                                                                                                                                                                                                                                           
                            {                                                                                                                                                                                                                                       
                                'type': 'assertion_error',                                                                                                                                                                                                          
                                'loc': (),                                                                                                                                                                                                                          
                                'msg': "Assertion failed, max '300' is not an 'int8'",                                                                                                                                                                              
                                'input': {                                                                                                                                                                                                                          
                                    'fqn': 'Vehicle.Something',                                                                                                                                                                                                     
                                    'type': <NodeType.SENSOR: 'sensor'>,                                                                                                                                                                                            
                                    'description': 'Something',                                                                                                                                                                                                     
                                    'comment': None,                                                                                                                                                                                                                
                                    'delete': False,                                                                                                                                                                                                                
                                    'deprecation': None,                                                                                                                                                                                                            
                                    'constUID': None,                                                                                                                                                                                                               
                                    'fka': [],                                                                                                                                                                                                                      
                                    'instantiate': True,                                                                                                                                                                                                            
                                    'datatype': 'int8',                                                                                                                                                                                                             
                                    'max': 300                                                                                                                                                                                                                      
                                }                                                                                                                                                                                                                                   
                            }                                                                                                                                                                                                                                       
                        ]                                                                                                                                                                                                                                           
    

@sschleemilch sschleemilch force-pushed the feature/improved-jsonschema-exporter branch from c7e187b to 71757b9 Compare November 25, 2024 09:27
@erikbosch
Copy link
Collaborator

MoM:

  • Please review
  • Potential merge decision next week

Copy link
Collaborator

@SebastianSchildt SebastianSchildt left a comment

Choose a reason for hiding this comment

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

This makes sense to me, but I am also not using jsonschema currently so it does not break anything for me :)

Do we need backwards compatibility here? (I don't)

@erikbosch
Copy link
Collaborator

MoM:

  • Erik wants to review
  • Ok to merge if no remarks after the review from Erik

target_type = type_map[datatype]
target_type = type_map[datatype]
ref["type"] = target_type[0]
if len(target_type) > 1:
Copy link
Collaborator

Choose a reason for hiding this comment

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

As of today we expect "len" to be either 1 or 3, right? I.e. never anything else.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Right now it only will be 1 or 3. Yeah. Thought about specifying at least the lower boundary for uint64. Then we would have 2 :)

@sschleemilch
Copy link
Collaborator Author

Do we need backwards compatibility here? (I don't)

Nope, old ones were not including array restrictions and the new one is. So it will definitely break

Signed-off-by: Sebastian Schleemilch <[email protected]>
@sschleemilch sschleemilch force-pushed the feature/improved-jsonschema-exporter branch from 71757b9 to 9a85ec8 Compare December 4, 2024 12:19
Copy link
Collaborator

@erikbosch erikbosch left a comment

Choose a reason for hiding this comment

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

LGTM

@erikbosch erikbosch merged commit 526754f into COVESA:master Dec 6, 2024
5 checks passed
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.

3 participants