Skip to content

refactor: introduce function for getting Schema Object type #10330

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

Merged
merged 17 commits into from
May 26, 2025

Conversation

char0n
Copy link
Member

@char0n char0n commented Feb 28, 2025

use function for getting schema object type/schema object type label depending on the OpenAPI version


Attribution: @robert-hebel-sb


export const getSchemaObjectTypeLabel = (label) => label

export const getSchemaObjectType = (schema) => schema.type ?? "any"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be consistent with JSON Schema 2020-12 processing, can we make the fallback type the string type?

Suggested change
export const getSchemaObjectType = (schema) => schema.type ?? "any"
export const getSchemaObjectType = (schema) => schema.type ?? "string"

Also, may I suggest to make this function safe:

Suggested change
export const getSchemaObjectType = (schema) => schema.type ?? "any"
export const getSchemaObjectType = (schema) => schema?.type ?? "string"

Copy link
Member Author

@char0n char0n Mar 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Preferred alternative: What if both the functions would have the following signature?

getSchemaObjectType: (schema: object) -> string
getSchemaObjectTypeLabel: (schema: object) -> string

This would simplify their usage inside the code as it is now abstracted how the the type or label are computed from the schema.

Instead of

const typeLabel = fn.getSchemaObjectTypeLabel(immutableToJS(schema?.get("type")))

you'll end up having

const typeLabel = fn.getSchemaObjectTypeLabel(immutableToJS(schema))

If you further abstract and allow the functions to consume immutable along with POJO, it can look like:

const typeLabel = fn.getSchemaObjectTypeLabel(schema)

NOTE: this applies both for Draft 5 and 2020-12 functions.

@@ -635,3 +635,7 @@ const resolver = (arg1, arg2, arg3) => [arg1, JSON.stringify(arg2), JSON.stringi
export const memoizedCreateXMLExample = memoizeN(createXMLExample, resolver)

export const memoizedSampleFromSchema = memoizeN(sampleFromSchema, resolver)

export const getSchemaObjectTypeLabel = (label) => label
Copy link
Member Author

@char0n char0n Mar 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The input is type not a label.

Suggested change
export const getSchemaObjectTypeLabel = (label) => label
export const getSchemaObjectTypeLabel = (type) => type

But anyway we can make this point-free by:

import identity from "lodash/identity"

export const getSchemaObjectTypeLabel = identity

const typeLabel = fn.jsonSchema202012.getType(immutableToJS(schema))
const type = fn.jsonSchema202012.foldType(immutableToJS(schema?.get("type")))
const itemType = fn.jsonSchema202012.foldType(immutableToJS(schema?.getIn(["items", "type"])))
const typeLabel = fn.getSchemaObjectType(immutableToJS(schema))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks a bit confusing. For getting a typeLabel we're using getSchemaObjectType. For actual type we're using getSchemaObjectTypeLabel. Shouldn't it be reversed?

@robert-hebel-sb robert-hebel-sb force-pushed the feat/schema-object-type-func branch from 536f636 to c70aaaf Compare May 5, 2025 10:14
@@ -154,3 +155,11 @@ export const inferType = (schema, processedSchemas = new WeakSet()) => {
export const getType = (schema) => {
return inferType(schema)
}

export const getFoldType = (schema) => {
if (isImmutable(schema)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

immutableToJS has the isImmutable check, so we should be able to skip checking it here.

Also, should we have a separate variable for the transformed schema, instead of reassigning?

Same for other places where this occurs.

@robert-hebel-sb robert-hebel-sb requested a review from glowcloud May 14, 2025 11:36
@robert-hebel-sb robert-hebel-sb requested a review from glowcloud May 16, 2025 09:44
Copy link
Contributor

@glowcloud glowcloud left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we also revert the changes to unrelated files, e.g. operation-tag? If not possible, then we can leave them as is.

@Mathias02
Copy link

This branch is out-of-date with main. Please rebase or merge the latest changes so we can check if the function works or not"

@robert-hebel-sb robert-hebel-sb merged commit 01c41b2 into master May 26, 2025
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy