forked from springwolf/springwolf-core
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): add SchemaTitleModelConverter
extracted from springwolf#890 Co-authored-by: David Beck <[email protected]>
- Loading branch information
Showing
5 changed files
with
50 additions
and
3 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
...java/io/github/springwolf/core/asyncapi/schemas/converters/SchemaTitleModelConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package io.github.springwolf.core.asyncapi.schemas.converters; | ||
|
||
import com.fasterxml.jackson.databind.JavaType; | ||
import io.swagger.v3.core.converter.AnnotatedType; | ||
import io.swagger.v3.core.converter.ModelConverter; | ||
import io.swagger.v3.core.converter.ModelConverterContext; | ||
import io.swagger.v3.core.util.Json; | ||
import io.swagger.v3.core.util.PrimitiveType; | ||
import io.swagger.v3.oas.models.media.Schema; | ||
import lombok.NoArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import java.util.Iterator; | ||
|
||
@NoArgsConstructor | ||
@Slf4j | ||
public class SchemaTitleModelConverter implements ModelConverter { | ||
@Override | ||
public Schema resolve(AnnotatedType type, ModelConverterContext context, Iterator<ModelConverter> chain) { | ||
JavaType javaType = Json.mapper().constructType(type.getType()); | ||
if (chain.hasNext()) { | ||
Schema<?> schema = chain.next().resolve(type, context, chain); | ||
boolean isPrimitiveType = PrimitiveType.createProperty(type.getType()) != null; | ||
if (schema != null && !isPrimitiveType) { | ||
if (schema.get$ref() != null) { | ||
Schema<?> definedModel = context.resolve(type); | ||
if (definedModel != null && definedModel.getTitle() == null) { | ||
definedModel.setTitle(javaType.getRawClass().getSimpleName()); | ||
} | ||
} | ||
} | ||
return schema; | ||
} | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters