pearmaster / json-schema-codegen Goto Github PK
View Code? Open in Web Editor NEWGenerate C++ or Python3 code from JSON-Schema
License: GNU General Public License v2.0
Generate C++ or Python3 code from JSON-Schema
License: GNU General Public License v2.0
If an array schema directly contains an object schema as its items, then it won't always codegen.
This doesn't work:
type: array
items:
type: object
properties:
foo:
type: integer
required: [foo]
This does work:
type: array
items:
$ref: "../theObjectSchema"
According to the JSON-Schema spec, an anyOf
schema combinator enforces one or more of the specified schemas. Currently, in this project anyOf
allows for zero or more.
The consequence for this is that the code will have a false positive (accepts without error when it should throw an error) when zero sub-schemas are provided.
Generate(self,
schema: {__contains__},
root: Any,
class_name: Any,
filename_base: Any) -> Union[str, bytes]
Template is here
def __init__(self, data=None, **kwargs):
"""Initialization for the {{Name}} object.
It can be initialized with an object, or by passing each
object property as a keyword argument.
"""
new_data = {}
{%-for propName, propSchema in schema.properties.items()%}
try:
prop = data["{{propName}}"] if ("{{propName}}" in data) else kwargs["{{propName}}"]
The last line should read:
prop = data["{{propName}}"] if (data is not None and "{{propName}}" in data) else kwargs["{{propName}}"]
Otherwise when data is None, it will fail every time.
Please update examples, I have no idea how to use the root argument with generator.Generate. Also your examples with Python are very difficult to use - I'm not sure which is the class_name and which is filename_base.
Hi there!
My understanding is that the output of json-schema-codegen constitutes a derivative work of the templates in the source repository and the templates are licensed under GPL. That makes the generated code also fall under the GPL.
And even if I write my own templates (licensed under a different license), I am not convinced that this will hold water legally. Or that the templates would not constitute a derivative work themselves and fall under GPL themselves.
My questions is:
When you chose GPL as license, were you aware that that choice will also transmit to the generated code? Was this a conscious decision?
In case this was a deliberate decision, would you consider adding a Bison-style exception[1] to json-schema-codegen, or change the license to a more permissive, non-copyleft license like MIT?
Thanks a lot for your work on json-schema-codegen and your consideration of this issue!
Tobias
When installing via pip install json-schema-codegen
, the following errors occur
ERROR: Could not find a version that satisfies the requirement jacobsjinjatoo (from json-schema-codegen->-r requirements.txt (line 1)) (from versions: none)
ERROR: No matching distribution found for jacobsjinjatoo (from json-schema-codegen->-r requirements.txt (line 1))
Appears jacobsjinjatoo project is listed as jacobs-jinja-too
in PyPI
Hi,
I'm trying to parse JSON Schema for OpenAPI 3 specification, and I'm getting a most cryptic error coming somewhere from jinja or a template.
Any hints on what could cause the problem?
File "/Users/matte/Documents/Projects/lapis/src/lapis/__main__.py", line 9, in main
generator.Generate(yaml.safe_load(fp), 'Example', 'example', 'example')
File "/Users/matte/Library/Caches/pypoetry/virtualenvs/lapis-4Aax896p-py3.10/lib/python3.10/site-packages/jsonschemacodegen/python.py", line 51, in Generate
return generator.render_template(template_name="file.py.jinja2", output_name="{}.py".format(filename_base), resolver=self.resolver, **args)
File "/Users/matte/Library/Caches/pypoetry/virtualenvs/lapis-4Aax896p-py3.10/lib/python3.10/site-packages/jacobsjinjatoo/templator.py", line 92, in render_template
rendered = template.render(kwargs)
File "/Users/matte/Library/Caches/pypoetry/virtualenvs/lapis-4Aax896p-py3.10/lib/python3.10/site-packages/jinja2/environment.py", line 1304, in render
self.environment.handle_exception()
File "/Users/matte/Library/Caches/pypoetry/virtualenvs/lapis-4Aax896p-py3.10/lib/python3.10/site-packages/jinja2/environment.py", line 925, in handle_exception
raise rewrite_traceback_stack(source=source)
File "/Users/matte/Library/Caches/pypoetry/virtualenvs/lapis-4Aax896p-py3.10/lib/python3.10/site-packages/jsonschemacodegen/templates/python/file.py.jinja2", line 8, in <module>
{{importer.GetImports(resolver, schema, includes)}}
File "/Users/matte/Library/Caches/pypoetry/virtualenvs/lapis-4Aax896p-py3.10/lib/python3.10/site-packages/jinja2/runtime.py", line 828, in _invoke
rv = self._func(*arguments)
File "/Users/matte/Library/Caches/pypoetry/virtualenvs/lapis-4Aax896p-py3.10/lib/python3.10/site-packages/jsonschemacodegen/templates/python/import.py.jinja2", line 67, in <module>
{%- do includes.append(resolver.py_include_statement(prop['$ref']))-%}
File "/Users/matte/Library/Caches/pypoetry/virtualenvs/lapis-4Aax896p-py3.10/lib/python3.10/site-packages/jinja2/utils.py", line 84, in from_obj
if hasattr(obj, "jinja_pass_arg"):
jinja2.exceptions.UndefinedError: 'None' has no attribute 'py_include_statement'
#include "/exceptions.hpp"
for empty cpp_get_lib_ns()
and SimpleResolver
always generates empty list.I tried both the short example function from the README page, and examples.py. Neither worked. See attached.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.