Skip to content

additionalProperties is not correctly defaulted to true #583

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

Closed
mehalter opened this issue May 5, 2021 · 5 comments · Fixed by #585
Closed

additionalProperties is not correctly defaulted to true #583

mehalter opened this issue May 5, 2021 · 5 comments · Fixed by #585

Comments

@mehalter
Copy link
Contributor

mehalter commented May 5, 2021

I am running into an issue where some of my openapi specifications are not generating to the correct typescript object because they do not include the additionalProperties. According to the OpenAPI specification this is default to true (https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.2.md):

additionalProperties - Value can be boolean or object. Inline or referenced schema MUST be of a Schema Object and not a standard JSON Schema. Consistent with JSON Schema, additionalProperties defaults to true.

With this being the case, shouldn't it automatically add the union with & { [key: string]: any } unless additionalProperties is set to something else i.e. a schema or false?

When I run the following openapi definition:

{
    "openapi": "3.0.0",
    "info": {
        "title": "Generated by cue.",
        "version": "no version"
    },
    "paths": {},
    "components": {
        "schemas": {
            "Blueprint": {
                "$ref": "#/components/schemas/Contract"
            },
            "Contract": {
                "type": "object",
                "required": [
                    "slug"
                ],
                "properties": {
                    "slug": {
                        "type": "string"
                    }
                }
            }
        }
    }
}

I get the typescript:

export interface paths {}

export interface components {
	schemas: {
		Blueprint: components['schemas']['Contract'];
		Contract: {
			slug: string;
		};
	};
}

export interface operations {}

where it should be Contract: { slug: string; } & { [key: string]: any }

Thanks for your help and all the work put into this project!

@mehalter
Copy link
Contributor Author

mehalter commented May 5, 2021

Also to note this only seems to be a part of OpenAPI 3 and not OpenAPI 2

@mikehalmamoj
Copy link

Hmmm is this right? It also says: This object cannot be extended with additional properties and any properties added SHALL be ignored. (https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.2.md#fixed-fields-19)

Admittedly I'm just sulking because my linter doesn't like any 😄

@drwpow
Copy link
Contributor

drwpow commented May 18, 2021

Hmmm is this right? It also says: This object cannot be extended with additional properties and any properties added SHALL be ignored.

Yeah this behavior should be behind a flag. I’m not sure which should be the default behavior, but it should be controllable. I’m kinda leaning toward the & { [key: string]: any } as the default, just because it’s more lenient / probably works for more people, but don’t feel strongly.

@mikehalmamoj
Copy link

As we're in control of the server side too, I think we'll probably set that additionalProperties flag to false. It's strange to default to true though - "Here is my contract (but I might stick a bunch of random properties on the end if I feel like it)" - that's not my idea of a contract 😄

+1 for putting this behaviour behind a flag. Agree you should be more lenient with the default.

@drwpow
Copy link
Contributor

drwpow commented May 18, 2021

It's strange to default to true though - "Here is my contract (but I might stick a bunch of random properties on the end if I feel like it)" - that's not my idea of a contract 😄

Phrasing it like that, perhaps it is better default off. It may be something that people aren’t expecting, and may bite them if they’re not aware of it. OK I’ll do that—introduce the flag, then make the default behavior what it used to be before this last release. That shouldn’t be too disruptive seeing as this is relatively new, and you can always opt in if needed.

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

Successfully merging a pull request may close this issue.

3 participants