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

Missing support for "const" keyword #658

Closed
larsks opened this issue Dec 16, 2021 · 6 comments · Fixed by #989
Closed

Missing support for "const" keyword #658

larsks opened this issue Dec 16, 2021 · 6 comments · Fixed by #989
Labels
enhancement New feature or request

Comments

@larsks
Copy link

larsks commented Dec 16, 2021

Describe the bug

OpenAPI 3 references JSONSchema 2019-09 (since OAI/OpenAPI-Specification#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
@larsks
Copy link
Author

larsks commented Dec 16, 2021

Or I guess more correctly:

class Namespace
  apiVersion: str = Field("v1", cont=True)
  kind: str = Field("Namespace", const=True)
  metadata: ObjectMeta

@koxudaxi
Copy link
Owner

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

@koxudaxi koxudaxi added the enhancement New feature or request label Jan 11, 2022
@bharathimohan11
Copy link

The generated model file should use Literal Instead of const.
Refer : pydantic/pydantic#561

@Sergei-Volkov
Copy link

@koxudaxi Hi! Are there any updates on adding this feature to datamodel-codegen?

@sjohnen
Copy link

sjohnen commented Oct 12, 2022

Is there any progress for the implementation of the "const" keyword?

@koxudaxi
Copy link
Owner

koxudaxi commented Jan 4, 2023

@larsks @bharathimohan11 @Sergei-Volkov @sjohnen
I'm sorry for my too-late reply.
I have released the PR as 0.15.0
Thank you very much!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants