question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Missing support for "const" keyword

See original GitHub issue

Describe the bug

OpenAPI 3 references JSONSchema 2019-09 (since https://github.com/OAI/OpenAPI-Specification/pull/1977), which includes support for the const keyword. The const keyword is for defining constant value properties in schemas.

To Reproduce

Example schema (this is part of an OpenAPI specification):

{
  "components": {
    "schemas": {
      "Namespace": {
        "type": "object",
        "required": [
          "apiVersion",
          "kind"
        ],
        "properties": {
          "apiVersion": {
            "const": "v1"
          },
          "kind": {
            "const": "Namespace"
          },
          "metadata": {
            "$ref": "#/compoments/schemas/ObjectMeta"
          }
        }
      }
    }
  }
}

Used commandline:

$ datamodel-codegen --input openapi.json --input-file-type openapi --output models.py

Expected behavior

I expected the generated model to look something like this:

class Namespace
  apiVersion: str = "v1"
  kind: str = "Namespace"
  metadata: ObjectMeta

But what the cli currently generates is:

class Namespace:
  apiVersion: Any
  kind: Any
  metadata: ObjectMeta

Version:

  • OS: Fedora 35
  • Python version: 3.10.0
  • datamodel-code-generator version: 0.11.15

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:3
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
koxudaxicommented, Jan 11, 2022

@larsks I’m sorry for my late reply. Thank you for sharing the feature. I will add the feature to the cli.

1reaction
sjohnencommented, Oct 12, 2022

Is there any progress for the implementation of the “const” keyword?

Read more comments on GitHub >

github_iconTop Results From Across the Web

SyntaxError: missing = in const declaration - JavaScript | MDN
The JavaScript exception "missing = in const declaration" occurs when a const declaration was not given a value in the same statement (like ......
Read more >
Suggestion: Support ES6 'let' and 'const' keyword #19 - GitHub
Personally I like the idea of having const and readonly as an option. It may make things more complicated, but I like that...
Read more >
C++ const correctness / missing const constructor for class ...
To me it looks like I need to have a constructor for creating a const object, which isn't available in C++. So I...
Read more >
SyntaxError: missing = in const declaration - JavaScript
The JavaScript exception "missing = in const declaration" occurs when a const declaration was not given a value in the same statement (like...
Read more >
What is the 'const' Keyword in JavaScript? | by Mayank Gupta
The const keyword represents a constant value ... In the below example, we have created a variable “userName” which is a constant variable....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found