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

Generate wrong swagger when use union type #880

Closed
honguyenhaituan opened this issue Apr 20, 2024 · 7 comments
Closed

Generate wrong swagger when use union type #880

honguyenhaituan opened this issue Apr 20, 2024 · 7 comments
Assignees
Labels
invalid This doesn't seem right question Further information is requested wontfix This will not be worked on

Comments

@honguyenhaituan
Copy link

Summary

Generate wrong swagger when use union type

  • SDK Version: 3.0.5

I have a query dto like below:

type Query = {
    platform: 'android' | 'ios';
};

but when I generate swagger what I receive is

"schemas": {
	"Query": {
		"type": "object",
		"properties": {
			"platform": {
				"oneOf": [{ "const": "android" }, { "const": "ios" }]
			}
		},
		"required": ["platform"]
	}
},

and I found nothing about key word "const" in Open API document https://swagger.io/specification/.

In nestia version 2.6.4, I receive enum instead oneOf

"Query": {
      "type": "object",
      "properties": {
	      "platform": { "type": "string", "enum": ["android", "ios"] }
      },
      "nullable": false,
      "required": ["platform"]
}
@samchon
Copy link
Owner

samchon commented Apr 20, 2024

https://json-schema.org/understanding-json-schema/reference/const

It is a spec of JSON schema of very wrong time ago

Also, OpenApi 3.1 supports it.

OAI/OpenAPI-Specification#1666 (comment)

@samchon samchon self-assigned this Apr 20, 2024
@samchon samchon added question Further information is requested invalid This doesn't seem right wontfix This will not be worked on labels Apr 20, 2024
@loucass003
Copy link
Contributor

loucass003 commented Jul 10, 2024

I am encountering issues with this currently.
IMO this is a bigger deal than it looks like as it looks like most openapi code generators do not parse it correctly yet, especially big ones like https://github.com/fabien0102/openapi-codegen or https://github.com/AndrejNemec/openapi-tanstack-query-solid/tree/master (currently the only tenstackquery codegen for solidjs)

they both generates Types that looks like this

export type FilteredUserDTOOrderBy = unknown | unknown;

// i also got this on other libs
export type FilteredUserDTOOrderBy = void | void;

instead of

export type FilteredUserDTOOrderBy = 'id' | 'email' ;

Idk if something can be done on your end. Maybe a setting toggle for the enum syntax instead of const?

@honguyenhaituan
Copy link
Author

@loucass003 I use this lib https://github.com/hey-api/openapi-ts and it generate ts correctly

@loucass003
Copy link
Contributor

loucass003 commented Jul 10, 2024

Good to know, sadly for me i would like to use tenstask query with solijds. there is tenstack query generators for react but only one for solid and they use an openapi lib that is not parsing those unions correctly 😭

i can understand how this is none of netia business but this greadly reduce compatibility with other libs that havent implemented that syntax. If it is decided that nestia wont move from the const syntax i will forward this issue to the other openapi generator libs

@samchon
Copy link
Owner

samchon commented Jul 11, 2024

https://github.com/samchon/openapi

@honguyenhaituan @loucass003 I think both of you want OpenAPI v3.0 document.

You can convert to it manually through above library.

Also, how do you think about providing OpenAPI version specification option in the nestia.config.ts file?

import { INestiaConfig } from "@nestia/sdk";
import { NestFactory } from "@nestjs/core";

import { AppModule } from "./src/AppModule";

export const NESTIA_CONFIG: INestiaConfig = {
  input: () => NestFactory.create(AppModule),
  swagger: {
    openapi: "3.0", // "2.0" | "3.0" | "3.1"
    output: "packages/api/swagger.json",
    beautify: true,
  },
};
export default NESTIA_CONFIG;

@samchon samchon reopened this Jul 11, 2024
@loucass003
Copy link
Contributor

oh wow ok. problem solved then. thank you very much @samchon again for this amazing tool that you provide <3

samchon added a commit that referenced this issue Jul 12, 2024
Close #880: support multiple openapi versions' generation.
@samchon
Copy link
Owner

samchon commented Jul 12, 2024

Upgrade to v3.7.0, then you can do it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
invalid This doesn't seem right question Further information is requested wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

3 participants