jsonschema-gentypes icon indicating copy to clipboard operation
jsonschema-gentypes copied to clipboard

Preserving case when generating $ref classes

Open eupharis opened this issue 1 year ago • 5 comments

Using Python 3.10.12 and version jsonschema-gentypes 2.4.0

So for this schema:

{
  "$schema": "http://json-schema.org/draft-06/schema#",
  "definitions": {
    "SubresourceUris": {
      "type": "object",
      "properties": {
        "feedback": {
          "type": "string"
        }
      },
      "required": ["feedback"],
      "title": "SubresourceUris"
    }
  },
  "type": "object",
  "properties": {
    "subresource_uris": {
      "$ref": "#/definitions/SubresourceUris"
    }
  },
  "required": [
    "subresource_uris"
  ],
  "title": "ResponseType"
}

when I run:

from jsonschema_gentypes.cli import process_config


config = {
    "python_version": None,
    "generate": [
      {
            "source": "./input.json",
            "destination": "./output.py",
        }
    ],
}
process_config(config)  # type: ignore

I get back the following:

from typing import TypedDict
from typing_extensions import Required


class Responsetype(TypedDict, total=False):
    """ ResponseType. """

    subresource_uris: Required["Subresourceuris"]
    """
    SubresourceUris.

    Required property
    """



class Subresourceuris(TypedDict, total=False):
    """ SubresourceUris. """

    feedback: Required[str]
    """ Required property """

Is there any way to have the output match the case of the input, and have the class be named SubresourceUris instead?

Love the library by the way! It's very nice.

eupharis avatar Jan 29 '24 20:01 eupharis

Yon can modify the schema SubresourceUris => Subresource_Uris Currently, there is no way to change the method that creates the name, we can imagine having a singleton that provides the get_name function.

sbrunner avatar Jan 30 '24 16:01 sbrunner

@sbrunner so like a custom_get_name config option?

looks like if we provided it as part of config in the api_arguments, it would already get passed to class API

https://github.com/sbrunner/jsonschema-gentypes/blob/df680536403ec8e7fd4e1afce5b14f690bad9e0b/jsonschema_gentypes/cli.py#L197

then if API called self.get_name it could use self.custom_get_name if defined then fallback to the normal get_name

seems like it could be a pretty minimal code change to support it!

eupharis avatar Jan 31 '24 17:01 eupharis

@sbrunner any interest in the change I proposed above? I'd be happy to work on a PR if you like the feature

eupharis avatar Mar 04 '24 21:03 eupharis

@eupharis I didn't catch every detail of your proposal, then I propose that you create a pull request, then we talk about it, does it look like ok to you?

sbrunner avatar Mar 05 '24 07:03 sbrunner

@sbrunner sounds good! will do!

eupharis avatar Mar 05 '24 14:03 eupharis