Comments (6)
Note, this is somewhat related to #37. The fact that schemas get merged component by component leads to this situation where the same schema, used in to different places in the API, gets a different name even though the underlying schema is the same. I think maintaining a registration of discovered schemas across all types components would eliminate this. The full name (t.PkgPath()
+t.Name()
) can serve as a fully qualified name that is unique. By having a registry of schemas, we can a) support $ref
and b) avoid the duplication mentioned in #37.
Is there interest in addressing this?
from huma.
A quick followup. I implemented this. I have a single function in the the schema
package that takes a reflect.Type
and returns a string representation. To continue with the current behavior, that function could be implemented to return simple t.Name()
. But since I have gone through and identified all places where t.Name()
was previously used and replaced them with calls to schema.SchemaName(t)
, now we can try different name strategies by changing only a single line of code.
What I've found is that returning fmt.Sprintf("%s.%s", strings.ReplaceAll(t.PkgPath(), "/", "$"), t.Name())
works quite well. There is no issue with keeping the /
characters in there from an OpenAPI perspective. But what I found is that there are several places where $ref
s are inserted of the form ./<SchemaName>
and if you have /
s in the <SchemaName>
, they don't resolve properly. But by replacing them with $
s things work ok.
I ran the generated openapi.json
through both restish
and oazapfts
just to ensure those tools don't trip on anything. Things look good. There are a few cases where $ref
s get replaced with .
that could trip things up.
To be honest, I think a separate refactoring to keep a catalog of schemas (as they are generated) would be better. Currently these are collected on a resource by resource basis which seems to be creating circumstances where you end up (when merging the spec together) with multiple schemas with the same name (even with the disambiguation I'm talking about here). An API wide registry would be better I think but the current approach seems to work nonetheless.
You can find the implementation using t.PkgPath()
here. If you are OK with this, I'll submit this as an additional PR.
Comments?
from huma.
Thanks, I will look into this issue and the PR ASAP!
from huma.
Just to be clear, the PR #86 is not a fix for this. That PR completes a feature I added with a previous PR (corner case I hadn't really observed previously). This is another issue although still related to how schemas are managed. I don't have a PR per se but I've published changes that address this to a limited extent. If you want that as a PR, I can certainly submit it. But as I pointed out with my reference to #37, there is a deeper issue here with not having a "global" registry of schemas so it might be better to dig a bit deeper formulate a global registry of schemas to solve both #37 and #87 (this issue).
from huma.
(sorry it is confusing that I'm both @mtiller and @mt35-rs, it all depends on whether I'm on my work computer or home computer)
from huma.
This was partially resolved in Huma v1 and the registry/schema system was reworked in v2. If you continue to use the same names for structs between different packages, you can now provide a custom schema namer function and resolve that however you like, e.g. by using the full package + struct name.
from huma.
Related Issues (20)
- Operation Specific Security Policies HOT 1
- How do you switch to v2? HOT 2
- Calling middleware.SetLoggerInContext in Middlewares HOT 1
- V2: Embedded structs are not inlined in the OpenAPI Spec HOT 1
- Validating JSON outside of a request HOT 3
- PUT and POST on humafiber always results in 400: Bad Request response HOT 1
- Schema: anyOf, allOf, oneOf, not HOT 1
- String Array Input Inconsistency HOT 1
- Migration to V2 using V1 Middleware and chi HOT 1
- Support nullable type HOT 2
- [gorilla/mux] Same route with different methods does not work HOT 2
- Better support for offline HOT 3
- Route grouping/sub routing HOT 4
- doc and description tags HOT 10
- How to document the enpoints with jwt authentication HOT 2
- Response Body can not be recognised in other directory and package HOT 1
- Best practice for supporting multiple versions of the same API HOT 1
- Unsupported param type []int on query parameters
- Value attached to request context in chi middleware is not available in context in huma operation HOT 1
- Best practice to use other services (e.g. db client, s3 client) in route handler HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from huma.