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

[BUG] Supprt JsonLD, allow property names @id, @type and @context (specifically for typescript-fetch) #13999

Open
5 of 6 tasks
Richard87 opened this issue Nov 12, 2022 · 2 comments

Comments

@Richard87
Copy link

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

Json-LD includes a few fields that starts with a @ sign, like @id, @type and ´@context, when using typescript-fetchthe@sign is stripped wich leads to invalid code (often duplicateid` properties).

I assumed modelPropertyNaming=original would work, but it had no effect.

openapi-generator version

npm: @openapitools/openapi-generator-cli 2.5.2

OpenAPI declaration file content or url
{
    "openapi": "3.0.0",
    "info": {
        "title": "Hello API Platform",
        "description": "",
        "version": "1.0.0"
    },
    "servers": [
        {
            "url": "/",
            "description": ""
        }
    ],
    "paths": {
        "/users/{id}": {
            "get": {
                "operationId": "api_users_id_get",
                "tags": [
                    "User"
                ],
                "responses": {
                    "200": {
                        "description": "User resource",
                        "content": {
                            "application/ld+json": {
                                "schema": {
                                    "$ref": "#/components/schemas/User.jsonld"
                                }
                            }
                        }
                    },
                    "404": {
                        "description": "Resource not found"
                    }
                },
                "summary": "Retrieves a User resource.",
                "description": "Retrieves a User resource.",
                "parameters": [
                    {
                        "name": "id",
                        "in": "path",
                        "description": "User identifier",
                        "required": true,
                        "deprecated": false,
                        "allowEmptyValue": false,
                        "schema": {
                            "type": "string"
                        },
                        "style": "simple",
                        "explode": false,
                        "allowReserved": false
                    }
                ],
                "deprecated": false
            },
            "parameters": []
        }
    },
    "components": {
        "schemas": {
            "User.jsonld": {
                "type": "object",
                "description": "",
                "deprecated": false,
                "properties": {
                    "@context": {
                        "readOnly": true,
                        "oneOf": [
                            {
                                "type": "string"
                            },
                            {
                                "type": "object",
                                "properties": {
                                    "@vocab": {
                                        "type": "string"
                                    },
                                    "hydra": {
                                        "type": "string",
                                        "enum": [
                                            "http://www.w3.org/ns/hydra/core#"
                                        ]
                                    }
                                },
                                "required": [
                                    "@vocab",
                                    "hydra"
                                ],
                                "additionalProperties": true
                            }
                        ]
                    },
                    "@id": {
                        "readOnly": true,
                        "type": "string"
                    },
                    "@type": {
                        "readOnly": true,
                        "type": "string"
                    },
                    "id": {
                        "readOnly": true,
                        "type": "string",
                        "format": "ulid"
                    },
                    "name": {
                        "type": "string"
                    },
                    "email": {
                        "type": "string"
                    },
                    "roles": {
                        "readOnly": true,
                        "type": "array",
                        "items": {
                            "type": "string"
                        }
                    },
                    "userIdentifier": {
                        "readOnly": true,
                        "type": "string"
                    }
                }
            }
        },
        "responses": {},
        "parameters": {},
        "examples": {},
        "requestBodies": {},
        "headers": {},
        "securitySchemes": {}
    },
    "security": [],
    "tags": []
}
Generation Details

I run this command as a php composer.json script:

        "generate-api": [
            "rm -Rf ../pwa/components/openapi",
            "bin/console api:openapi:export > openapi.json",
            "../pwa/node_modules/.bin/openapi-generator-cli generate -i openapi.json -o ../pwa/components/openapi -g typescript-fetch --additional-properties=supportsES6=true,npmVersion=6.9.0,typescriptThreePlus=true,allowUnicodeIdentifiers=true,disallowAdditionalPropertiesIfNotPresent=false,paramNaming=original,modelPropertyNaming=original,enumPropertyNaming=original,nullSafeAdditionalProps=true"
        ]
Steps to reproduce
// create openapi.json file
./node_modules/.bin/openapi-generator-cli generate -i openapi.json -o ./components/openapi -g typescript-fetch --additional-properties=supportsES6=true,npmVersion=6.9.0,typescriptThreePlus=true,allowUnicodeIdentifiers=true,disallowAdditionalPropertiesIfNotPresent=false,paramNaming=original,modelPropertyNaming=original,enumPropertyNaming=original,nullSafeAdditionalProps=true
Related issues/PRs

#5899

Suggest a fix
@Mauriceu
Copy link

Mauriceu commented Apr 13, 2023

Any news?

I guess one solution would be to set a different "serializedName" for those properties that intersect with hydra, granted that requires touching a lot of stuff

Edit:

Found a dirty but simple workaround that involves a custom generator.

First create the scaffolding for the custom generator:

  • openapi-generator-cli meta -o out/custom-generator -n custom-codegen

Now open the file located in custom-generator/src/main/java/company/codegen/CustomCodegenGenerator.java.
Replace everything but the first line with:

import org.openapitools.codegen.*;
import org.openapitools.codegen.languages.TypeScriptAngularClientCodegen;

public class CustomCodegenGenerator extends TypeScriptAngularClientCodegen implements CodegenConfig {
    public String getName() {
        return "custom-codegen";
    }

    @Override
    public String toVarName(String name) {
        switch(name) {
            case "@context":
            case "@id":
            case "@type":
                return ("\"" + name + "\"");
        }
        return super.sanitizeName(name);
    }
}

You basically just intercept any call to "toVarName" and make sure that certain properties are quoted and not passed to the sanitize-function.

Compile the generator by executing

  • mvn package

wherever the pom.xml is located in.

And then generate your Code with:

  • openapi-generator-cli generate --custom-generator generator/custom-generator/target/custom-codegen-openapi-generator-1.0.0.jar -g custom-codegen -i schema.yaml -o out/

One could go ahead and add all other hydra-context properties to the switch, or just check if an @ is at the beginning of "name", though that may catch unwanted properties as well.

@PierreTraversFamileo
Copy link

i have the same issues thank's for the work arroud

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants