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

Fix generation for swaggers with oneOf construct, objects with null properties and inline schemas #12

Merged
merged 24 commits into from
Jun 7, 2024
Merged
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
76e00f6
Prima implementazione fix generazione oneOf
May 28, 2024
a35edc0
Gestione array type object witouth props
May 28, 2024
6ed5f7f
Prima implementazione fix generazione con schema inline
May 28, 2024
880c9d5
Merge branch 'fix/objWithoutProps' into fix/oneOfGeneration
May 28, 2024
da7ae07
Add .iml a gitignore
May 28, 2024
8e0c4fc
Implementazione test per oggetti con properties vuote
May 28, 2024
06ac8da
forcing additionalProps to true in case of type:object without props
May 28, 2024
54cf337
Merge commit '06ac8da2fcde59ae5f205ce14d1aef8e74afe81e' into fix/oneO…
May 28, 2024
08fefb7
Aggiornamento swagger di test
May 28, 2024
016f7d8
fix forcing additionalProperties to true with grafted objs
May 28, 2024
d9e2d18
Merge commit '016f7d8c46b815b615454c51838bec6be3338184' into fix/oneO…
May 28, 2024
3082663
Add .iml a gitignore
May 29, 2024
dbbaf4b
Implementazione test per generazione con properties vuote
May 29, 2024
046296c
Fix swagger di test
May 29, 2024
a76cb6a
Merge branch 'fix/objWithoutProps' into develop
May 29, 2024
9d87d8f
Merge branch 'fix/oneOfGeneration' into develop
May 29, 2024
eeeb0a7
Update changelog per merge feature
May 29, 2024
46d0963
Fix nomi json schema generati
May 29, 2024
8f1df52
Merge branch 'fix/schemaInOperation' into develop
May 29, 2024
a740639
Fix errori di generazione in caso di swagger con schema inline
May 30, 2024
af24f49
Aggiunta voci changelog
May 30, 2024
97ef060
Fix date changelog
May 30, 2024
1231132
Fix mancato uso della clausola "strict" per additionalProperties
May 31, 2024
0ace175
Delete openapi2jsonschema4j.iml
gcornacchia Jun 7, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix nomi json schema generati
natti committed May 29, 2024
commit 46d0963b736b8e68db07d73c19fba23a60ef9cef
Original file line number Diff line number Diff line change
@@ -39,17 +39,20 @@ public class BaseJsonSchemaGenerator {
protected static final String ITEMS = "items";
protected static final String TYPE = "type";
@Getter
protected Set<String> messageObjects = new HashSet<String>();
protected Map<String,String> messageObjects = new HashMap<String,String>();
@Getter
private Map<String, Schema> objectsDefinitions = new HashMap<String, Schema>();

protected boolean isSwaggerFlattened = false;


protected void readFromInterface(File interfaceFile) {
ParseOptions po = new ParseOptions();
po.setResolve(true);
SwaggerParseResult result = new OpenAPIParser().readLocation(interfaceFile.getAbsolutePath(),null,po);
OpenAPI swagger = result.getOpenAPI();
if (swagger.getComponents() == null) {
isSwaggerFlattened = true;
log.info("Components missing. Trying again with flatten=true in case of inline schemas.");
po.setFlatten(true);
result = new OpenAPIParser().readLocation(interfaceFile.getAbsolutePath(),null,po);
@@ -75,7 +78,12 @@ private void analyzeOperation(PathItem v) {
if (r.getContent().get(APPLICATION_JSON) != null) {
if (r.getContent().get(APPLICATION_JSON).getSchema().get$ref() != null) {
log.info("code={} responseSchema={}", key, r.getContent().get(APPLICATION_JSON).getSchema().get$ref());
messageObjects.add(r.getContent().get(APPLICATION_JSON).getSchema().get$ref());
if (isSwaggerFlattened) {
String t = op.getOperationId()+"response"+key;
messageObjects.put(r.getContent().get(APPLICATION_JSON).getSchema().get$ref(),t);
}else{
messageObjects.put(r.getContent().get(APPLICATION_JSON).getSchema().get$ref(),"");
}
} else {
log.warn("code={} response schema is not a referenced definition! type={}", key, r.getContent().get("application/json").getClass());
}
@@ -85,14 +93,19 @@ private void analyzeOperation(PathItem v) {
}
}

private void findRequestBodySchema(Operation op, Set<String> messageObjects) {
private void findRequestBodySchema(Operation op, Map<String,String> messageObjects) {
if (op.getRequestBody()!=null) {
if (op.getRequestBody().getContent().get(APPLICATION_JSON)!=null) {
Schema sc = op.getRequestBody().getContent().get(APPLICATION_JSON).getSchema();
if (sc != null) {
log.info("Request schema={}", sc.get$ref());
if (sc.get$ref()!=null) {
messageObjects.add(sc.get$ref());
if (isSwaggerFlattened) {
String t = op.getOperationId()+"request";
messageObjects.put(sc.get$ref(),t);
}else{
messageObjects.put(sc.get$ref(),"");
}
} else {
log.warn("Request schema is not a referenced definition!");
}
Original file line number Diff line number Diff line change
@@ -7,6 +7,8 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.datatype.jsr310.*;

import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.media.*;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
@@ -65,7 +67,7 @@ public DraftV4JsonSchemaGenerator(boolean strict) {
}

private Map<String, JsonNode> generateForObjects() throws Exception {
for (String ref : getMessageObjects()) {
for (String ref : getMessageObjects().keySet()) {
String title = ref.replace(DEFINITIONS2, "");
Map<String, Object> defs = (Map<String, Object>) ((HashMap<String, Schema>) getObjectsDefinitions()).clone();
Schema<Object> ob = (Schema<Object>) defs.get(title);
@@ -74,7 +76,10 @@ private Map<String, JsonNode> generateForObjects() throws Exception {
Map<String,Object> schemas = new HashMap<>();
schemas.put(SCHEMAS,defs);
res.put(COMPONENTS, schemas);
res.put(TITLE2, title);
if (isSwaggerFlattened) {
title = getMessageObjects().get(ref);
}
res.put(TITLE2, title);
log.info("Generating json schema for object '{}' of type {}", title,ob.getClass());
if (ob instanceof ObjectSchema) {
res.put(TYPE, ((ObjectSchema) ob).getType());
@@ -101,7 +106,7 @@ private Map<String, JsonNode> generateForObjects() throws Exception {
}
res.put($SCHEMA, HTTP_JSON_SCHEMA_ORG_DRAFT_04_SCHEMA);
removeUnusedObject(res,ob);
getGeneratedObjects().put(title, postprocess(res));
getGeneratedObjects().put(title, postprocess(res));
}
return getGeneratedObjects();